Performance
This page reports how westering detect scales with the size of a trace, from a thousand events to a million. It names the machine the numbers came from, gives the bounds behind each stage of a run, and shows how to rerun the measurement. Every timing and memory number on it was read from a run of a release build. Every count of work now is a count the unit tests pin, and the counts before this change come from one run of an instrumented copy of the old detector that the tree does not keep.
What was measured
The measurement is a Rust example at examples/perf. It writes a synthetic trace in each of eight shapes at 1000, 10000, 100000 and 1000000 events, runs the release binary over each trace three times and keeps the best wall time, runs it a fourth time under the system time command to read the peak resident memory, and prints the detection count and a checksum of the output bytes. Every trace is a pure function of its shape and its event count, and a test pins the SHA-256 digest of every shape at 1000 and 10000 events, so the same command writes the same traces on any machine.
| Shape | What it holds | Detections at 1000 events |
|---|---|---|
no_windows | Distinct calls that all make progress, so no stall window opens and the run costs the reader and the writer alone | 0 |
long_distinct | One stall window of distinct calls, three tools in turn | 0 |
short_windows | Windows of eight calls closed by a progress call, each holding a run of three identical calls and four varied reads | 222 |
long_identical | One call repeated through one stall window | 1 |
near_cycles | A cycle of four calls whose last call changes on every turn, so no cycle repeats twice in a row | 0 |
fixation_heavy | Sixteen tools taking turns in blocks of six varied calls in one stall window, with one available tool unused | 16 |
loop_runs | Runs of three identical calls back to back in one stall window, each run a looping catch | 333 |
share_edge | A fixation share held at exactly three quarters in one stall window, built so that no span qualifies | 2 |
The last two shapes are the adversarial ones. loop_runs packs as many looping matches into one window as the rules allow, and share_edge keeps the fixation share a few calls short of its threshold across the whole second half for a third of the starts in its first half.
Machine and load
The numbers come from an Apple M3 Max with 16 cores and 128 GiB of memory under macOS 14.6. The load average stood near 85 when the run began and near 28 when it ended.
The machine was shared with other work through every run, so the wall times are inflated and move from one run to the next. The two binaries took turns on every run of every trace, which spreads a load that comes and goes across both without making it equal for each. The no_windows rows show it, since that shape runs no search at all and still came out slower after the change at 1000000 events. The growth from one size to the next is the reading that counts.
Results
The before columns are the detector as it stood before this change and the after columns the detector as it stands now. Each time is the best of three runs in seconds. A run that passed the cap of 120 seconds was stopped and its memory was not read.
| Shape | Events | Detections | Seconds before | Seconds after | Peak RSS MiB before | Peak RSS MiB after |
|---|---|---|---|---|---|---|
no_windows | 1000 | 0 | 0.0077 | 0.0071 | 2.8 | 2.8 |
no_windows | 10000 | 0 | 0.0399 | 0.0371 | 8.4 | 8.9 |
no_windows | 100000 | 0 | 0.3384 | 0.3297 | 62.3 | 62.3 |
no_windows | 1000000 | 0 | 3.8616 | 4.4899 | 598.3 | 598.3 |
long_distinct | 1000 | 0 | 0.0080 | 0.0070 | 2.5 | 2.5 |
long_distinct | 10000 | 0 | 0.0382 | 0.0376 | 8.9 | 9.1 |
long_distinct | 100000 | 0 | 0.7161 | 0.8547 | 68.9 | 72.6 |
long_distinct | 1000000 | 0 | 5.6475 | 6.1049 | 599.3 | 599.2 |
short_windows | 1000 | 222 | 0.0083 | 0.0096 | 3.0 | 2.9 |
short_windows | 10000 | 2222 | 0.0544 | 0.0512 | 10.8 | 11.6 |
short_windows | 100000 | 22222 | 0.8709 | 0.9236 | 81.0 | 80.4 |
short_windows | 1000000 | 222222 | 12.2416 | 8.5524 | 684.8 | 569.1 |
long_identical | 1000 | 1 | 0.0067 | 0.0070 | 2.7 | 2.7 |
long_identical | 10000 | 1 | 0.0372 | 0.0348 | 9.5 | 9.3 |
long_identical | 100000 | 1 | 0.4501 | 0.3526 | 62.3 | 70.6 |
long_identical | 1000000 | 1 | 4.0518 | 4.0180 | 597.4 | 597.5 |
near_cycles | 1000 | 0 | 0.0067 | 0.0069 | 2.7 | 2.6 |
near_cycles | 10000 | 0 | 0.0491 | 0.0383 | 9.2 | 9.2 |
near_cycles | 100000 | 0 | 0.3707 | 0.3872 | 67.8 | 63.5 |
near_cycles | 1000000 | 0 | 5.5996 | 3.0548 | 595.7 | 595.7 |
fixation_heavy | 1000 | 16 | 0.0076 | 0.0069 | 2.9 | 2.7 |
fixation_heavy | 10000 | 16 | 0.0369 | 0.0370 | 9.5 | 7.7 |
fixation_heavy | 100000 | 16 | 0.2928 | 0.3159 | 64.2 | 65.2 |
fixation_heavy | 1000000 | 16 | 3.2615 | 2.8591 | 633.5 | 633.6 |
loop_runs | 1000 | 333 | 0.0062 | 0.0063 | 2.6 | 2.6 |
loop_runs | 10000 | 3333 | 0.0663 | 0.0392 | 12.8 | 11.6 |
loop_runs | 100000 | 33333 | 3.3615 | 0.3563 | 130.5 | 100.6 |
loop_runs | 1000000 | 333333 | over 120, stopped | 3.2618 | not read | 804.2 |
share_edge | 1000 | 2 | 0.0045 | 0.0044 | 2.5 | 2.5 |
share_edge | 10000 | 2 | 0.0303 | 0.0236 | 8.0 | 7.3 |
share_edge | 100000 | 2 | 0.7317 | 0.2230 | 67.4 | 62.1 |
share_edge | 1000000 | 2 | 54.1921 | 2.8577 | 597.2 | 597.2 |
The output checksums of the two binaries agree in all 31 rows where both finished, so the new detector prints the same bytes on every shape at every size the old one reached. On the six linear shapes the two binaries stand within the noise of the machine, one ahead at one size and the other at the next. The two adversarial shapes show the change. From 100000 events to 1000000, loop_runs went from 3.36 seconds to past the cap before the change and from 0.36 to 3.26 seconds after it, and share_edge went from 0.73 to 54.19 seconds before and from 0.22 to 2.86 seconds after. In the first probe, taken before any change under a load average near 190, the old binary passed the cap on share_edge at 1000000 events as well.
Bounds
Write n for the events in a trace, W for the events in one stall window, A for the entries in tools_available, p for the calls one tool makes in a window, and d for the detections.
| Stage | Bound | Reason |
|---|---|---|
| Reader | Linear in the bytes of the file | The JSON reader visits each byte a bounded number of times and keeps a key index for every object, so a repeated key costs one lookup, and the trace builder reads four fields per event and compares each step with the one before it |
| Stall windows | O(n) | One pass over the events |
| Looping search | O(W) per window, at most 10 W counted units | The calls of a window are gathered once and the search resumes after each match, and each offset it tries costs at most nine comparisons of a candidate cycle, a match paying for its own span |
| Fixation search | O(W log W + W A log W) per window, at most W (5 + A + the bit length of W) counted units beside the sort | From each start a binary search over the share levels of the tool's calls finds the first end that meets the share and the variety conditions, each tool's levels are sorted once in O(p log p), and the variety test looks each available tool up in a sorted map and searches its positions by bisection, which the counted units count as one unit each |
| Whole detector | O(n log n + n A log n), at most n (15 + A + the bit length of n) counted units beside the sorts | The sum of the two searches over every window |
| Final sort | O(d log d) | One sort of the detections by span start |
| Writer | Linear in the bytes printed | Each fixation detection names the available tools it left unused, so the output can hold d times A names |
| Sandbox | A fixed bound | A scripted session reruns detect over at most 200 steps |
Before this change two searches were quadratic. After every looping match the looping search gathered the calls of the rest of the window again, so a window with m matches cost m times its length, and loop_runs holds one match for every three events. From every start of a tool the fixation search walked forward in steps of the share deficit, and where the share stands at its edge that deficit stays at a few calls, so in share_edge the walk from every start in the first half crossed the whole second half.
Counted work
In the test build alone the two searches count one unit for every call they gather, every comparison of a candidate cycle, every call position they gather or level, every start they try, every probe of the binary search, and every available tool they examine. Outside the test build the counter does nothing, and no source file under src reads a clock. Unit tests in src/detect_work_tests.rs hold the counts under the bounds above and pin each count in the table below exactly, which makes the bounds deterministic pins rather than timings.
| Trace | Counted work | Units now | Bound | Units before this change |
|---|---|---|---|---|
loop_runs at 200000 events | The looping search | 466668 | 2000000 | 6667033335 |
share_edge at 200000 events | The fixation search | 2484422 | 5000000 | 626503759 |
loop_runs at 200000 events | The whole detector | 4673302 | 7200000 | 6668954939 |
share_edge at 200000 events | The whole detector | 3509418 | 7000000 | 627828753 |
The units now are the counts the unit tests pin. The units before this change come from one run of an instrumented copy of the old detector, with the same counter at the same places, that the tree does not keep, so that column is a record of that run and cannot be rerun from the tree. The old detector fails every one of these pins.
A test in tests/detect_reference.rs keeps a verbatim copy of the detector as it stood before this change and requires the same detections and the same printed bytes from both over 3600 generated traces, over six shapes of its own built on the model of the shapes above, and over the eight shapes above as the measurement writes them, at sizes up to 1000 events.
Memory and the worker stack
The binary runs its work on a worker thread with a 512 MiB stack, sized so a document nested to the reader's ceiling is refused on the tool's own terms. That stack is reserved address space and not resident memory. The kernel backs a page of the stack with memory only when the thread touches it, and the resident memory numbers above show it. Every run over 1000 events peaked between 2.5 and 3.0 MiB of resident memory, far below the 512 MiB the stack reserves, so almost none of the stack is ever resident. Resident memory grows with the trace instead. With no stall window it stood near 600 MiB at 1000000 events, about 630 bytes per event and about eight times the size of the trace file, most of it the reader's, which holds the file text, the parsed document and the trace at the same time while it builds the trace. A shape with many detections adds the detections and the printed document on top, which is why loop_runs peaks near 800 MiB at 1000000 events.
Rerun the measurement
Build the release binary and run the example from the repository root.
cargo build --release
cargo run --release --example perf -- measure
The measure command takes these options.
| Option | Default | Meaning |
|---|---|---|
--bin PATH | target/release/westering | A binary to measure. Repeat it to measure several binaries in turn |
--sizes LIST | 1000,10000,100000,1000000 | Event counts, comma separated |
--shapes LIST | every shape | Shape names, comma separated |
--runs N | 3 | Timed runs per binary and trace, the best of which is kept |
--cap SECONDS | 120 | The wall time after which a run is stopped |
--dir PATH | target/perf | Where the traces and outputs are written |
--keep | off | Keep the traces and outputs instead of removing each after its row |
To compare the detector before and after a change, build the earlier commit, set its binary aside, build the current tree, and pass both. The two binaries take turns on every run, and the output checksum column prints one checksum when the outputs agree and the word differs with every checksum when they do not.
git checkout <earlier-commit>
cargo build --release
cp target/release/westering target/westering-before
git checkout -
cargo build --release
cargo run --release --example perf -- measure --bin target/westering-before --bin target/release/westering
cargo run --release --example perf -- generate share_edge 100000 trace.json writes one trace without measuring it.