# Performance Playground

Three controlled C++20 experiments, with downloadable raw trials and an interactive
static presentation on [salvatorespina.com](https://salvatorespina.com/experiments/performance/).
This is a standalone, dependency-free part of Perfxplorer. It does not start the
binary-upload service or execute visitor-supplied programs.

## Run it

Requires a C++20 compiler with `std::barrier`, CMake 3.20+, threads, Python 3.10+.
GCC 13.3 on Linux was used for the first recorded run.

```sh
cmake -S playground -B playground/build -DCMAKE_BUILD_TYPE=Release
cmake --build playground/build -j2
ctest --test-dir playground/build --output-on-failure
python3 -m unittest discover -s playground -p test_collect.py
python3 playground/collect.py --binary playground/build/perf-playground --output my-results.json
```

The collector never overwrites an existing evidence file. It validates all 112
samples against expected configuration/trial pairs, independently computed checksums,
allocation counts and finite positive durations before writing JSON. Pass
`--compiler /path/to/compiler` if it is not `c++`; this reports the compiler's
version, not an introspection of an arbitrary binary. Source and binary SHA-256
hashes are included. CMake's generated `flags.make` supplies flags when available.

## Experiments

| Experiment | Configurations | Work per timed sample | Comparison |
| --- | --- | --- | --- |
| Memory locality | 4,096 / 65,536 / 1,048,576 nodes | 2,097,152 node visits | Sequential successor links / shuffled successor links |
| False sharing | 2 / 4 workers | 1,000,000 relaxed atomic increments per worker | Adjacent 8-byte counters / counters 128 bytes apart |
| Allocation reuse | 32 / 256 / 2,048 uint64 elements | 10,000 batches | New vector per batch / pre-reserved vector reused |

All variants of an experiment produce the same expected checksum for a given
configuration. The normal benchmark uses two warmup pairs followed by seven
measured pairs, alternating which variant runs first. There is no speed threshold
in tests: an optimization need not win on every environment or workload.

### Locality

Both variants use the same dependent pointer-chasing kernel on 16-byte nodes.
Each node holds an index to its successor and a value. One cycle visits all nodes,
either in address order or a fixed shuffled order (Fisher-Yates, mt19937 seed 42).
Both sum the same values with identical visit counts. Allocation, initialization
and shuffling are excluded. Both node arrays remain resident. This explores a
warm steady state; the 16 MiB largest list can fit in some last-level caches.
Shuffle uses modulo reduction; it is a deterministic traversal, not a claim of
perfectly uniform random sampling. No hardware cache-miss counts are recorded.

### False sharing

Each worker owns one counter; there is no shared logical counter. The packed
array is 128-byte aligned and its first four counters occupy 32 bytes. Padded
slots are 128 bytes each. The recorded Linux host reports 64-byte cache lines.
These choices are workload parameters, not a portable cache-line-size guarantee.
Relaxed atomics ensure real updates without a data race. Threads rendezvous before
timing; creation and join are excluded. Release/completion barrier overhead and
OS scheduling are included. Thread affinity and physical-core placement are not
controlled. Atomics' implementation is platform-dependent.

### Allocation

Both variants resize, fill with values dependent on batch index, sum, then clear
the vector. A counting allocator observes calls for this vector only. Fresh vectors
make 10,000 calls per measured sample; reuse makes zero during the sample, after
one pre-reserve outside timing. These are not process-wide allocation statistics.
The runtime cost includes vector initialization, writes, sum and deallocation for
the fresh path. Large buffers can make allocation a small part of total cost.

## Interpreting results

`results.json` is one recorded run in a shared, virtualized Linux container. It is
not a personal-machine benchmark, a portable speedup claim, a regression baseline,
or evidence of production performance. CPU quota, compiler, source hashes,
timestamps, raw samples and environment limitations accompany each run. Affinity,
frequency and power policy are uncontrolled. Other tenants can add noise.

Charts show median wall time and the observed min–max range across seven trials;
these are not confidence intervals. Ratios compare medians, not per-trial ratios.
Samples are correlated by warm state and the shared machine. Report conditions
and repeat on your target hardware before making an engineering decision.

The public memory-access grid is a deterministic 64-node illustration with a
simplified last-line reuse count. It does not run the native code or model a CPU's
full cache/prefetch system. Browser timing is deliberately not mixed with C++ data.
No flamegraphs or hardware counter values are inferred from wall time.

For a deeper investigation, profile the controlled executable locally with your
normal sampling/tracing tools. Include recording overhead and profiler permissions
when documenting those results.

## Public package

The portfolio carries a copy of the C++ source, CMake file, collector, contract tests,
README, license, and recorded JSON so visitors can inspect/reproduce the experiments
even while this repository is private. Their SHA-256 manifest is checked in site CI.
The suite's correctness checks run in Perfxplorer CI; CI timing is never published
as a controlled performance baseline. Existing profiler backend dependencies are
unaffected.
