Skip to content

3.x.x: render the shared part of mining.notify once per published work (#861) - #864

Merged
kiwidream merged 1 commit into
3.x.xfrom
orca/861-notify-render-once
Nov 13, 2026
Merged

kiwidream merged 1 commit into
3.x.xfrom
orca/861-notify-render-once

Conversation

@kiwidream

@kiwidream kiwidream commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Problem

For each new job, each session built its own serde_json::Value for mining.notify (Job::notify() in codec.rs) and write_json serialised it: coinb1, coinb2 and the hex merkle branch were copied into the value and copied again into the bytes, per session per job. coinb2 carries the payout outputs, so at the 2,600-address shape that is a 225 KB message rebuilt 2,000 times per tip, on the runtime threads that also turn share appends around.

Premise, checked against the code

Within one publication only the job ID and clean_jobs differ between sessions' notifies:

  • announce_job announces only jobs from prepare_job → Coordinator::build_job → materialize_wire, which hands every session prepared.base_wire.clone() (built once by shared_base_wire, with a placeholder extranonce1 that coinb1/coinb2 exclude), then reassign, which changes the job ID, extranonce1 and share target only. Jobs restored by resume_job are never announced.
  • announce_job sets clean_jobs per session. The version mask (mining.set_version_mask) and the share difficulty (mining.set_difficulty) travel in their own messages; extranonce1 in the subscribe answer.
  • One exception: before the first share (an empty window, no base_wire), each worker's coinbase is its own. Those sessions render their own body, as before.

Change

codec::NotifyBodies (kept in StratumConfig::notify_bodies, shared by both listeners) renders the shared part of the params array once, ,prevhash,coinb1,coinb2,[branch],version,nbits,ntime,, with serde_json itself, and each session's line is the constant head, its job ID serialised by serde_json, that body, true/false, ]} and the newline, written with one write_all under the same send timeout as before.

  • Same bytes. id < method < params is both the BTreeMap order and the insertion order (preserve_order is enabled nowhere), the strings go through the same serde_json escaping, and there are no numbers (arbitrary_precision does not apply). Job::notify() is unchanged and is the tests' reference.
  • A body belongs to its work. A kept body is reused only for a job whose coinb1 and coinb2 are the very same allocations (the cache holds them, so their addresses cannot be reused while it keeps the body) and whose prevhash, merkle branch, version, bits and time are equal: every field the body holds.
  • Bounded. The eight most recently used bodies. The cache keeps up to eight superseded works' coinbase halves (coinb1/coinb2, the allocations it compares by address) and their rendered bodies alive, about 3.6 MB at the 2,600-output shape, until eight newer works evict them; it is not cleared when idle, so the resident floor after a landing (landing_malloc_trim_resident_bytes) can be up to that much higher. Sessions that miss together at the start of a fan-out may each render it (at most one per runtime worker); the first kept is used.

Measurement (2,000 sessions, single-threaded, tests/perf/notify_render_fan_out.md)

Optimised (opt-level=3 for this crate, serde_json and hex; AMD Ryzen 9 9955HX, shared host):

coinbase message before: fastest / median after: fastest / median before: allocations, bytes after: allocations, bytes
130 outputs 12,405 bytes 9.8 / 9.8 ms 0.2 / 0.2 ms 94,000, 100.5 MB 2,025, 24.9 MB
2,600 outputs 224,829 bytes 104.4 / 104.6 ms 3.3 / 3.3 ms 94,000, 1,799.9 MB 2,025, 450.6 MB

Unoptimised (CI's test build): 313 → 1.8 ms and 5,157 → 7.6 ms. The remaining cost is one copy of the body into each session's line, kept so the line is still one write.

Load runs (run-load, two samples on b7adeaf) next to the scheduled nightlies

All presets passed their gates in both samples. Tip delivery is tip-delivery-2000-miners-400k-2fe-retarget (8 external tips, all in warm-up). The two nightlies are the ones whose artifacts are still available. Controls first: each frontend's initial-refresh snapshot_ms (VM speed over the same seeded window, #842), pg_test_fsync fdatasync (one 8 kB write), and client share-ack in the tip-free phases.

run commit initial snapshot_ms fe0 / fe1 fdatasync share-ack p50 steady / reconnect tip-to-last-notify median / slowest refresh median / slowest fan-out after the refresh, median / slowest client share-ack p99 warm-up / steady
nightly 42994888106 (11-11) 47ad4e1 7,250 / 6,519 75.0k ops/s 1.03 / 0.80 ms 571 / 632 ms 440 / 483 ms 129 / 174 ms 2.53 / 1.87 ms
nightly 43194821373 (11-12) 3a23df9 8,084 / 8,517 69.0k ops/s 0.68 / 0.84 ms 675 / 704 ms 486 / 552 ms 160 / 194 ms 2.39 / 1.36 ms
this PR, sample 1 (43334915761) b7adeaf 7,224 / 7,363 50.4k ops/s 1.09 / 1.21 ms 883 / 1,312 ms 609 / 1,136 ms 216 / 291 ms 8.36 / 1.92 ms
this PR, sample 2 (43337976535) b7adeaf 8,325 / 7,674 65.0k ops/s 1.00 / 1.35 ms 614 / 934 ms 442 / 670 ms 161 / 305 ms 5.19 / 1.72 ms

"Fan-out after the refresh" is each tip's per-frontend last-notify latency minus that frontend's refresh_ms for the tip (its refresh published trigger="tip" log line, which starts at the tip's mint): job build, issued-job persistence and the set_difficulty/notify writes, 16 values per run.

Per tip (fan-out after the refresh, fe0 / fe1, ms; refresh in brackets):

tip nightly 11-11 nightly 11-12 PR sample 1 PR sample 2
h101 155 / 135 (444, 483) 152 / 178 (552, 480) 202 / 205 (506, 470) 305 / 305 (618, 629)
h102 129 / 115 (405, 456) 181 / 155 (478, 524) 223 / 222 (486, 474) 260 / 239 (605, 645)
h103 129 / 118 (440, 463) 192 / 177 (496, 526) 222 / 210 (528, 560) 220 / 265 (670, 587)
h104 130 / 128 (425, 426) 159 / 120 (464, 551) 196 / 226 (543, 481) 157 / 166 (456, 429)
h105 115 / 122 (438, 411) 162 / 149 (461, 483) 246 / 291 (750, 658) 150 / 166 (465, 424)
h106 112 / 136 (440, 394) 154 / 140 (450, 489) 198 / 243 (1,000, 833) 125 / 114 (348, 372)
h107 131 / 124 (432, 447) 173 / 194 (508, 464) 176 / 233 (1,136, 894) 111 / 116 (379, 345)
h108 166 / 174 (466, 450) 149 / 179 (505, 457) 166 / 210 (1,048, 872) 129 / 126 (365, 374)

Reading it:

  • Sample 2 ran on a VM about as fast as the nightlies' by database speed. fsync was 65.0k ops/s against 69.0k and 75.0k, and the initial snapshot matched 11-12's. Share-ack p50 was 1.00 / 1.35 ms in the tip-free phases, against 0.68–1.03 / 0.80–0.84.
  • On that VM the post-refresh fan-out is inside the nightlies' range: a 161 ms median against 129 and 160 ms.
  • Tips 1–3 of sample 2 are the exception: 220–305 ms, above every nightly tip. The refresh before them was also the slowest of any run's first tips (587–670 ms against 405–552), so both halves slowed together early in that run. Its last five tips, 111–166 ms, are as fast as or faster than either nightly's.
  • Sample 1 ran on a slower-disk VM: fsync 50.4k, share-ack p50 about 1.5× the nightlies'. Its fan-out (216 ms) and, from tip 5, its refresh (up to 1,136 ms) were slow together; the refresh is a database read that this change does not touch.
  • The load runs are inconclusive at this sample size. Two samples against two nightlies on different VMs can neither show nor exclude an effect of about 50 ms in either direction, and the slowest post-refresh fan-out in both PR samples (291 and 305 ms) is above both nightlies' (174 and 194 ms).
  • The conclusion rests on the mechanism, not on these runs. Per session the change does strictly less work (one scan of at most eight kept bodies under the lock and one copy of the body, instead of building a Value tree and serialising it) and adds no await and no I/O. The per-session CPU it removes (about 5–50 µs per session by the measurement above) is smaller than the spread between runs of this preset, so no gain is measurable here either.

Tests

New, crates/qbit-prism-server/tests/notify_render.rs:

  • every_session_gets_exactly_the_bytes_it_serialised_for_itself: byte-for-byte against notify() serialised, over 1, 130 and 2,600 recipients, no merkle branch and a 12-level one, both clean_jobs values, job IDs at their limit (128-byte PRISM_INSTANCE_ID plus a UUID, 161 bytes) and at 256 bytes, and instance IDs with quotes, backslashes, slashes, multi-byte characters and U+2028.
  • a_body_belongs_to_the_work_it_was_rendered_from: a second session of a work reuses the body (exactly one allocation, its line; mutation-checked), while equal coinbase text in another allocation and a change to each field the body holds, one at a time, each render their own, with the right bytes; eviction past eight works.
  • fan_out_cost_before_and_after (#[ignore]d): the measurement above.

Run locally (through the host gate, CARGO_PROFILE_DEV_DEBUG=line-tables-only):

  • cargo test -p qbit-prism-server --test notify_render: 2 passed, 1 ignored (the measurement, run separately with -- --ignored --nocapture as above). Mutation check: with renders() always false, a_body_belongs_to_the_work_it_was_rendered_from fails.
  • cargo test -p qbit-prism-server --test stratum_protocol: 50 passed, 1 ignored (manual inspection).
  • cargo test -p qbit-prism-server --test stratum_properties: 8 passed.
  • PRISM_TEST_PG_BIN_DIR=/usr/lib/postgresql/16/bin cargo test -p qbit-prism-server --lib -- stratum:: codec:: --test-threads=4 (private PostgreSQL 16 clusters): 31 passed.
  • cargo fmt --all -- --check, cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings: clean (apart from the three known vendored sqlx-core warnings).
  • python3 scripts/check_prism_pool_acquires.py: pass (no database site added).

What CI must cover

The full lib suite and the other Stratum binaries (stratum_rebuild_lane, vardiff_convergence, landing_metrics, the fuzz build), which all read notifies off the wire.

Refs #861

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T19:42:40.145408Z 8ce399f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@qbit-reviewer qbit-reviewer 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.

Approved: a complete review with no bugs found.

No findings.

Reviewed by Claude Opus 5.5.

@kiwidream kiwidream added run-load Run the PRISM load nightly preset set on this PR's merge result (#549) and removed run-load Run the PRISM load nightly preset set on this PR's merge result (#549) labels Nov 13, 2026

@qbit-reviewer qbit-reviewer 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.

Approved: a complete review with no bugs found.

No findings.

Reviewed by Claude Opus 5.5.

@qbit-reviewer qbit-reviewer 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.

Approved: a complete review with no bugs found.

No findings.

Reviewed by Claude Opus 5.5.

#861)

## Problem

For each new job, each session built its own `serde_json::Value` for `mining.notify` (`Job::notify()` in `codec.rs`) and `write_json` serialised it: `coinb1`, `coinb2` and the hex merkle branch were copied into the value and copied again into the bytes, per session per job. `coinb2` carries the payout outputs, so at the 2,600-address shape that is a 225 KB message rebuilt 2,000 times per tip, on the runtime threads that also turn share appends around.

## Premise, checked against the code

Within one publication only the job ID and `clean_jobs` differ between sessions' notifies:
- `announce_job` announces only jobs from `prepare_job` → `Coordinator::build_job` → `materialize_wire`, which hands every session `prepared.base_wire.clone()` (built once by `shared_base_wire`, with a placeholder extranonce1 that `coinb1`/`coinb2` exclude), then `reassign`, which changes the job ID, extranonce1 and share target only. Jobs restored by `resume_job` are never announced.
- `announce_job` sets `clean_jobs` per session. The version mask (`mining.set_version_mask`) and the share difficulty (`mining.set_difficulty`) travel in their own messages; extranonce1 in the subscribe answer.
- One exception: before the first share (an empty window, no `base_wire`), each worker's coinbase is its own. Those sessions render their own body, as before.

## Change

`codec::NotifyBodies` (kept in `StratumConfig::notify_bodies`, shared by both listeners) renders the shared part of the params array once, `,prevhash,coinb1,coinb2,[branch],version,nbits,ntime,`, with serde_json itself, and each session's line is the constant head, its job ID serialised by serde_json, that body, `true`/`false`, `]}` and the newline, written with one `write_all` under the same send timeout as before.

- **Same bytes.** `id` < `method` < `params` is both the `BTreeMap` order and the insertion order (`preserve_order` is enabled nowhere), the strings go through the same serde_json escaping, and there are no numbers (`arbitrary_precision` does not apply). `Job::notify()` is unchanged and is the tests' reference.
- **A body belongs to its work.** A kept body is reused only for a job whose `coinb1` and `coinb2` are the very same allocations (the cache holds them, so their addresses cannot be reused while it keeps the body) and whose prevhash, merkle branch, version, bits and time are equal: every field the body holds.
- **Bounded.** The eight most recently used bodies. The cache keeps up to eight superseded works' coinbase halves (`coinb1`/`coinb2`, the allocations it compares by address) and their rendered bodies alive, about 3.6 MB at the 2,600-output shape, until eight newer works evict them; it is not cleared when idle, so the resident floor after a landing (`landing_malloc_trim_resident_bytes`) can be up to that much higher. Sessions that miss together at the start of a fan-out may each render it (at most one per runtime worker); the first kept is used.

## Measurement (2,000 sessions, single-threaded, `tests/perf/notify_render_fan_out.md`)

Optimised (`opt-level=3` for this crate, `serde_json` and `hex`; AMD Ryzen 9 9955HX, shared host):

| coinbase | message | before: fastest / median | after: fastest / median | before: allocations, bytes | after: allocations, bytes |
| --- | --- | --- | --- | --- | --- |
| 130 outputs | 12,405 bytes | 9.8 / 9.8 ms | 0.2 / 0.2 ms | 94,000, 100.5 MB | 2,025, 24.9 MB |
| 2,600 outputs | 224,829 bytes | 104.4 / 104.6 ms | 3.3 / 3.3 ms | 94,000, 1,799.9 MB | 2,025, 450.6 MB |

Unoptimised (CI's test build): 313 → 1.8 ms and 5,157 → 7.6 ms. The remaining cost is one copy of the body into each session's line, kept so the line is still one write.

## Load runs (`run-load`, two samples on b7adeaf) next to the scheduled nightlies

All presets passed their gates in both samples. Tip delivery is `tip-delivery-2000-miners-400k-2fe-retarget` (8 external tips, all in warm-up). The two nightlies are the ones whose artifacts are still available. Controls first: each frontend's initial-refresh `snapshot_ms` (VM speed over the same seeded window, #842), `pg_test_fsync` fdatasync (one 8 kB write), and client share-ack in the tip-free phases.

| run | commit | initial `snapshot_ms` fe0 / fe1 | fdatasync | share-ack p50 steady / reconnect | tip-to-last-notify median / slowest | refresh median / slowest | **fan-out after the refresh, median / slowest** | client share-ack p99 warm-up / steady |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| nightly 42994888106 (11-11) | 47ad4e1 | 7,250 / 6,519 | 75.0k ops/s | 1.03 / 0.80 ms | 571 / 632 ms | 440 / 483 ms | **129 / 174 ms** | 2.53 / 1.87 ms |
| nightly 43194821373 (11-12) | 3a23df9 | 8,084 / 8,517 | 69.0k ops/s | 0.68 / 0.84 ms | 675 / 704 ms | 486 / 552 ms | **160 / 194 ms** | 2.39 / 1.36 ms |
| this PR, sample 1 (43334915761) | b7adeaf | 7,224 / 7,363 | 50.4k ops/s | 1.09 / 1.21 ms | 883 / 1,312 ms | 609 / 1,136 ms | **216 / 291 ms** | 8.36 / 1.92 ms |
| this PR, sample 2 (43337976535) | b7adeaf | 8,325 / 7,674 | 65.0k ops/s | 1.00 / 1.35 ms | 614 / 934 ms | 442 / 670 ms | **161 / 305 ms** | 5.19 / 1.72 ms |

"Fan-out after the refresh" is each tip's per-frontend last-notify latency minus that frontend's `refresh_ms` for the tip (its `refresh published trigger="tip"` log line, which starts at the tip's mint): job build, issued-job persistence and the `set_difficulty`/`notify` writes, 16 values per run.

Per tip (fan-out after the refresh, fe0 / fe1, ms; refresh in brackets):

| tip | nightly 11-11 | nightly 11-12 | PR sample 1 | PR sample 2 |
| --- | --- | --- | --- | --- |
| h101 | 155 / 135 (444, 483) | 152 / 178 (552, 480) | 202 / 205 (506, 470) | 305 / 305 (618, 629) |
| h102 | 129 / 115 (405, 456) | 181 / 155 (478, 524) | 223 / 222 (486, 474) | 260 / 239 (605, 645) |
| h103 | 129 / 118 (440, 463) | 192 / 177 (496, 526) | 222 / 210 (528, 560) | 220 / 265 (670, 587) |
| h104 | 130 / 128 (425, 426) | 159 / 120 (464, 551) | 196 / 226 (543, 481) | 157 / 166 (456, 429) |
| h105 | 115 / 122 (438, 411) | 162 / 149 (461, 483) | 246 / 291 (750, 658) | 150 / 166 (465, 424) |
| h106 | 112 / 136 (440, 394) | 154 / 140 (450, 489) | 198 / 243 (1,000, 833) | 125 / 114 (348, 372) |
| h107 | 131 / 124 (432, 447) | 173 / 194 (508, 464) | 176 / 233 (1,136, 894) | 111 / 116 (379, 345) |
| h108 | 166 / 174 (466, 450) | 149 / 179 (505, 457) | 166 / 210 (1,048, 872) | 129 / 126 (365, 374) |

Reading it:
- **Sample 2 ran on a VM about as fast as the nightlies' by database speed.** fsync was 65.0k ops/s against 69.0k and 75.0k, and the initial snapshot matched 11-12's. Share-ack p50 was 1.00 / 1.35 ms in the tip-free phases, against 0.68–1.03 / 0.80–0.84.
- **On that VM the post-refresh fan-out is inside the nightlies' range: a 161 ms median against 129 and 160 ms.**
- **Tips 1–3 of sample 2 are the exception: 220–305 ms, above every nightly tip.** The refresh before them was also the slowest of any run's first tips (587–670 ms against 405–552), so both halves slowed together early in that run. Its last five tips, 111–166 ms, are as fast as or faster than either nightly's.
- **Sample 1 ran on a slower-disk VM: fsync 50.4k, share-ack p50 about 1.5× the nightlies'.** Its fan-out (216 ms) and, from tip 5, its refresh (up to 1,136 ms) were slow together; the refresh is a database read that this change does not touch.
- **The load runs are inconclusive at this sample size.** Two samples against two nightlies on different VMs can neither show nor exclude an effect of about 50 ms in either direction, and the slowest post-refresh fan-out in both PR samples (291 and 305 ms) is above both nightlies' (174 and 194 ms).
- **The conclusion rests on the mechanism, not on these runs.** Per session the change does strictly less work (one scan of at most eight kept bodies under the lock and one copy of the body, instead of building a `Value` tree and serialising it) and adds no await and no I/O. The per-session CPU it removes (about 5–50 µs per session by the measurement above) is smaller than the spread between runs of this preset, so no gain is measurable here either.

## Tests

New, `crates/qbit-prism-server/tests/notify_render.rs`:
- `every_session_gets_exactly_the_bytes_it_serialised_for_itself`: byte-for-byte against `notify()` serialised, over 1, 130 and 2,600 recipients, no merkle branch and a 12-level one, both `clean_jobs` values, job IDs at their limit (128-byte `PRISM_INSTANCE_ID` plus a UUID, 161 bytes) and at 256 bytes, and instance IDs with quotes, backslashes, slashes, multi-byte characters and U+2028.
- `a_body_belongs_to_the_work_it_was_rendered_from`: a second session of a work reuses the body (exactly one allocation, its line; mutation-checked), while equal coinbase text in another allocation and a change to each field the body holds, one at a time, each render their own, with the right bytes; eviction past eight works.
- `fan_out_cost_before_and_after` (`#[ignore]`d): the measurement above.

Run locally (through the host gate, `CARGO_PROFILE_DEV_DEBUG=line-tables-only`):
- `cargo test -p qbit-prism-server --test notify_render`: 2 passed, 1 ignored (the measurement, run separately with `-- --ignored --nocapture` as above). Mutation check: with `renders()` always false, `a_body_belongs_to_the_work_it_was_rendered_from` fails.
- `cargo test -p qbit-prism-server --test stratum_protocol`: 50 passed, 1 ignored (manual inspection).
- `cargo test -p qbit-prism-server --test stratum_properties`: 8 passed.
- `PRISM_TEST_PG_BIN_DIR=/usr/lib/postgresql/16/bin cargo test -p qbit-prism-server --lib -- stratum:: codec:: --test-threads=4` (private PostgreSQL 16 clusters): 31 passed.
- `cargo fmt --all -- --check`, `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings`: clean (apart from the three known vendored sqlx-core warnings).
- `python3 scripts/check_prism_pool_acquires.py`: pass (no database site added).

## What CI must cover

The full lib suite and the other Stratum binaries (`stratum_rebuild_lane`, `vardiff_convergence`, `landing_metrics`, the fuzz build), which all read notifies off the wire.

Refs #861

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Squashed commits:
b7adeaf 3.x.x: render the shared part of mining.notify once per published work (#861)
396e323 Merge 3.x.x (fa8ac84) into orca/861-notify-render-once: take in the PostgreSQL 16.15 service pin (#866)
b54f138 Merge 3.x.x (234ff1c) into orca/861-notify-render-once: take in the landed 3.1.2 PRs and the collate test fix (#874)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kiwidream
kiwidream force-pushed the orca/861-notify-render-once branch from b54f138 to 8ce399f Compare November 13, 2026 19:40
@kiwidream
kiwidream merged commit 7015770 into 3.x.x Nov 13, 2026
46 checks passed

@qbit-reviewer qbit-reviewer 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.

Approved: a complete review with no bugs found.

No findings.

Reviewed by Claude Opus 5.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-load Run the PRISM load nightly preset set on this PR's merge result (#549)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant