from https://blog.twitter.com/engineering/en_us/a/2014/attribute-directives-in-objective-c#:~:text=attribute__%20resources-,What%20are%20__attribute__%20directives%3F,the%20consumer%20of%20that%20code.
What are __attribute__ directives?
The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.
Better said, __attribute__ directives provide context. The value of providing context to code cannot be overstated. Developers have provided context by way of explicit declarations and comments since before the advent of the integrated circuit, but the value of providing context that can be evaluated by a compiler gives us a whole new level of control. By explicitly providing the confines of how an API behaves to the compiler, a programmer can gain some tangible benefits. The directives can be used to enforce compliance with how other programmers consume that API. In other cases, __attribute__ directives can help the compiler to optimize - sometimes to large performance gains.
As Mattt Thompson cogently put it in a blog post: “Context is king when it comes to compiler optimizations. By providing constraints on how to interpret your code, [you’ll increase] the chance that the generated code is as efficient as possible. Meet your compiler half-way, and you’ll always be rewarded… [It] isn’t just for the compiler either: The next person to see the code will appreciate the extra context, too. So go the extra mile for the benefit of your collaborator, successor, or just 2-years-from-now you.” Which leads nicely into wise words from Sir Paul McCartney, “and in the end, the love you take is equal to the love you make.”
reference for C/C++ compilers
Variable Attributes (Using the GNU Compiler Collection (GCC))

本文介绍了Objective-C编程中__attribute__指令的作用,它如何提供代码上下文以优化编译,增强API约束并可能带来显著性能提升。通过实例和引用开发者观点,展示了__attribute__在提升代码质量和性能上的重要性。

被折叠的 条评论
为什么被折叠?



