3

If I compile Bitcoin Core using the instructions in the README, does the C++ compiler use compiler optimizations for my current CPU, or will it output a "generic" binary, that can run on almost any CPU? For example if my CPU supports the AVX-512 instructions, will the compiler automatically output a suitable binary or does it not take into account any CPU features at all? If so, how can I make sure I have the optimal binary running on my machine?

3

By default, yes and no.

No, most of the code is compiled for generic target hardware. If you want something optimized for your actual hardware, you can add -march=native to your compilation flags (e.g. you'd run ./configure CFLAGS="-march=native" CXXFLAGS="-march=native"). This will result in a binary that may not work on other hardware than your own. This likely gives you some performance gains, but is unlikely to have a huge impact, because...

Yes, some code has several variants that are always compiled in, and the optimal one is decided at runtime based on available hardware features. This includes:

  • SHA256 versions for SSE4, SSE4.1, AVX2, and SHA-NI exist.
  • CRC32 versions (inside LevelDB) for ARM64 and SSE4.2 exist.
  • The internal random number generator will use RDRAND/RDSEED if provided by the hardware (in addition to other randomness sources).

Certainly more optimizations are possible for specific hardware or features, but generally this isn't as easy as just turning a compiler flag - it also needs different code to actually make use of it. For example, compiling generic SHA256 code with -march=native will give a modest speedup, but using code that is designed to make use of the 256-bit wide registers AVX2 can be several times faster in some cases than that, by computing multiple hashes in parallel.

7
  • Thanks for the detailed answer! Are there any benchmarks on what CPUs are the best to use? What are the most important instruction set extensions that will give the biggest performance gains? Nov 25 '20 at 22:07
  • 1
    Bitcoin Core will run fastest on x86_64 hardware with SHA-NI instructions enabled. But things like how much memory you have for your dbcache has a far bigger impact on performance than any compiler flag. Nov 25 '20 at 22:12
  • My question was more about criteria for purchasing a CPU that will be optimal for running a Bitcoin node. But if you say the supported instruction set extensions don't really matter, I guess the most important factor is just raw performance. I was just thinking that because signature verification is a huge part of what Bitcoin Core does, it might make sense to understand what instructions set extensions are most important. Nov 25 '20 at 22:15
  • 2
    The signature validation code itself doesn't have any hardware-specific optimizations (it uses handwritten x86_64 assembly, but doesn't use any optional features). A significant part of validation is hashing though, so SHA256 performance matters. Nov 25 '20 at 22:17
  • 2
    That's exactly what SHA-NI is, and Bitcoin Core uses it. Apparently the proper name is "Intel SHA extensions", see en.wikipedia.org/wiki/Intel_SHA_extensions (ironically, there is fairly little Intel hardware that supports it, while recent AMD desktop CPUs all do). Nov 25 '20 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.