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.

ShapeWhat it holdsDetections at 1000 events
no_windowsDistinct calls that all make progress, so no stall window opens and the run costs the reader and the writer alone0
long_distinctOne stall window of distinct calls, three tools in turn0
short_windowsWindows of eight calls closed by a progress call, each holding a run of three identical calls and four varied reads222
long_identicalOne call repeated through one stall window1
near_cyclesA cycle of four calls whose last call changes on every turn, so no cycle repeats twice in a row0
fixation_heavySixteen tools taking turns in blocks of six varied calls in one stall window, with one available tool unused16
loop_runsRuns of three identical calls back to back in one stall window, each run a looping catch333
share_edgeA fixation share held at exactly three quarters in one stall window, built so that no span qualifies2

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.

ShapeEventsDetectionsSeconds beforeSeconds afterPeak RSS MiB beforePeak RSS MiB after
no_windows100000.00770.00712.82.8
no_windows1000000.03990.03718.48.9
no_windows10000000.33840.329762.362.3
no_windows100000003.86164.4899598.3598.3
long_distinct100000.00800.00702.52.5
long_distinct1000000.03820.03768.99.1
long_distinct10000000.71610.854768.972.6
long_distinct100000005.64756.1049599.3599.2
short_windows10002220.00830.00963.02.9
short_windows1000022220.05440.051210.811.6
short_windows100000222220.87090.923681.080.4
short_windows100000022222212.24168.5524684.8569.1
long_identical100010.00670.00702.72.7
long_identical1000010.03720.03489.59.3
long_identical10000010.45010.352662.370.6
long_identical100000014.05184.0180597.4597.5
near_cycles100000.00670.00692.72.6
near_cycles1000000.04910.03839.29.2
near_cycles10000000.37070.387267.863.5
near_cycles100000005.59963.0548595.7595.7
fixation_heavy1000160.00760.00692.92.7
fixation_heavy10000160.03690.03709.57.7
fixation_heavy100000160.29280.315964.265.2
fixation_heavy1000000163.26152.8591633.5633.6
loop_runs10003330.00620.00632.62.6
loop_runs1000033330.06630.039212.811.6
loop_runs100000333333.36150.3563130.5100.6
loop_runs1000000333333over 120, stopped3.2618not read804.2
share_edge100020.00450.00442.52.5
share_edge1000020.03030.02368.07.3
share_edge10000020.73170.223067.462.1
share_edge1000000254.19212.8577597.2597.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.

StageBoundReason
ReaderLinear in the bytes of the fileThe 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 windowsO(n)One pass over the events
Looping searchO(W) per window, at most 10 W counted unitsThe 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 searchO(W log W + W A log W) per window, at most W (5 + A + the bit length of W) counted units beside the sortFrom 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 detectorO(n log n + n A log n), at most n (15 + A + the bit length of n) counted units beside the sortsThe sum of the two searches over every window
Final sortO(d log d)One sort of the detections by span start
WriterLinear in the bytes printedEach fixation detection names the available tools it left unused, so the output can hold d times A names
SandboxA fixed boundA 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.

TraceCounted workUnits nowBoundUnits before this change
loop_runs at 200000 eventsThe looping search46666820000006667033335
share_edge at 200000 eventsThe fixation search24844225000000626503759
loop_runs at 200000 eventsThe whole detector467330272000006668954939
share_edge at 200000 eventsThe whole detector35094187000000627828753

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.

OptionDefaultMeaning
--bin PATHtarget/release/westeringA binary to measure. Repeat it to measure several binaries in turn
--sizes LIST1000,10000,100000,1000000Event counts, comma separated
--shapes LISTevery shapeShape names, comma separated
--runs N3Timed runs per binary and trace, the best of which is kept
--cap SECONDS120The wall time after which a run is stopped
--dir PATHtarget/perfWhere the traces and outputs are written
--keepoffKeep 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.