"Go" iOS inline function inline

Source: Internet
Author: User

iOS inline function inline reason

As a result of learning to use the UISCROLLVEW development process, the following attribute is encountered (setting the padding):

@property(nonatomic)         UIEdgeInsets                 scrollIndicatorInsets;          // default is UIEdgeInsetsZero. adjust indicators inside 
    • 1

Light Look at UIEdgeInsets this type, for a moment still do not know how its concrete internal structure is how, so continue to point in to find it is defined as follows:

typedef struct UIEdgeInsets {    CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to ‘outset‘} UIEdgeInsets;
    • 1
    • 2
    • 3

It turned out to be such a structural body! ~ followed by, see and UIEdgeInsets related use methods, enumerate parts:

UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {    UIEdgeInsets insets = {top, left, bottom, right};    return insets;}UIKIT_STATIC_INLINE UIOffset UIOffsetMake(CGFloat horizontal, CGFloat vertical) {    UIOffset offset = {horizontal, vertical};    return offset;}...
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

Looking at the code above, there is a thing I do not know UIKIT_STATIC_INLINE , continue to point in to see that this is a macro:

#define UIKIT_STATIC_INLINE static inline
    • 1

Oh, it's meant to tell the compiler that this function is a static inline function! inline function? Well familiar ah, but do not remember what the role of the specific, so Baidu Baidu!! I have come to the conclusion that I am impressed:

    • Inline functions are introduced to solve the problem of function call efficiency

    • Because calls between functions are transferred from one memory address to another, the address of the original function execution is returned when the function call is complete. function calls have a certain amount of time overhead, and an inline function is introduced to solve this problem.

Practice

So what's the difference between referencing an inline function? In case the interview asks, it can only answer the question "in order to solve the problem of function call efficiency"? If the interviewer asks again, "How to solve it?" Why not write your own code test to see?!! Open Xcode.

Code One

Description: Define an Add (int,int) function and declare static inline it as, and invoke.

Header file: inline.h

//  inline.h//  inline//  Created by fenglh on 15/8/24.//  Copyright (c) 2015年 fenglh. All rights reserved.#ifndef inline_inline_h#define inline_inline_hstatic inline int add(int a, int b){ return a+b;}#endif
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

. m File: main.m

//main.m//inline//Created by Fenglj on 15/8/24. Copyright (c) 2015 FENGLH. All rights reserved.  #import <foundation/foundation.h> #import " Inline.h "int Main (int argc,  Const char * argv[]) {int c = Add (1, Span class= "Hljs-number" >2); return 0;}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

View MAIN.M's assembly file as follows:

    . Section __text,__text,regular,pure_instructions. Globl _main. align4,0x90_main:# # @mainLfunc_begin0:. Loc2140# #/users/fenglihai/desktop/inline/inline/main.m:14:0. Cfi_startproc# # Bb#0:pushq%RBPltmp0: .cfi_def_cfa_offset 16 LTMP1: .cfi_offset%rbp,-16 Movq%rsp,%rbpLTMP2: .cfi_def_cfa_register%rbp # #DEBUG_VALUE: MAIN:ARGC <-EDI # #DEBUG_VALUE: Main: argv <-rsiltmp3: # #DEBUG_VALUE: Main:c <-3 Xorl%eax, %eax .loc 2 17 5 prologue_end ##/users/fenglihai/desktop/inline/inline/main.m:17:5 ltmp4:popq%rbp retq           
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
Code two

Description: Defines an Add (int,int) function and invokes it.

Header file: Header.h

//  Header.h//  notInline//  Created by fenglh on 15/8/25.//  Copyright (c) 2015年 fenglh. All rights reserved.#ifndef notInline_Header_h#define notInline_Header_h int add(int a, int b){ return a+b;}#endif
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

. m File: main.m

//main.m//notinline//Created by FENGLH on 15/8/25. Copyright (c) 2015 FENGLH. All rights reserved. // #import <foundation/foundation.h> #import "Header.h" int Main (int argc, Span class= "Hljs-keyword" >const char * argv[]) {int C = Add ( Span class= "Hljs-number" >1,2); return 0;}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

