mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
-9ee91d3e43Export of internal Abseil changes by Abseil Team <absl-team@google.com> -8efba58a3bExport of internal Abseil changes by Abseil Team <absl-team@google.com> -b49b8d16b6Export of internal Abseil changes by Abseil Team <absl-team@google.com> -67222ffc4cExport of internal Abseil changes by Abseil Team <absl-team@google.com> -c5c4db4f51Export of internal Abseil changes by Abseil Team <absl-team@google.com> -14550beb3bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -52e88ee56bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -36d37ab992Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -ad1485c898Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -f3840bc5e3Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -278b26058cExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -c6c3c1b498Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -44efe96dfcExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -3c98fcc046Merge pull request #340 from jtsylve/macos_cxx17_fix by Matt Calabrese <38107210+mattcalabrese-google@users.noreply.github.com> -74d91756c1Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -e6b050212cExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -c964fcffacExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -72e09a54d9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -d65e19dfcdExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -5162fc83d2Merge pull request #336 from shields/patch-2 by Shaindel Schwartz <31392632+shaindelschwartz@users.noreply.github.com> -0389f7bf58Merge pull request #335 from shields/patch-1 by Shaindel Schwartz <31392632+shaindelschwartz@users.noreply.github.com> -e9324d926aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -43ef2148c0Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -a13d3df2b3Merge pull request #323 from gosnik/master by Gennadiy Rozental <rogeeff@google.com> -310a11865cMerge pull request #324 from RasPat1/patch-1 by Gennadiy Rozental <rogeeff@google.com> -8f11724067Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -b1dd425423Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -361cb8a9dbExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -0238ab0a83Merge pull request #321 from christoph-cullmann/c4245_fix... by Xiaoyi Zhang <zhangxy988@gmail.com> -61c9bf3e3eExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -bc9101f998Merge pull request #320 from christoph-cullmann/master by Xiaoyi Zhang <zhangxy988@gmail.com> -2f76a9bf50Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -4adaf54909Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -27c30ec671Avoid undefined behavior when nullptr is passed to memcpy... by Roman Gershman <romange@gmail.com> -ce65f5ac3cExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -a18fc7461eExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -8a394b19c1Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -daf381e853Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -fa00c32107Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -436ba6c4a0Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -0cbdc774b9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -27c2f6e2f3Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -aa468ad755Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -cd86d0d20aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -33841c5c96Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -ca3f87560aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -d902eb869bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -a02f62f456Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -0b545b4601Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -dbae8764fbExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -044da8a29cExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -6cc6ac44e0Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -666fc1266bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -93dfcf74cbExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -2c8421e1c6Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -5b65c4af51Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -eab2078b53Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -253eb74164[CMake] Set correct flags for clang-cl (#278) by Loo Rong Jie <loorongjie@gmail.com> -e75672f6afExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -bf29470384Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -6fd827124fMerge pull request #280 from chiumichael/master by Derek Mauro <761129+derekmauro@users.noreply.github.com> -7c7754fb3eExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -256be56344Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -88a152ae74Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -c1cecb25a9Implement Span::first and Span::last from C++20 (#274) by Girts <girtsf@users.noreply.github.com> -38b704384cChanged HTTP URLs to HTTPS where possible (#270) by nik7273 <nik8470@gmail.com> -febc5ee6a9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -9fdf5e5b80Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -419f3184f8Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -b312c3cb53Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -308ce31528Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -93d155bc44Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -426eaa4aa4Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -2901ec32a9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -d78310fe5aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -a4cb1c8ba6Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -540e2537b9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -89ea0c5ff3Merge pull request #255 from uilianries/hotfix/conan by ahedberg <ahedberg@google.com> -5e0dcf72c6Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -0dffca4e36Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -6b4201f9efFix GCC8 warnings by Boris Staletic <boris.staletic@gmail.com> -0b1e6d417bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -efccc50260Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -5e6a78131fExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -5eea0f713cExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -66f9becbb9Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -018b4db1d7Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -9449ae9439Merge pull request #243 from ThomsonTan/FixIntrinsic by Alex Strelnikov <strel@google.com> -b16aeb6756Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -7ffbe09f3dExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -01b471d9f3Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -7bd8f36c74Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -968a34ffdaExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -3e2e9b5557Merge pull request #231 from uilianries/feature/conan by Mark Barolak <mbxx@users.noreply.github.com> -111ca7060aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -389ec3f906Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -8fbcdb9095Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -455dc17ba1Export of internal Abseil changes. by Abseil Team <absl-team@google.com> -f197d7c72aExport of internal Abseil changes. by Abseil Team <absl-team@google.com> -284378a71bExport of internal Abseil changes. by Abseil Team <absl-team@google.com> GitOrigin-RevId:9ee91d3e43Change-Id: Ia06e548bc106cc9d136f6c65714be6645317aced
182 lines
6.8 KiB
C++
182 lines
6.8 KiB
C++
//
|
|
// Copyright 2017 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// -----------------------------------------------------------------------------
|
|
// File: optimization.h
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// This header file defines portable macros for performance optimization.
|
|
|
|
#ifndef ABSL_BASE_OPTIMIZATION_H_
|
|
#define ABSL_BASE_OPTIMIZATION_H_
|
|
|
|
#include "absl/base/config.h"
|
|
|
|
// ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
|
|
//
|
|
// Instructs the compiler to avoid optimizing tail-call recursion. Use of this
|
|
// macro is useful when you wish to preserve the existing function order within
|
|
// a stack trace for logging, debugging, or profiling purposes.
|
|
//
|
|
// Example:
|
|
//
|
|
// int f() {
|
|
// int result = g();
|
|
// ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
|
|
// return result;
|
|
// }
|
|
#if defined(__pnacl__)
|
|
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
|
|
#elif defined(__clang__)
|
|
// Clang will not tail call given inline volatile assembly.
|
|
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
|
|
#elif defined(__GNUC__)
|
|
// GCC will not tail call given inline volatile assembly.
|
|
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
|
|
#elif defined(_MSC_VER)
|
|
#include <intrin.h>
|
|
// The __nop() intrinsic blocks the optimisation.
|
|
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
|
|
#else
|
|
#define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
|
|
#endif
|
|
|
|
// ABSL_CACHELINE_SIZE
|
|
//
|
|
// Explicitly defines the size of the L1 cache for purposes of alignment.
|
|
// Setting the cacheline size allows you to specify that certain objects be
|
|
// aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
|
|
// (See below.)
|
|
//
|
|
// NOTE: this macro should be replaced with the following C++17 features, when
|
|
// those are generally available:
|
|
//
|
|
// * `std::hardware_constructive_interference_size`
|
|
// * `std::hardware_destructive_interference_size`
|
|
//
|
|
// See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
|
|
// for more information.
|
|
#if defined(__GNUC__)
|
|
// Cache line alignment
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#elif defined(__powerpc64__)
|
|
#define ABSL_CACHELINE_SIZE 128
|
|
#elif defined(__aarch64__)
|
|
// We would need to read special register ctr_el0 to find out L1 dcache size.
|
|
// This value is a good estimate based on a real aarch64 machine.
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#elif defined(__arm__)
|
|
// Cache line sizes for ARM: These values are not strictly correct since
|
|
// cache line sizes depend on implementations, not architectures. There
|
|
// are even implementations with cache line sizes configurable at boot
|
|
// time.
|
|
#if defined(__ARM_ARCH_5T__)
|
|
#define ABSL_CACHELINE_SIZE 32
|
|
#elif defined(__ARM_ARCH_7A__)
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef ABSL_CACHELINE_SIZE
|
|
// A reasonable default guess. Note that overestimates tend to waste more
|
|
// space, while underestimates tend to waste more time.
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#endif
|
|
|
|
// ABSL_CACHELINE_ALIGNED
|
|
//
|
|
// Indicates that the declared object be cache aligned using
|
|
// `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
|
|
// load a set of related objects in the L1 cache for performance improvements.
|
|
// Cacheline aligning objects properly allows constructive memory sharing and
|
|
// prevents destructive (or "false") memory sharing.
|
|
//
|
|
// NOTE: this macro should be replaced with usage of `alignas()` using
|
|
// `std::hardware_constructive_interference_size` and/or
|
|
// `std::hardware_destructive_interference_size` when available within C++17.
|
|
//
|
|
// See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
|
|
// for more information.
|
|
//
|
|
// On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
|
|
// or `__declspec` attribute. For compilers where this is not known to work,
|
|
// the macro expands to nothing.
|
|
//
|
|
// No further guarantees are made here. The result of applying the macro
|
|
// to variables and types is always implementation-defined.
|
|
//
|
|
// WARNING: It is easy to use this attribute incorrectly, even to the point
|
|
// of causing bugs that are difficult to diagnose, crash, etc. It does not
|
|
// of itself guarantee that objects are aligned to a cache line.
|
|
//
|
|
// NOTE: Some compilers are picky about the locations of annotations such as
|
|
// this attribute, so prefer to put it at the beginning of your declaration.
|
|
// For example,
|
|
//
|
|
// ABSL_CACHELINE_ALIGNED static Foo* foo = ...
|
|
//
|
|
// class ABSL_CACHELINE_ALIGNED Bar { ...
|
|
//
|
|
// Recommendations:
|
|
//
|
|
// 1) Consult compiler documentation; this comment is not kept in sync as
|
|
// toolchains evolve.
|
|
// 2) Verify your use has the intended effect. This often requires inspecting
|
|
// the generated machine code.
|
|
// 3) Prefer applying this attribute to individual variables. Avoid
|
|
// applying it to types. This tends to localize the effect.
|
|
#define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
|
|
#elif defined(_MSC_VER)
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE))
|
|
#else
|
|
#define ABSL_CACHELINE_SIZE 64
|
|
#define ABSL_CACHELINE_ALIGNED
|
|
#endif
|
|
|
|
// ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
|
|
//
|
|
// Enables the compiler to prioritize compilation using static analysis for
|
|
// likely paths within a boolean branch.
|
|
//
|
|
// Example:
|
|
//
|
|
// if (ABSL_PREDICT_TRUE(expression)) {
|
|
// return result; // Faster if more likely
|
|
// } else {
|
|
// return 0;
|
|
// }
|
|
//
|
|
// Compilers can use the information that a certain branch is not likely to be
|
|
// taken (for instance, a CHECK failure) to optimize for the common case in
|
|
// the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
|
|
//
|
|
// Recommendation: Modern CPUs dynamically predict branch execution paths,
|
|
// typically with accuracy greater than 97%. As a result, annotating every
|
|
// branch in a codebase is likely counterproductive; however, annotating
|
|
// specific branches that are both hot and consistently mispredicted is likely
|
|
// to yield performance improvements.
|
|
#if ABSL_HAVE_BUILTIN(__builtin_expect) || \
|
|
(defined(__GNUC__) && !defined(__clang__))
|
|
#define ABSL_PREDICT_FALSE(x) (__builtin_expect(x, 0))
|
|
#define ABSL_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
|
|
#else
|
|
#define ABSL_PREDICT_FALSE(x) (x)
|
|
#define ABSL_PREDICT_TRUE(x) (x)
|
|
#endif
|
|
|
|
#endif // ABSL_BASE_OPTIMIZATION_H_
|