A concept,
In the project, the functions that need to be called at startup are grouped together (for example, in the first 10 pages) to minimize page faults and achieve optimization. And this is called binary rearrangement
* * * *
Second, the principle of
- First of all,
Xcode
Is using a linker calledld
,ld
There’s a parameter calledOrder File
, we can configure one with this parameterorder
File path. - In this
order
In the file, write the symbols you need in order. - When the project
build
The time,Xcode
The file is read, and the binary packages are generated in the order of the symbols in the filemach-O
\
3. How to view the symbol order of your project
Before and after the rearrangement, we need to check whether our symbol order has been changed successfully. This is where Link Map is used
\
The Link Map is produced during compilation. (The ld reads the binaries in the order shown in Compile source-GUI by default.) It records the layout of the binaries.
Set Write Link Map File to output or not. The default value is no.
Set Write Link Map File to YES as shown in the following figure
Clean up after modification, run project, product-show in Finder, as shown below
Find the.txt file as shown below
Open the.txt file shown in the figure below
As you can see, the order of symbols is clearly in the file order Compile Sources uses
4. How to realize the binary rearrangement
4.1
Go to the project root directory, create a new file my_oder.order, and pick a few methods that need to be loaded at startup (I’ll use the ViewController method as an example).
The my_oder.order file is shown below
The methods in the ViewController are shown below
4.2 Configuring the oder file path
4.3 Rerun and viewThe symbol order of the project
As you can see, the three methods that we wrote have been brought up to the top, so far,
The resulting code with the smallest offset from the first address in macho is the three methods we wrote,
Assuming that the three methods were originally on three different pages, we have optimized away two page faults.
So that’s the implementation of binary rearrangement