HomePhabricator

util: improve streams.h:FindByte() performance

Description

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`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D16180

Details

Provenance
Larry Ruane <larryruane@gmail.com>Authored on Aug 10 2020, 21:23
PiRKCommitted on May 17 2024, 13:54
PiRKPushed on May 17 2024, 13:54
Reviewer
Restricted Project
Differential Revision
D16180: util: improve streams.h:FindByte() performance
Parents
rABCa2c47d769cc1: [bench] add streams findbyte
Branches
Unknown
Tags
Unknown