Skip to content

Faster base58 operations for 32/64 decoded strings - #99461

Merged
alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
jh0x:jh-base58
Mar 24, 2026
Merged

alexey-milovidov merged 10 commits into
ClickHouse:masterfrom
jh0x:jh-base58

Conversation

@jh0x

@jh0x jh0x commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #88774

Adapt Firedance code for very optimized base58 encode for lengths 32/64 and optimised decode where decoded length is 32/64

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Use optimised Firedancer base58 encode for inputs of 32/64 bytes (automatic for base58Encode). Allow using optimised base58 decode if decoded result is 32/64 bytes (explicit with base58Decode('...', 32) or alike).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.234

Comment thread src/Common/Base58fd.h Resolved
@jh0x jh0x changed the title Jh base58 Faster base58 operations for 32/64 decoded strings Mar 13, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dc74c5d]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Mar 14, 2026
Comment thread src/Common/Base58fd.h Outdated
if (binary[0] > 0xFFFFFFFFULL)
return std::nullopt;

uint32_t * out32 = reinterpret_cast<uint32_t *>(dst);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decodeBase58_32 writes through reinterpret_cast<uint32_t *>(dst). dst comes from ColumnString byte storage and is not guaranteed to be 4-byte aligned, so these stores are undefined behavior and can fail on strict-alignment targets. Please store as bytes (e.g. memcpy(dst + 4 * i, &word_be, 4)) or use an explicit unaligned store helper.

Comment thread src/Common/Base58fd.h Outdated
__m128i last = _mm_bslli_si128(_mm256_extractf128_si256(base58_1, 0), 3);
_mm_storeu_si128(reinterpret_cast<__m128i *>(dst + 29ULL - skip), last);

_mm256_maskstore_epi64(reinterpret_cast<long long *>(dst - skip), mask2, base58_0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This computes dst - skip, and for the first row dst points to the beginning of ColumnString storage. Even if masking avoids writing the lowest lane, forming a pointer before the start of the object is undefined behavior in C++ and can be miscompiled.

Please rewrite this store sequence so all base pointers stay within dst..dst+N (for example, keep the base at dst and adjust masks/shifts/offsets), then keep only in-bounds addresses for all rows.

Comment thread src/Common/Base58fd.h Outdated
__m256i mask2 = _mm256_cmpgt_epi64(compare, skip_div8);

auto dst_addr = reinterpret_cast<uintptr_t>(dst);
_mm256_maskstore_epi64(reinterpret_cast<long long *>(dst_addr - 8ULL * (skip / 8ULL)), mask1, shifted);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encodeBase58_64 has the same UB pattern as the 32-byte path: dst_addr - 8ULL * (skip / 8ULL) / dst_addr - skip can form a pointer before the start of the destination row. Even with masked stores, creating out-of-bounds pointers is undefined behavior. Please rewrite this store sequence so all pointer values stay within object bounds (for example, stage to a scratch buffer and memcpy only the valid output slice).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed - by introducing a stack buffer with enough headroom. This is not present in Firedancer code.

@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.70% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 96.13% (298/310, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Mar 24, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 24, 2026
Merged via the queue into ClickHouse:master with commit 4a8fefe Mar 24, 2026
159 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 24, 2026
@ptaffet-jump

Copy link
Copy Markdown

Hi there,
Great to see this code getting more use! Just FYI, we hit a GCC 15 regression when compiling this code for use in Firedancer, so you may want to be careful with that version.

@alexey-milovidov

Copy link
Copy Markdown
Member

Should be ok, we compile with clang 21+

@Algunenano

Copy link
Copy Markdown
Member

Note that this optimization introduced bugs: #104394

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 8, 2026
`base58Decode(s, n)` and `tryBase58Decode(s, n)` rejected a value that did not
decode to exactly `n` bytes only at n = 32 and n = 64, the two sizes the fixed-size
decoders served, and ignored the argument at every other value. With those decoders
removed the argument has no decoder left to select, so the asymmetry was all that
was left of it: `base58Decode('2', 32)` threw while `base58Decode('2', 34)` returned
one byte.

The requirement now holds at every size. 32 and 64 keep exactly the behaviour they
have shipped since 26.4.1 (ClickHouse#99461): over 101764 (input, size) pairs of the real
decoder, 0 outcomes differ there, and no input moves from rejected to accepted at
any size. Other sizes reject a wrong-sized value where they previously ignored the
request, which is a released behaviour change and the reason for the changelog
category. Ignoring the argument instead would have replaced a rejection a caller
may be using as a key-length guard with silent acceptance, which is the worse of
the two directions.

`maxBase58EncodedLength` generalizes the length window: an n-byte value encodes to
between n and n*1366/1000+1 characters, so an input outside that window cannot
decode to n and is rejected without paying the quadratic conversion (95132 of the
101764 pairs). The window is a necessary condition only, and over the same sweep it
never rejected an input that the exact decoded-size check accepts, so it stays an
optimization rather than part of the contract. Passing its first comparison bounds
`expected_size` by the input length, which is what keeps the product in the second
from overflowing for a user-supplied `UInt64`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Faster base58 encoding for FixedString(32) and FixedString(64)

5 participants