mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4910] [CLOSED] [Engine] avoid per-call unordered_map allocation in PleReader::issue #5246
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5246
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4910
Author: @martifaj
Created: 11/1/2026
Status: ❌ Closed
Base:
main← Head:engine/ple-reader-issue-dedup📝 Commits (1)
10e5214[Engine] avoid per-call unordered_map allocation in PleReader::issue📊 Changes
1 file changed (+41 additions, -16 deletions)
View changed files
📝
src/ngram/ple_reader.cpp(+41 -16)📄 Description
Problem and result
Problem:
Every call to
PleReader::issue()(invoked per token/layer during direct-SSD PLE streaming) constructed a temporarystd::unordered_map<uint64_t, size_t> by_pageandstd::vector<Job> jobson the heap to deduplicate cache-missed rows that land on the same 4 KiB page, and then sortedjobsby page offset anyway (std::sort(jobs.begin(), jobs.end(), ...)). For decode and prefetch batches (n = 1orn = 16), heap-allocating and hashing intostd::unordered_mapon every call dominates the CPU time spent insideissue().Result:
PleReader::issue()now collects cache misses into a stack array (Miss stack_misses[64], falling back to a reusableImpl::miss_scratchvector whenn > 64), sorts them by(first, order), and coalesces adjacent equal-page misses in a single linear pass directly intom.queue. The queuedJobs (their page offsets, read lengths,Useordering, andstats.dedup_rows) are identical toorigin/main, reducing CPU time inissue()by 2.6x–3.2x across sparse/random batches (n = 1..256) and reducing caller-threadissuetime on a real 28.8 GBQwen3.8-Flash-Next-GSQ-RCO-IQ2_XSPLE shard from 2.9–3.2 µs/token to 1.7–2.1 µs/token on Intel Core i9-10900F + Crucial P3 1TB NVMe (CT1000P3SSD8).Approach
Missstruct holding{first, length, row, in_page, order, dst}.n <= 64(which covers single-row readsn = 1, decode/prefetchPLE_N_HEADS = 16, and 4-token chunks), store misses on the stack (Miss stack_misses[64]); forn > 64, reuseImpl::miss_scratchso steady-state calls do not reallocate.misses[0..n_miss)by(first, order)and merge contiguous runs with the samefirstpage offset directly intom.queue, reservingend - kentries inj.uses.length(PAGEor2 * PAGE), sameUseorder within each job, and identicalstats.dedup_rowscounts.Validation
14fc8a0a030c85504f850546a3df8d14c276f104(vs.origin/main46c152a1)Linux 6.17.0-41-generic #41-Ubuntu SMP PREEMPT_DYNAMIC x86_64Intel(R) Core(TM) i9-10900F CPU @ 2.80GHz(10 cores / 20 threads)60 GiB(58 GiBavailable)Crucial P3 1TB NVMe SSD(CT1000P3SSD8, firmwareP9CR313, PCIe Gen3 x48.0 GT/s,ext4on/dev/nvme0n1p5for the GGUF PLE shard;/tmpontmpfsfor synthetic stress-test tables)NVIDIA GeForce RTX 2080 Ti(11,264 MiBVRAM, driver580.159.03)Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf(28.8 GB PLE shard)g++ (Ubuntu 15.2.0-4ubuntu4) 15.2.0(-O2 -std=c++20 -Wall -Wextra -Wpedantic -Werror -pthread)ple_reader_test --gguf):mmap(20,000rows /1,250tokens): Output:--direct-onlycomparison onQwen3.8-Flash-Next-GSQ-RCO-IQ2_XS-00002-of-00002.gguf(--rows 20000,1,250tokens ×16heads,cache_rows = 0):direct(µs/token)issue(µs/token)origin/main)591.22.9170 / 1092OK (bit-identical)HEAD)247.61.7122 / 279OK (bit-identical)origin/main)247.63.2121 / 275OK (bit-identical)HEAD)247.42.1122 / 275OK (bit-identical)origin/main)248.03.1122 / 275OK (bit-identical)HEAD)246.71.9121 / 276OK (bit-identical)issuetime inple_reader_testincludes mutex lock,PleReader::issue()job construction,cv_work.notify_one(), and theeventfdfile.wake()syscall, dropping from2.9–3.2 µs/tokento1.7–2.1 µs/token.)ple_reader_test --selftest):ple_reader_stress_test), including ASan/UBSan and TSan:ple_reader stress: 7 tables x 15 configurations ... error path: a failed read fails the next tickets too, none hangs ... ple_reader stress: OK(all 105 configurations + error-path check passed bit-identical against ground truth, with zero compiler warnings and zero ASan/UBSan/TSan findings).rb = 90,table_offset = 192):issue()produces identicalqueuejobs (offset,length,ticket, andusesvector elements) anddedup_rowscounts before and after the change.issue()call (miss-collection + deduplication + sort +queuepush, all 3 runs shown):n)origin/main) — Runs 1 / 2 / 3engine/ple-reader-issue-dedup) — Runs 1 / 2 / 30..1,000,000)170.1 / 71.9 / 71.2 ns/call23.4 / 22.5 / 22.7 ns/call0..1,000,000)16(PLE_N_HEADS)1245.0 / 1245.5 / 1221.6 ns/call452.7 / 460.9 / 452.2 ns/call0..1,000,000)647737.6 / 7907.1 / 7690.9 ns/call2429.4 / 2495.8 / 2487.4 ns/call0..1,000,000)25631162.5 / 32309.2 / 31237.2 ns/call11920.7 / 12263.9 / 11999.1 ns/call0..512, ~11 pages)168.2 / 69.5 / 69.2 ns/call21.2 / 21.8 / 22.0 ns/call0..512, ~11 pages)16(PLE_N_HEADS)775.3 / 805.2 / 770.8 ns/call323.2 / 336.0 / 325.6 ns/call0..512, ~11 pages)641869.3 / 1916.5 / 1862.5 ns/call1966.9 / 2023.6 / 1959.5 ns/callstrata-engine): not built on this host because no CUDA (nvcc), ROCm/HIP, or SYCL compiler toolkit is installed in the build environment (PleReaderis pure host C++ and tested end-to-end against the real GGUF PLE shard viaple_reader_test --gguf).Limits
PleReader::issue()page-miss deduplication and sorting; eachJobstill owns itsstd::vector<Use> usesallocation andDirectFileis unchanged.n = 64rows all falling into ~11 pages), sorting all 64Missstructs before coalescing is ~5% (~100 ns) slower than hashing into an 11-entryunordered_mapand sorting 11Jobs; on real PLE n-gram tables (320M rows across 16 heads), page collisions within a call are rare (Sparse / randomrows above, 3.1x faster atn = 64).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.