The cause of
Used in the projectYYKitSome components, such asYYTextandYYImage“Took a moment to read some of the code.
Found in YYKit used a lot of function methods, such as:
static inline UIEdgeInsets UIEdgeInsetRotateVertical(UIEdgeInsets insets) {
UIEdgeInsets one;
one.top = insets.left;
one.left = insets.bottom;
one.bottom = insets.right;
one.right = insets.top;
return one;
}
Copy the code
This kind of:
static void YYTextGetRunsMaxMetric(CFArrayRef runs, CGFloat *xHeight, CGFloat *underlinePosition, CGFloat *lineThickness) {
CGFloat maxXHeight = 0;
CGFloat maxUnderlinePos = 0;
CGFloat maxLineThickness = 0;
for (NSUInteger i = 0, max = CFArrayGetCount(runs); i < max; i++) {
CTRunRef run = CFArrayGetValueAtIndex(runs, i);
CFDictionaryRef attrs = CTRunGetAttributes(run);
if (attrs) {
CTFontRef font = CFDictionaryGetValue(attrs, kCTFontAttributeName);
if (font) {
CGFloat xHeight = CTFontGetXHeight(font);
if (xHeight > maxXHeight) maxXHeight = xHeight;
CGFloat underlinePos = CTFontGetUnderlinePosition(font);
if (underlinePos < maxUnderlinePos) maxUnderlinePos = underlinePos;
CGFloat lineThickness = CTFontGetUnderlineThickness(font);
if (lineThickness > maxLineThickness) maxLineThickness = lineThickness;
}
}
}
if (xHeight) *xHeight = maxXHeight;
if (underlinePosition) *underlinePosition = maxUnderlinePos;
if (lineThickness) *lineThickness = maxLineThickness;
}
Copy the code
And this:
CGFloat YYTextScreenScale() {
static CGFloat scale;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
scale = [UIScreen mainScreen].scale;
});
return scale;
}
Copy the code
These three functions are similar in use and look alike, but there are still some differences. If we observe, we know:
The first method, which uses static and inline identifiers, returns UIEdgeInsets. The second method, which uses static only, returns void. The third method, which does not include static at all, returns CGFloat.Copy the code
So what’s the difference between the three? To look down
Analysis of the
As observed above, the basic difference among the three is the difference in the use of identifiers. Then, let’s analyze the advantages and benefits of different uses of identifiers.
A, references,inline
identifier
A reference to the inline identifier makes function 1 a standard inline function, whose code is placed in a symbol table and replaced directly when used (expanded like a macro).
In general, the introduction of inline function is to solve the problem of function call efficiency. The call between functions is the call between memory addresses. When the function call is completed, it will return the address of the original function execution, which will have a certain time overhead. A function that does not use inline modifications will call the call directive when assembled.
1) push the address of the next instruction to the stack 2) and send the starting address of the subroutine to the PC (so that the next instruction of the CPU goes to execute the subroutine).Copy the code
An inline function in GCC can be equivalent to adding an inline attribute to a normal global function. The inline keyword simply suggests that the compiler perform inline expansion at compile time, rather than forcing the GCC compiler to ignore the inline keyword. In some cases, the compiler will automatically ignore the inline keyword and revert the function to a normal function. If the compile option is set to negative infinity, even inline functions will not be expanded inline unless the attribute((always_inline)) that forces inline expansion is set, namely the NS_INLINE macro definition.
Second, the referencestatic
identifier
It is usually used to declare static variables.
1) When modifying a local variable, let the local variable be initialized only once. The local variable has only one memory in the program, but does not change the scope of the local variable, only changes the life cycle of the local variable (only until the end of the program, the local variable will be destroyed). 2) When modifying global variables, the scope of global variables is limited to the current file.Copy the code
When modifying a function, it affects the way the function is connected so that the function is only valid within this file and not visible to other files. Such functions are also called static functions. The advantage of using static functions is that you don’t need to worry about interfering with functions with the same name in other files.
If you want other files to refer to local functions, use the extern keyword in the function definition to indicate that the function is external and can be called by other files. Extern is also used to declare external functions in files that reference external functions defined in other files.
In addition, the static modifier ensures that copy is not constantly called, ensuring consistent function addresses and reducing memory stress.
3. The above two identifiers are not used
A quick look at this function shows that this function is a construction of a singleton method, relatively rare usage, relatively concise, this is still a simple function method that can be called externally with CG_EXTERN.
Static functions are restricted to this source file and cannot be called outside this source file. A normal function, by default, is extern; that is, it can be called by other code files.
conclusion
Why can inline functions replace macros?
1) the function defined by #define has special formatting requirements that not everyone is familiar with. Using 'inline' is just like writing normal functions. 2) Like other macro definitions, the compiler will not check the validity of the parameters of the code defined by define macro, which is prone to undetected errors and will cause a lot of trouble during debugging. 3) Not only the input type, the #define macro defines the code, the return value cannot be cast to the suitable conversion class for conversion. 4) #define is a text replacement that needs to be expanded at precompile time, while inline functions are expanded at compile time.Copy the code
Inline functions have advantages over normal functions:
1) Inline functions avoid the disadvantages of ordinary functions that must call when assembling. It eliminates the function parameter pressing, reduces the overhead of the call, and improves efficiency. Therefore, the execution speed is really faster than the general function. 2) Integrate the advantages of macros, when using directly with code replacement (like macros);Copy the code
Description of inline functions
1) An inline function is just a request we provide to the compiler. The compiler does not necessarily call the function inline. 2) An inlined function cannot carry a large amount of code. If the function body of an inlined function is too large, the compiler will automatically abandon inlining. 3) Loop statements or switch statements are not allowed in inline functions. 4) Inline functions must be defined before they are called. 5) When using an inline function, if the function is called at more than one place, the function will have N copies of the code segment, so it is used with the 'static' identifier.Copy the code
Refrence
- Inline and static functions in C++
- All iOS variables (static/extren/const, etc.)
In addition
- Jane’s address book
- Denver address