Repository navigation
Faster base58 operations for 32/64 decoded strings - #99461
Conversation
| if (binary[0] > 0xFFFFFFFFULL) | ||
| return std::nullopt; | ||
|
|
||
| uint32_t * out32 = reinterpret_cast<uint32_t *>(dst); |
There was a problem hiding this comment.
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.
| __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); |
There was a problem hiding this comment.
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.
| __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); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Addressed - by introducing a stack buffer with enough headroom. This is not present in Firedancer code.
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 96.13% (298/310, 0 noise lines excluded) |
|
Hi there, |
|
Should be ok, we compile with clang 21+ |
|
Note that this optimization introduced bugs: #104394 |
`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`.
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):
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 withbase58Decode('...', 32)or alike).Documentation entry for user-facing changes
Version info
26.4.1.234