macro

Hello, macro definitions magical world

Only in the macro preprocessor in text replacement, no type, without any type checking, the compiler may be optimized to the same string. Only to keep a .rodata segment. Even with the same suffix string can also be optimized, you can use GCC compiler test, "Hello world" and "world" two strings, stores only one front. When taken just to give an intermediate of the front and address, if it is plastic, there will be multiple copies of the float, but these numbers written in the instruction. Accounted for only a snippet of it, a large number of binary files with macros will lead to larger.

The difference between macro definitions and global variables

  1. Macro is replaced in the preprocessing stage, global variables at runtime;
  2. Macro definition does not allocate memory, define global variables need to allocate memory;
  3. Macro does not distinguish between data types, which essentially is a range of characters, the location reference to be replaced when the pretreatment, data type specific global variables;
  4. After the macro definition value is not changed, the value of the global variable can be changed;
  5. Only use macro definitions in the definition file is located, or where the referenced file other documents. The global variables can be used in all project documents, just before use plus a statement.

FOUNDATION_EXTERN

#if defined(__cplusplus)
#define FOUNDATION_EXTERN extern "C"
#else
#define FOUNDATION_EXTERN extern
#endif

As can be seen from the above defined FOUNDATION_EXTERN be compatible with the extern C ++ macro.

FOUNDATION_EXTERN NSString * Extern_S = @"sss";

#define Define_S @"sss"

{
    NSString * s = @"tt";
        
    CFTimeInterval begin = CACurrentMediaTime();
        
    for (int i = 0; i < 10000; i++) {
        if ([s isEqualToString:Define_S]) {
            
        }
    }
    NSLog(@"%f", CACurrentMediaTime() - begin);
        
    begin = CACurrentMediaTime();
        
    for (int i = 0; i < 10000; i++) {
        if ([s isEqualToString:Extern_S]) {
            
        }
    }
    NSLog(@"%f", CACurrentMediaTime() - begin);
}

2019-09-09 16:06:04.849874+0800 Demo[86518:3255355] 0.000148
2019-09-09 16:06:04.850170+0800 Demo[86518:3255355] 0.000137

extern than a macro speed in the comparison character string to be faster, since the pointer address extern direct comparison, while the macro is a string comparison for equality.

__builtin_expect

In fact, this is a function, the compiler optimization for a function.

Write code, we often encounter a conditional statement

if (今天是工作日) {
    printf("好好上班");
}
else {
    printf("好好睡觉");
}

When the CPU reads the instruction is not a one to read, but come loaded with a number of such already loaded if(今天是工作日) printf(“好好上班”);instructions, this time, if a non-conditional expressions, in which non-working day, then the CPU continues to printf(“好好睡觉”);this instruction is loaded come in, thus causing the performance of the waste phenomenon.

__Builtin_expect first parameter is the actual value of the second parameter value is predicted. With this purpose it is to tell the compiler if the conditional expression is not there more likely to be met.

likely 和 unlikely

In fact, after unravel the macro is __builtin_expect package, likely represents established more likely, unlikely or may not represent more established.

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

Such encountered, if (likely (a == 0)) is understood as if (a == 0) can, unlikely is the same.

fastpath 和 slowpath

With the above is almost the same, fastpath represents more likely to be established, slowpath represents more may not hold

#define fastpath(x) ((typeof(x))__builtin_expect(_safe_cast_to_long(x), ~0l))
#define slowpath(x) ((typeof(x))__builtin_expect(_safe_cast_to_long(x), 0l))

Both like to understand likely and unlikely, which only need to focus on the type meets the conditions can be.

os_atomic_cmpxchg

Its interior is atomic_compare_exchange_strong_explicit function, the function returns: comparing the second parameter to the first parameter value if they are equal, the value of the third parameter value of the first parameter is replaced. If not equal, the value assigned to the first parameter the second parameter.

#define os_atomic_cmpxchg(p, e, v, m) \
        ({ _os_atomic_basetypeof(p) _r = (e); \
        atomic_compare_exchange_strong_explicit(_os_atomic_c11_atomic(p), \
        &_r, v, memory_order_##m, memory_order_relaxed); })

os_atomic_store2o

The second parameter, the first parameter to save

#define os_atomic_store2o(p, f, v, m)  os_atomic_store(&(p)->f, (v), m)
#define os_atomic_store(p, v, m) \
      atomic_store_explicit(_os_atomic_c11_atomic(p), v, memory_order_##m)

os_atomic_inc_orig

1 to save the first parameter

#define os_atomic_inc_orig(p, m)  os_atomic_add_orig((p), 1, m)
#define os_atomic_add_orig(p, v, m) _os_atomic_c11_op_orig((p), (v), m, add, +)
#define _os_atomic_c11_op_orig(p, v, m, o, op) \
        atomic_fetch_##o##_explicit(_os_atomic_c11_atomic(p), v, \
        memory_order_##m)

UNAVAILABLE_ATTRIBUTE , __has_include

Guess you like

Origin www.cnblogs.com/dins/p/macro.html