optimization.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: optimization.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines portable macros for performance optimization.
  21. #ifndef ABSL_BASE_OPTIMIZATION_H_
  22. #define ABSL_BASE_OPTIMIZATION_H_
  23. #include "absl/base/config.h"
  24. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
  25. //
  26. // Instructs the compiler to avoid optimizing tail-call recursion. Use of this
  27. // macro is useful when you wish to preserve the existing function order within
  28. // a stack trace for logging, debugging, or profiling purposes.
  29. //
  30. // Example:
  31. //
  32. // int f() {
  33. // int result = g();
  34. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
  35. // return result;
  36. // }
  37. #if defined(__pnacl__)
  38. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  39. #elif defined(__clang__)
  40. // Clang will not tail call given inline volatile assembly.
  41. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  42. #elif defined(__GNUC__)
  43. // GCC will not tail call given inline volatile assembly.
  44. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  45. #elif defined(_MSC_VER)
  46. #include <intrin.h>
  47. // The __nop() intrinsic blocks the optimisation.
  48. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
  49. #else
  50. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  51. #endif
  52. // ABSL_CACHELINE_SIZE
  53. //
  54. // Explicitly defines the size of the L1 cache for purposes of alignment.
  55. // Setting the cacheline size allows you to specify that certain objects be
  56. // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
  57. // (See below.)
  58. //
  59. // NOTE: this macro should be replaced with the following C++17 features, when
  60. // those are generally available:
  61. //
  62. // * `std::hardware_constructive_interference_size`
  63. // * `std::hardware_destructive_interference_size`
  64. //
  65. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  66. // for more information.
  67. #if defined(__GNUC__)
  68. // Cache line alignment
  69. #if defined(__i386__) || defined(__x86_64__)
  70. #define ABSL_CACHELINE_SIZE 64
  71. #elif defined(__powerpc64__)
  72. #define ABSL_CACHELINE_SIZE 128
  73. #elif defined(__aarch64__)
  74. // We would need to read special register ctr_el0 to find out L1 dcache size.
  75. // This value is a good estimate based on a real aarch64 machine.
  76. #define ABSL_CACHELINE_SIZE 64
  77. #elif defined(__arm__)
  78. // Cache line sizes for ARM: These values are not strictly correct since
  79. // cache line sizes depend on implementations, not architectures. There
  80. // are even implementations with cache line sizes configurable at boot
  81. // time.
  82. #if defined(__ARM_ARCH_5T__)
  83. #define ABSL_CACHELINE_SIZE 32
  84. #elif defined(__ARM_ARCH_7A__)
  85. #define ABSL_CACHELINE_SIZE 64
  86. #endif
  87. #endif
  88. #ifndef ABSL_CACHELINE_SIZE
  89. // A reasonable default guess. Note that overestimates tend to waste more
  90. // space, while underestimates tend to waste more time.
  91. #define ABSL_CACHELINE_SIZE 64
  92. #endif
  93. // ABSL_CACHELINE_ALIGNED
  94. //
  95. // Indicates that the declared object be cache aligned using
  96. // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
  97. // load a set of related objects in the L1 cache for performance improvements.
  98. // Cacheline aligning objects properly allows constructive memory sharing and
  99. // prevents destructive (or "false") memory sharing.
  100. //
  101. // NOTE: this macro should be replaced with usage of `alignas()` using
  102. // `std::hardware_constructive_interference_size` and/or
  103. // `std::hardware_destructive_interference_size` when available within C++17.
  104. //
  105. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  106. // for more information.
  107. //
  108. // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
  109. // or `__declspec` attribute. For compilers where this is not known to work,
  110. // the macro expands to nothing.
  111. //
  112. // No further guarantees are made here. The result of applying the macro
  113. // to variables and types is always implementation-defined.
  114. //
  115. // WARNING: It is easy to use this attribute incorrectly, even to the point
  116. // of causing bugs that are difficult to diagnose, crash, etc. It does not
  117. // of itself guarantee that objects are aligned to a cache line.
  118. //
  119. // NOTE: Some compilers are picky about the locations of annotations such as
  120. // this attribute, so prefer to put it at the beginning of your declaration.
  121. // For example,
  122. //
  123. // ABSL_CACHELINE_ALIGNED static Foo* foo = ...
  124. //
  125. // class ABSL_CACHELINE_ALIGNED Bar { ...
  126. //
  127. // Recommendations:
  128. //
  129. // 1) Consult compiler documentation; this comment is not kept in sync as
  130. // toolchains evolve.
  131. // 2) Verify your use has the intended effect. This often requires inspecting
  132. // the generated machine code.
  133. // 3) Prefer applying this attribute to individual variables. Avoid
  134. // applying it to types. This tends to localize the effect.
  135. #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
  136. #elif defined(_MSC_VER)
  137. #define ABSL_CACHELINE_SIZE 64
  138. #define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE))
  139. #else
  140. #define ABSL_CACHELINE_SIZE 64
  141. #define ABSL_CACHELINE_ALIGNED
  142. #endif
  143. // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
  144. //
  145. // Enables the compiler to prioritize compilation using static analysis for
  146. // likely paths within a boolean branch.
  147. //
  148. // Example:
  149. //
  150. // if (ABSL_PREDICT_TRUE(expression)) {
  151. // return result; // Faster if more likely
  152. // } else {
  153. // return 0;
  154. // }
  155. //
  156. // Compilers can use the information that a certain branch is not likely to be
  157. // taken (for instance, a CHECK failure) to optimize for the common case in
  158. // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
  159. //
  160. // Recommendation: Modern CPUs dynamically predict branch execution paths,
  161. // typically with accuracy greater than 97%. As a result, annotating every
  162. // branch in a codebase is likely counterproductive; however, annotating
  163. // specific branches that are both hot and consistently mispredicted is likely
  164. // to yield performance improvements.
  165. #if ABSL_HAVE_BUILTIN(__builtin_expect) || \
  166. (defined(__GNUC__) && !defined(__clang__))
  167. #define ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
  168. #define ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
  169. #else
  170. #define ABSL_PREDICT_FALSE(x) (x)
  171. #define ABSL_PREDICT_TRUE(x) (x)
  172. #endif
  173. // ABSL_INTERNAL_ASSUME(cond)
  174. // Informs the compiler than a condition is always true and that it can assume
  175. // it to be true for optimization purposes. The call has undefined behavior if
  176. // the condition is false.
  177. // In !NDEBUG mode, the condition is checked with an assert().
  178. // NOTE: The expression must not have side effects, as it will only be evaluated
  179. // in some compilation modes and not others.
  180. //
  181. // Example:
  182. //
  183. // int x = ...;
  184. // ABSL_INTERNAL_ASSUME(x >= 0);
  185. // // The compiler can optimize the division to a simple right shift using the
  186. // // assumption specified above.
  187. // int y = x / 16;
  188. //
  189. #if !defined(NDEBUG)
  190. #define ABSL_INTERNAL_ASSUME(cond) assert(cond)
  191. #elif ABSL_HAVE_BUILTIN(__builtin_assume)
  192. #define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond)
  193. #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
  194. #define ABSL_INTERNAL_ASSUME(cond) \
  195. do { \
  196. if (!(cond)) __builtin_unreachable(); \
  197. } while (0)
  198. #elif defined(_MSC_VER)
  199. #define ABSL_INTERNAL_ASSUME(cond) __assume(cond)
  200. #else
  201. #define ABSL_INTERNAL_ASSUME(cond) \
  202. do { \
  203. static_cast<void>(false && (cond)); \
  204. } while (0)
  205. #endif
  206. // ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
  207. // This macro forces small unique name on a static file level symbols like
  208. // static local variables or static functions. This is intended to be used in
  209. // macro definitions to optimize the cost of generated code. Do NOT use it on
  210. // symbols exported from translation unit since it may casue a link time
  211. // conflict.
  212. //
  213. // Example:
  214. //
  215. // #define MY_MACRO(txt)
  216. // namespace {
  217. // char VeryVeryLongVarName[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt;
  218. // const char* VeryVeryLongFuncName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  219. // const char* VeryVeryLongFuncName() { return txt; }
  220. // }
  221. //
  222. #if defined(__GNUC__)
  223. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x
  224. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x)
  225. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
  226. asm(ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.absl.__COUNTER__))
  227. #else
  228. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME()
  229. #endif
  230. #endif // ABSL_BASE_OPTIMIZATION_H_