Page MenuHomePhabricator

util: improve streams.h:FindByte() performance
ClosedPublic

Authored by PiRK on May 17 2024, 08:26.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABC3e657f8f3c1d: util: improve streams.h:FindByte() performance
Summary

The original code is doing a modulus operation for every character, which is often orders of magnitude slower than the byte comparision or addition/subtraction.
Move the expensive operation out of the loop.

Note that a simpler version of this fix ended up yielding worse performances on ARM Mac architectures, hence the switch to std::find (see https://github.com/bitcoin/bitcoin/pull/19690#pullrequestreview-1318324998)

Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>

This concludes backport of core#19690
https://github.com/bitcoin/bitcoin/pull/19690/commits/72efc26439da9a1344a19569fb0cab01f82ae7d1

Depends on D16179

Test Plan

ninja all check-all bench-bitcoin

|               ns/op |                op/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
Before:
|              306.76 |        3,259,865.52 |    0.5% |      0.01 | `FindByte`
After:
|               28.35 |       35,270,185.20 |    0.0% |      0.01 | `FindByte`