View MAIN.M's assembly file as follows:

. Section __text,__text,regular,pure_instructions. globl _add. Align4,0x90_add:# # @addLfunc_begin0:. File3"/users/fenglihai/desktop/notinline/notinline""Header.h". Loc3120# #/users/fenglihai/desktop/notinline/notinline/header.h:12:0. Cfi_startproc# # Bb#0:pushq%rbpltmp0:. Cfi_def_cfa_offset16LTMP1:. Cfi_offset%RBP,-Movq%RSP,%RBPLTMP2:. Cfi_def_cfa_register%RBP MOVL%edi,-4 (%RBP) MOVL%esi,-8 (%RBP). Loc3135 Prologue_end# #/USERS/FENGLIHAI/DESKTOP/NOTINLINE/NOTINLINE/HEADER.H:13:5LTMP3:MOVL-4 (%RBP),%esi Addl-8 (%RBP),%esi MOVL%esi,%eax POPQ%RBP Retqltmp4:lfunc_end0:. Cfi_endproc. globl _main. Align4,0x90_main:# # @mainLfunc_begin1:. Loc2120# #/users/fenglihai/desktop/notinline/notinline/main.m:12:0. Cfi_startproc# # Bb#0:pushq%RBPLTMP5:. Cfi_def_cfa_offset16LTMP6:. Cfi_offset%RBP,-Movq%RSP,%RBPLTMP7:. Cfi_def_cfa_register%RBP SUBQ$32,%RSP MOVL$,%eax MOVL$,%ECX MOVL$ A,-4 (%RBP) MOVL%edi,-8 (%RBP) movq%rsi,-16 (%RBP). Loc21313 prologue_end ##/users/fenglihai/desktop/notinline/ NOTINLINE/MAIN.M:13:13LTMP8:MOVL %eax, %edi movl %ecx, %esi callq _add xorl %ecx, %ecx movl %eax,-20 (%RBP"). Loc 2 14 5 Span class= "hljs-comment" >##/users/fenglihai/desktop/notinline/notinline/main.m:14:5 MOVL %ecx, %eax addq $32, %rsp popq %RBP retq       
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • +
    • -
    • -
    • +

As you can see from the 2 code above, only the definition of the Add function is different, one is static inline modified, and the other is not. Then compare the assembly code, found that there is really a big difference!!! The first feeling, the useful decoration of the code after the assembly is static inline not static inline modified after the compilation of the code is much more concise!!

Second, in the static inline MAIN.M assembly code that does not call the decorated add function, the Add function has a separate assembly code!

Instead of MAIN.M assembly code that uses inline functions, only the assembly code of the main function!

And look at the MAIN.M assembler code that uses inline functions:

Comparing the assembly code of the two mian.m, you can find that the call `static inline command will appear without the Mian function assembly code that uses the decorated inline function! That's the difference! Calling the call command is what you need:
- (1) Put the address of the next instruction (that is, the contents of the program counter PC) into the stack
- (2) and the starting address of the subroutine is fed into the PC (so the next instruction of the CPU will be transferred to execute the subroutine).

NN, for the assembly is not nonsense, with the school when learning a little assembly can only go deep here! Alas! Then we know that the reason for the efficiency is in the solution call here!!

Conclusion

1. Use the inline modified function to embed the code directly into the calling code at compile time. It is equivalent to defining an add function with a # define macro definition! The difference from # define is:
1) #define定义的格式要有要求, and the use of inline the usual writing function, just add it `inline !
2) using the code defined by the # define macro, the compiler does not check for parameter validation, just to replace the symbol table.
3) #define宏定义的代码, the return value cannot be cast to a convertible suitable conversion type. Can refer to the Baidu liberal arts about inline

2. inline add `static modifier, just to show that the function is visible only in this file! That is, in the same project, even if there are other files in the same name, the same parameter function will not cause the function to repeat the definition of the error! **

Practice here, for the final understanding of the inline function, finally deepened understanding and memory!!

"Go" iOS inline function inline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.