Skip to content

Remove the ThreadPool queue's cache-line over-alignment - #22

Merged
mlin merged 2 commits into
mlin:mainfrom
Wid-L-Hacker:conrad/remove-queue-overalignment
Oct 28, 2026
Merged

mlin merged 2 commits into
mlin:mainfrom
Wid-L-Hacker:conrad/remove-queue-overalignment

Conversation

@Wid-L-Hacker

Copy link
Copy Markdown
Contributor

The thread pool's lock-free queue (moodycamel::BlockingReaderWriterQueue in ThreadPool.h) is declared with 64-byte cache-line alignment. That alignment is a false-sharing optimization — the queue algorithm does not need it — but this project builds as C++11, where plain new guarantees only 16-byte alignment. Heap allocations of the objects embedding the queue, the web VFS File among them, have therefore been formally under-aligned: undefined behavior, saved in practice only by allocator luck.

This change removes the requirement instead of accommodating it. ThreadPool.h defines the queue header's own opt-out macro, MOODYCAMEL_MAYBE_ALIGN_TO_CACHELINE, empty immediately before including it — the same override upstream itself applies for older macOS deployment targets. The queue type's alignment requirement drops to the ordinary 8 bytes, so plain allocation is correct by construction. ThreadPool.h is the only inclusion of that header in sqlite_web_vfs, sqlite_zstd_vfs, or GenomicSQLite, so the single define covers every translation unit in the stack; nothing else changes.

The accepted trade-off: without the cache-line padding, the queue's contended fields can share cache lines, which may cost some performance on the web and nested fetch paths. That cost is currently unmeasured. The intent is to ship defined behavior and measure the false-sharing effect before deciding whether the alignment is worth restoring — restoring it would mean requiring -faligned-new (or a newer C++ standard) in every build that compiles this header, in all three projects.

Verification: a layout probe against the fetcv1.0.7 header shows the queue type moving from alignof 64 / sizeof 128 to alignof 8 / sizeof 80, and ThreadPoolWithEnqueueFast from 64 to 8. The sqlite_web_vfs standalone build and its full test suite pass, and sqlite_zstd_vfs built against this header passes its full suite, including the nested-VFS thread-pool tests. The change also composes cleanly with the open Catch2-pin branch and the ARM Barrier-yield branch.

@mlin mlin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

No blocking findings at 08138fe. Removing the declared over-alignment makes ordinary C++11 allocation valid for the current production include paths.

Independent checks: built the exact header without -faligned-new; confirmed 8-byte alignment for the pool and both VFS file types on x86-64; completed 20,000 ordered fast-enqueue jobs with barriers; and verified 600,000-row scans at both 16 and 64 KiB page sizes. The resulting extension imports no aligned-new/delete symbols.

Two nonblocking suggestions:

  • Add static_assert(alignof(ThreadPoolWithEnqueueFast) <= alignof(std::max_align_t), "Thread pool must support ordinary allocation"); after the class definition, with <cstddef>. The macro cannot affect a queue header already included elsewhere; this guard catches that include-order regression. Current production inclusion paths are fine.
  • Tighten the PR description: explicit internal cache-line padding remains; the change removes overall alignment and trailing size rounding. Also drop the claim that past success depended only on allocator luck: generated instructions can tolerate addresses that miss the declared 64-byte boundary.

The inline suggestion shortens the explanatory comment and records the alignment-performance question as a TODO. Its performance effect remains unmeasured.

Comment thread src/ThreadPool.h Outdated
Comment on lines +16 to +20
// Opt the readerwriterqueue out of its cache-line over-alignment: it is a false-sharing
// optimization, not a correctness requirement, and C++11 plain new cannot honor it, so
// objects embedding the queue would otherwise be under-aligned (undefined behavior).
// Defining the header's own opt-out macro empty makes ordinary allocation correct; the
// possible false-sharing cost is to be measured before restoring the alignment.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested shorter comment:

Suggested change
// Opt the readerwriterqueue out of its cache-line over-alignment: it is a false-sharing
// optimization, not a correctness requirement, and C++11 plain new cannot honor it, so
// objects embedding the queue would otherwise be under-aligned (undefined behavior).
// Defining the header's own opt-out macro empty makes ordinary allocation correct; the
// possible false-sharing cost is to be measured before restoring the alignment.
// Disable optional cache-line over-alignment so ordinary C++11 new suffices.
// TODO: Benchmark whether restoring it materially improves prefetch throughput.

@mlin
mlin merged commit 297b5e5 into mlin:main Oct 28, 2026
5 checks passed
@mlin
mlin deleted the conrad/remove-queue-overalignment branch October 28, 2026 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants