Skip to content

perf(kernel-routes): decode artifact pages with base64-simd - #206

Merged
ahrav merged 5 commits into
mainfrom
perf/kr-09-base64-simd
Sep 4, 2026
Merged

perf(kernel-routes): decode artifact pages with base64-simd#206
ahrav merged 5 commits into
mainfrom
perf/kr-09-base64-simd

Conversation

@ahrav

@ahrav ahrav commented Sep 4, 2026

Copy link
Copy Markdown
Owner

The scalar base64 decoder ran at 1.6 GB/s and was 40% of a full 16 MiB page ingest; SHA-256 (SHA-NI) was the rest. base64-simd dispatches at runtime to AVX2 or NEON with a scalar fallback (no target-cpu=native dependency) and decodes the standard alphabet about 5× faster. A one-off local differential over 200k mutated encodings found no input where base64_simd::STANDARD and base64 0.22 STANDARD differ in acceptance or output; tests/base64_differential.rs commits that check as three proptest properties (5,632 cases per run, ~4 s) so CI re-verifies it on every base64-simd bump. base64 stays as a dependency for encoding in tests. New crates: base64-simd 0.8, vsimd, outref.

Measured (marginal): page/16MiB 18.3 → 11.3 ms, page/256k 310 → 213 µs, page/4k 38 → 37 µs. Cumulative 1.62× / 1.46× / 1.04×; 3 improved, 0 regressed.

Stack (9/13): base perf/kr-08-alignment-reachable-load → head perf/kr-09-base64-simd.

Method. Numbers come from crates/mc-module/benches/kernel_routes.rs (added in the first PR of this stack): a baseline binary and a candidate binary run as separate processes in 10 ABBA blocks pinned to one core; each cell is median_baseline / median_candidate with a paired bootstrap 95% CI over the blocks. Baseline is the suite commit on stack/kernel-routes-03-routes before any optimization; ratios are therefore cumulative through this PR, and the marginal effect of this PR alone is called out. A/A control on the same host: all CIs include 1.0, half-widths about 1%. Host: AMD EPYC 9R14, SQLite 3.51.3 (bundled).

Guard. cargo test -p mc-module --features test-support --test kernel_routes (46), cargo test -p mc-module --test base64_differential (3), cargo test -p mc-kernel --features test-support (all suites), cargo clippy -p mc-module -p mc-kernel --all-targets -- -D warnings. tests/kernel_routes.rs gains one test that drives malformed base64 through ingest.page on a live upload; tests/base64_differential.rs is new; both are now run by CI. The wire contract, receipts, and serving semantics are unchanged.

…iB page spends its time hashing, not decoding

The scalar decoder ran at 1.6 GB/s and took 40% of a full-page ingest;
base64-simd dispatches to AVX2 or NEON at runtime with a scalar fallback
and decodes the same standard alphabet five times faster.
@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 108 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Essentials

Run ID: 3b487c0f-0a91-4d1a-96a3-863ad9a8099f

📥 Commits

Reviewing files that changed from the base of the PR and between c5bc1fa and f82d0a8.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • .github/workflows/ci.yml
  • crates/mc-module/Cargo.toml
  • crates/mc-module/src/kernel_routes/ingest.rs
  • crates/mc-module/tests/base64_differential.rs
  • crates/mc-module/tests/kernel_routes.rs

Comment @coderabbitai help to get the list of available commands.

let decoded = base64::engine::general_purpose::STANDARD
.decode(parsed.bytes_base64.as_bytes())
let decoded = base64_simd::STANDARD
.decode_to_vec(parsed.bytes_base64.as_bytes())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correctness of this swap rests on base64_simd::STANDARD rejecting/accepting exactly what base64 0.22's STANDARD engine does (same alphabet, same canonical-padding strictness, no forgiving/whitespace-tolerant mode). The PR description says this was checked with an offline differential fuzz run over 200k mutated encodings, but that isn't checked into the repo, and the only in-suite test that sends malformed bytes_base64 (tests/kernel_routes.rs:3373-3379, "not base64 at all!") targets an upload id that was never begin'd, so accepts_page short-circuits with upload_not_found before this line is ever reached — the decode-rejection branch below ("is not standard base64") has no automated coverage today.

Worth adding a small test that begins an upload and then feeds genuinely malformed base64 (bad alphabet char, truncated/extra padding, non-zero padding bits) through ingest.page, so a future base64-simd upgrade or config change that diverges from base64's acceptance rules gets caught in CI rather than relying on a one-off validation run.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Confirmed and fixed in bc4b3af. The "not base64 at all!" case at tests/kernel_routes.rs:3373 never reached the decoder: accepts_page returns upload_not_found first, so the "is not standard base64" branch had no coverage.

Added a_begun_upload_refuses_a_page_that_is_not_canonical_standard_base64, which begins an upload and sends ten malformed shapes through ingest.page: a byte outside the alphabet, the URL-safe alphabet (-/_), dropped / extra / misplaced padding (QUJDRA, QUJDRA===, QUJDR===, QUJDRA=A), non-zero trailing bits (QUJDRB==), embedded and trailing whitespace, and two frames concatenated. Each must come back invalid_params with the decode message, and each is also asserted rejected by base64 0.22's STANDARD engine, so the test pins base64-simd to the scalar engine's acceptance rule. It then completes the upload with a payload whose encoding contains + and /, checking the standard alphabet on the accept side, and asserts the staging budget returns to zero so each refusal released its decode reservation.

@claude

claude Bot commented Sep 4, 2026

Copy link
Copy Markdown

Review

Small, well-scoped perf change: swaps the scalar base64 STANDARD decoder for base64-simd's runtime-dispatched SIMD decoder on the artifact-page ingest hot path, keeping base64 around for test-only encoding. Diff is tight (Cargo.toml/Cargo.lock additions + a 3-line change in ingest.rs), and the wire contract / error branches are unchanged.

Code quality / correctness

  • The swap is a clean drop-in: same call shape (.decode_to_vec(bytes) -> Result<Vec<u8>, _>), same downstream error handling (Ok(Err(_)) still maps to the existing invalid_params_error), no new unsafe surfaced at this crate's boundary.
  • base64::Engine as _ was correctly relocated to #[cfg(test)] only, since base64 is now used purely for encoding in tests — no dead import left behind, and base64 isn't fully unused so no future unused_crate_dependencies churn.
  • Left one inline note: the correctness argument (base64_simd::STANDARD accepts/rejects exactly what base64 0.22 STANDARD does) is currently backed only by an offline differential fuzz run mentioned in the PR description, not by anything in the repo, and the one existing test that sends malformed bytes_base64 never actually reaches the new decode call (it fails earlier on upload_not_found). Suggest adding a small test that exercises the decode-rejection branch directly.

Security

  • No change to validation ordering (digest/size checks still happen before decode), no change to bounds/reservation accounting (decode_bytes estimate untouched). Pulls in base64-simd/vsimd/outref, which carry their own unsafe SIMD intrinsics for AVX2/NEON dispatch — reasonable tradeoff for a well-known, narrowly-scoped crate family, and mitigated by the differential testing described, but worth being aware this is new unsafe code (in a dependency) on a path that decodes untrusted network input.

Performance

  • Matches the intent of the PR — runtime dispatch means no target-cpu=native requirement, and a scalar fallback keeps behavior portable. Measured numbers in the description look credible (paired bootstrap CI methodology, A/A control called out).

No blocking issues found; the one suggestion above is a test-coverage improvement, not a bug.

@ahrav
ahrav marked this pull request as ready for review September 4, 2026 20:34
…ive upload

The only test that sent malformed bytes_base64 targeted an upload that was
never begun, so accepts_page refused it before the decoder ran. The new test
begins an upload and sends ten malformed shapes: a byte outside the alphabet,
URL-safe alphabet, dropped/extra/misplaced padding, non-zero trailing bits,
embedded and trailing whitespace, and two frames run together. Each must be
refused as invalid_params and also rejected by the base64 crate's STANDARD
engine, which pins base64-simd to the same acceptance rule. The upload then
completes with a payload whose encoding uses '+' and '/', so the standard
alphabet is checked on the accepting side too.
@claude

claude Bot commented Sep 4, 2026

Copy link
Copy Markdown

Review: perf(kernel-routes): decode artifact pages with base64-simd

Solid, well-scoped swap. Went through the decode path in ingest.rs, the reservation/budget math, error handling, and the new regression test.

What looks good:

  • Single call site (ingest.rs:715-716) — no risk of a second decoder diverging elsewhere in the crate.
  • Error handling is decoder-agnostic (Ok((Err(_), _)) => invalid_params), so swapping the error type from base64::DecodeError to base64_simd's error is safe.
  • The pre-decode reservation math (decode_bytes = encoded_bytes + encoded_bytes.div_ceil(4) * 3) is unaffected — it's a worst-case estimate independent of which decoder runs.
  • Panics inside the decode closure are already caught by blocking() (kernel_routes/mod.rs:406) and turned into an unavailable outcome rather than taking the process down, which covers the (unlikely) risk of a bug in the SIMD/unsafe path on unusual input.
  • The new test (a_begun_upload_refuses_a_page_that_is_not_canonical_standard_base64) is genuinely good: it self-verifies each malformed case against the real base64 STANDARD engine at test time (not just a hardcoded expectation), covers 10 distinct malformation classes, and checks that the decode reservation is released on every rejection.
  • Import cleanup (use base64::Engine as _; moved into #[cfg(test)]) is correct — no other call site outside the test module needs the trait.

One suggestion (non-blocking): see inline comment on Cargo.toml about the equivalence claim in the new dependency comment.

Didn't execute the guard commands myself in this environment (sandboxed, no cargo/network access), so I'm relying on static review — the described guard (cargo test -p mc-module --features test-support --test kernel_routes, full mc-kernel suite, clippy -D warnings) looks like the right coverage for this change.

Comment on lines +51 to +54
# Page decoding: SIMD with runtime dispatch and a scalar fallback; decodes
# the same inputs to the same bytes as `base64`'s STANDARD engine and rejects
# the same malformed ones.
base64-simd = "0.8"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The comment states this crate "decodes the same inputs to the same bytes as base64's STANDARD engine and rejects the same malformed ones" — a strong equivalence claim. The only thing enforcing that in-repo is the 10 fixed cases in the new kernel_routes.rs test; the "differential over 200k mutated encodings" mentioned in the PR description isn't captured anywhere in the repo (no fuzz target, no proptest strategy against base64's decoder).

Since the version constraint ("0.8") allows patch updates, a future base64-simd 0.8.x release could in principle shift an edge case (padding strictness, trailing-bit checks, etc.) with nothing in CI to catch a divergence beyond the 10 hardcoded shapes. Given the repo already has proptest as a dev-dependency, consider committing the differential check as a proptest property (base64_simd::STANDARD.decode(x) == base64::engine::general_purpose::STANDARD.decode(x) for arbitrary mutated byte strings) so the equivalence this comment promises stays enforced as the pin moves. Not blocking — the current test coverage is reasonable for the size of this diff.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Agreed — the comment promised more than the ten fixed cases enforced. Added tests/base64_differential.rs in 75a9b56, following the existing *_differential.rs pattern with base64's STANDARD engine as the reference:

  • valid_encodings_decode_to_the_same_bytes (512 cases): arbitrary 0–384-byte payloads encoded with base64 decode to the payload under both engines.
  • mutated_encodings_get_the_same_verdict (4096 cases): a valid encoding with 1–4 edits (replace / insert / delete / truncate), drawing bytes from the boundary set — =, +, /, -, _, \n, \r, space, A/B/Q/R/w/x (zero vs non-zero trailing bits), 0x00, 0xff, and any::<u8>() — must get the same Ok(bytes)/Err from both.
  • arbitrary_bytes_get_the_same_verdict (1024 cases): short strings built only from that boundary set.

Runs in ~4s. A base64-simd 0.8.x that shifts padding strictness, trailing-bit checks, or alphabet now fails CI rather than the comment.

Base automatically changed from perf/kr-08-alignment-reachable-load to main September 4, 2026 21:31
… differential proptest

The Cargo.toml note promises that base64-simd accepts, decodes, and rejects
exactly what base64's STANDARD engine does. Ten fixed cases in
kernel_routes.rs were the only in-repo check, and the "0.8" pin admits patch
releases that could move an edge case.

Add differential proptests against base64 STANDARD for valid encodings,
mutated encodings, and short strings of edge bytes (padding, standard-only
and URL-safe alphabet positions, whitespace, non-zero trailing bits).
}

proptest! {
#![proptest_config(ProptestConfig::with_cases(4096))]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PR description says the differential check covered "200k mutated encodings," but the case counts committed here sum to 512 + 4096 + 1024 = 5,632 (no PROPTEST_CASES override found in the repo). Either the description is stale from a larger local run, or the committed config is lower than what was actually validated — worth reconciling so the safety claim backing this decoder swap matches what CI will actually execute.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The 200k figure was a one-off local run before the test was committed; the committed config is intentionally smaller so the target stays a ~4 s CI check. Reconciled by editing the PR description: it now says the 200k run was local and that tests/base64_differential.rs re-verifies the same property at 5,632 cases per run in CI. The "Guard" paragraph was also stale ("no file under tests/ changed") and now lists both test files.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 75a9b561a3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +107 to +110
proptest! {
#![proptest_config(ProptestConfig::with_cases(4096))]
#[test]
fn mutated_encodings_get_the_same_verdict(text in mutated_encoding()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Run the new differential target in CI

The new compatibility guard is not executed by the inspected mc-host-lifecycle workflow: .github/workflows/ci.yml runs only --lib and an explicit list of integration-test targets at lines 271-289, none of which includes base64_differential. Consequently, a future allowed base64-simd 0.8 update can change decoding semantics without this test detecting it; add this target to the CI invocation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Confirmed — neither base64_differential nor kernel_routes was in the enumerated targets. Fixed in 13f628c: base64_differential is added to the "Module differential contracts" step, and a new "Kernel route contracts" step runs cargo nextest run -p mc-module --test kernel_routes (the self dev-dependency already enables test-support, so no feature flag is needed). Both suites pass under nextest locally (49 tests, ~40 s).

@claude

claude Bot commented Sep 4, 2026

Copy link
Copy Markdown

Review summary

Solid, low-risk change: a single call-site swap from base64::engine::general_purpose::STANDARD to base64_simd::STANDARD, backed by a purpose-built differential proptest suite that checks both acceptance and byte-for-byte output equality against the reference decoder, plus a targeted kernel_routes regression test for non-canonical inputs (bad padding, non-zero trailing bits, URL-safe alphabet, embedded/trailing whitespace, concatenated blocks). The wire-facing error message and reservation/budget lifecycle are untouched. New dependency surface (base64-simd, vsimd, outref) is small and the crates are well-established.

Two things worth reconciling before merge:

  1. Description vs. committed test config — the PR body claims the differential fuzz "found no input where base64_simd::STANDARD and base64 0.22 STANDARD differ" over 200k mutated encodings, but base64_differential.rs's three proptest blocks sum to 512 + 4096 + 1024 = 5,632 cases (no PROPTEST_CASES override found anywhere in the repo). Left an inline comment on the case-count line — please confirm whether the description reflects an ad hoc local run with a higher count, or whether the committed config should be bumped to match the stated coverage.

  2. CI doesn't run either test file yet.github/workflows/ci.yml enumerates mc-module integration test targets explicitly (caveman_differential, historian_truncate_differential, selection_differential, host_adapter, lifecycle_cli, plus --lib), and neither kernel_routes nor the new base64_differential appears in that list. The "Guard" commands in the PR description are real and pass locally, but nothing in main's CI currently re-runs them, so a future dependency bump of base64-simd (or an unrelated refactor) wouldn't be caught automatically. If CI wiring for the kernel_routes suite is planned for a later PR in this stack, worth calling that out explicitly so it doesn't get lost before the stack lands on main.

Neither point blocks the correctness of the decoder swap itself — the differential test methodology is sound, just not fully reflected in the numbers or wired into automated enforcement yet.

Neither tests/base64_differential.rs nor tests/kernel_routes.rs was in the
enumerated mc-module test targets, so a base64-simd bump or an ingest change
could break either without CI noticing. The self dev-dependency already turns
on test-support, so kernel_routes needs no extra feature flag here.
@ahrav

ahrav commented Sep 4, 2026

Copy link
Copy Markdown
Owner Author

Both points addressed:

  1. Description vs. committed config — the 200k run was a one-off local pass; the PR body now says so and states the committed 5,632-case config. Details on the inline thread.
  2. CI wiring — 13f628c adds base64_differential to the differential step and a new step running kernel_routes under nextest, so neither suite depends on the PR-description guard anymore.

fn encoded_payload() -> impl Strategy<Value = Vec<u8>> {
prop::collection::vec(any::<u8>(), 0..384).prop_map(|bytes| {
base64::engine::general_purpose::STANDARD
.encode(bytes)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Finding (recall-biased review, PLAUSIBLE): differential fuzz coverage stops far short of production page sizes.

encoded_payload() (and the mutation strategies built on it) only generate payloads in 0..384 bytes. But the handler this suite is meant to protect (crates/mc-module/src/kernel_routes/ingest.rs:715) decodes pages up to PAGE_BYTES_MAX = 16 * MIB, and the PR's own benchmark numbers exercise a 16 MiB page.

base64-simd's AVX2/NEON kernels process input in fixed-size SIMD chunks with a scalar remainder loop; classes of bugs in this kind of code (offset/counter handling across many chunk iterations, remainder-loop stitching only reachable past a certain chunk count) tend to surface at large sizes, not small ones. A 384-byte cap means at most a few dozen SIMD blocks are ever exercised here — nowhere near the ~350k blocks a 16 MiB page would drive through the same code.

Failure scenario: a legitimate or malformed 16 MiB artifact page hits a size-dependent decode divergence in base64_simd::STANDARD that never manifests for inputs ≤384 bytes. CI (and the local 200k-mutation run described in the PR body, which appears to use the same small-payload generator) stays green while a production-sized page is silently mis-decoded or wrongly accepted/rejected.

Suggest widening the size strategy (e.g. prop::collection::vec(any::<u8>(), 0..=PAGE_BYTES_MAX as usize) gated to a smaller case count, or at least a few fixed large-size cases near typical/max page sizes) so the differential check actually covers the sizes this decoder runs against in production.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fair — 384 bytes exercises the remainder loop but only a handful of full SIMD blocks. Fixed in f82d0a8: production_sized_pages_decode_to_the_same_bytes runs a fixed-size pass over 4 KiB, 64 KiB+1/+2/+3 (each remainder class past a block boundary), 256 KiB, 1 MiB+17, PAGE_BYTES_MAX - 1, and PAGE_BYTES_MAX (16 MiB) with pseudo-random payloads. Each size checks both engines decode to the payload, then corrupts the encoding at four positions (mid-stream !, = five bytes from the end, B in the last position, \n at one third) and asserts identical verdicts. The 16 MiB case drives ~350k blocks through the kernel; the whole test takes ~5.5 s. I kept the proptest strategies small so shrinking stays fast and put the size coverage in fixed cases instead of an 0..=16 MiB strategy.

# Artifact pages cross the JSON ring as base64; same version mc-tokenizer uses.
base64 = "0.22"
# Page decoding: SIMD with runtime dispatch and a scalar fallback; decodes
# the same inputs to the same bytes as `base64`'s STANDARD engine and rejects

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Finding (recall-biased review, PLAUSIBLE): NEON dispatch path is never exercised by CI.

The PR description and this comment both claim base64-simd "dispatches at runtime to AVX2 or NEON with a scalar fallback," and the differential suite (tests/base64_differential.rs) is the mechanism relied on to prove base64_simd::STANDARD matches base64 0.22 STANDARD on acceptance and output. But every CI job in .github/workflows/ci.yml runs on runs-on: ubuntu-latest, i.e. x86_64 only — there is no aarch64 runner anywhere in the workflow.

Failure scenario: if this service is ever built/run on an ARM host (e.g. AWS Graviton, Apple Silicon dev machines), the NEON kernel — the one code path the differential test is supposed to validate but structurally cannot on x86 CI — could diverge from the AVX2/scalar-verified behavior (accept something base64 would reject, or decode to different bytes) and ship straight to production, since nothing in this PR's guard (cargo test/cargo nextest steps, all x86-only) would ever catch it.

Worth at least flagging as a known gap, or adding an aarch64 job (even just for base64_differential) if ARM is a real deployment target.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Correct — every job in ci.yml was x86-64, and ARM is a real target (the retrieval benchmark already uses a self-hosted ARM64 runner). Fixed in f82d0a8: new base64-differential-arm64 job on the GitHub-hosted ubuntu-24.04-arm runner (free for public repos) that checks out commons the same way mc-host-lifecycle does and runs cargo nextest run -p mc-module --test base64_differential, so the NEON kernel is now checked against the reference decoder on every push.

Remaining known gap: the scalar fallback is only reached on a host without AVX2/NEON, which neither runner is, and vsimd's runtime detection can't be disabled per-test. That path is the one base64-simd upstream tests most directly, so I'm leaving it uncovered here rather than adding a no-SIMD build variant.

…rm64

The proptest strategies stop at 384-byte payloads, so at most a few dozen
SIMD blocks ran per case while the handler decodes pages up to 16 MiB. Add a
fixed-size pass over 4 KiB through PAGE_BYTES_MAX, including remainders of one
to three bytes past a 64 KiB boundary, with corruptions placed mid-stream and
in the padding tail.

Every existing CI job runs on x86-64, so the NEON kernel base64-simd selects on
aarch64 was never executed. Add an arm64 job that runs the differential suite.
@ahrav
ahrav merged commit 3645a59 into main Sep 4, 2026
18 of 20 checks passed
@ahrav
ahrav deleted the perf/kr-09-base64-simd branch September 4, 2026 21:59
Comment thread .github/workflows/ci.yml
- uses: taiki-e/install-action@nextest

- name: NEON path against the reference decoder
run: cargo nextest run -p mc-module --test base64_differential

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correctness / test-coverage gap: the scalar fallback of base64-simd is never exercised.

base64-simd = "0.8" does runtime CPU-feature dispatch with a scalar fallback (per the comment added in crates/mc-module/Cargo.toml: "SIMD with runtime dispatch and a scalar fallback"). The new differential coverage only runs on:

  • the existing x86 runner (mc-host-lifecycle, ubuntu-latest) — almost certainly AVX2-capable, so it exercises the AVX2 kernel, and
  • this new ubuntu-24.04-arm job — NEON is baseline/mandatory on AArch64, so it always exercises the NEON kernel.

Neither CI job's hardware is ever missing SIMD support, so the scalar decode path (used in production whenever the daemon runs on a CPU without AVX2 — e.g. older/budget x86 instances, some virtualization/sandboxing that masks CPU features, or any non-x86/non-ARM target) is compiled but never run against the reference base64 decoder by base64_differential.rs or the kernel_routes canonical-base64 test. A divergence that exists only in the scalar implementation (e.g. a different acceptance rule for non-canonical padding or trailing bits) would silently corrupt or reject artifact pages in production without ever failing CI.

Failure scenario: the service runs on a host whose CPU lacks AVX2 (or the dispatcher's feature probe conservatively picks scalar), base64-simd's scalar decoder accepts (or rejects) an input where base64::engine::general_purpose::STANDARD disagrees — e.g. a non-canonical padding pattern the scalar path fails to reject — and the ingest handler either stages corrupted bytes as a page or spuriously rejects a valid page, while every CI signal (including this new differential suite) stays green because it only ever runs the SIMD kernels.

Consider forcing the scalar path in at least one CI job (many SIMD-dispatch crates expose a way to disable feature detection, e.g. via an env var or a #[cfg]-gated test build) so the fallback used by real, non-AVX2/non-NEON hardware gets the same reference-decoder differential coverage as the SIMD kernels.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Confirmed, and this is the gap I flagged in the arm64 thread: ubuntu-latest has AVX2 and NEON is mandatory on AArch64, so neither runner reaches decode_fallback. The PR merged before this comment landed, so it goes to a follow-up rather than this branch.

The mechanism is concrete: vsimd's detect feature (on by default through base64-simd's default = ["std", "detect"]) is what enables runtime dispatch. Without it, vsimd::dispatch! resolves from cfg(target_feature) at compile time, and a baseline x86-64 build resolves straight to the scalar fallback. So the follow-up is: base64-simd = { version = "0.8", default-features = false, features = ["std"] }, an mc-module feature base64-detect = ["base64-simd/detect"] in default so the shipped binary keeps runtime dispatch, and one extra CI step cargo nextest run -p mc-module --no-default-features --test base64_differential that runs the scalar path against the reference decoder on x86. Tracked as magic-context-xmrb.

@kilo-code-bot

kilo-code-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (5 files)
  • .github/workflows/ci.yml
  • crates/mc-module/Cargo.toml
  • crates/mc-module/src/kernel_routes/ingest.rs
  • crates/mc-module/tests/base64_differential.rs
  • crates/mc-module/tests/kernel_routes.rs

Reviewed by gemini-3.8-flash · Input: 181.3K · Output: 41.4K · Cached: 1.4M

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.

1 participant