8 Commits

Author SHA1 Message Date
Anthony Eid 2440236055 gpui: Add seeded benchmarks and heap allocation metrics (#64843)
Two additions to GPUI's benchmark harness, split out of #64842 so the
randomized element tree harness can build on them.

## Seeded `#[gpui::bench]` benchmarks

A benchmark that takes a `StdRng` parameter runs once per seed, and each
seed is its own Criterion benchmark named `<input>/seed-<n>` (or
`<function>/seed-<n>` without inputs). Seeds come from
`calculate_seeds`, moved out of `test.rs` into `seeds.rs` so it compiles
under `bench-support` and is shared with `#[gpui::test]`. `seed = N`,
`seeds(...)`, `iterations = N`, `SEED` and `ITERATIONS` therefore mean
the same in tests and benchmarks, and one `SEED` reproduces either. The
RNG is rebuilt on every Criterion routine call, so warm-up and every
sample measure the same workload. The generated code seeds it through
`gpui::private::rand`, so a bench crate needs `rand` only to name
`StdRng` in its signature.

```rust
#[gpui::bench(inputs = families(), input_name = "tree", iterations = 6)]
fn full_refresh(family: &Family, mut rng: StdRng, cx: &mut BenchAppContext) { ... }
```

The macro now also checks the benchmark's parameters, and reports a
wrong parameter count, a `&mut StdRng`, or `seed`/`seeds`/`iterations`
without a `StdRng` parameter as clear errors.

**Behavior change in `gpui::test`:** with `SEED` set and `iterations >
1`, `calculate_seeds` returned `SEED` and then `SEED..SEED+iterations`,
so `SEED=7 ITERATIONS=2` ran 7, 7, 8. Criterion rejects the duplicate
benchmark ID, and the function's comment already described
`SEED..SEED+iterations`, so it now runs 7, 8.

## Heap allocation metrics in `bench_metrics`

- `CountingAllocator<A = System>` wraps any global allocator and counts
allocations, allocated bytes and freed bytes in sharded relaxed atomics,
with no backtraces. Each thread also counts its own allocations in plain
thread-locals.
- `AllocationCounter` measures allocations or allocated bytes per
iteration over a `MetricScope`, like `HardwareCounter`. When the
allocator is installed, `allocations`, `foreground allocations` and
`allocated bytes` are default secondaries.
`BENCH_MEASUREMENT=allocations` or `foreground-allocations` makes one of
them the primary. Without the allocator they're skipped with a one-time
note, like unavailable hardware counters.
- `foreground allocations` leaves out background workers' timing noise.
It can still vary between samples when the workload does, for example
when frames coalesce or caches grow in early samples, so a CI gate on it
needs a small tolerance.
- `allocation_stats()` exposes the totals and `live_bytes()` for heap
held around setup and teardown.
- `gpui::bench_main!` installs `CountingAllocator` over the system
allocator. `gpui::bench_main!(allocator = mimalloc::MiMalloc; benches)`
wraps another allocator instead, and `allocator = none;` installs
nothing.

Example, `inspector_render` smallest case: `allocations per iteration:
median 558`, `allocated bytes per iteration: median 390 K`.

## Notes for reviewers

- The counting allocator is installed in every GPUI bench binary. It
adds a few relaxed atomic increments per allocation, so wall-time
baselines saved before this PR shift slightly.
- Placements in GPUI's element arena aren't heap allocations, so they
aren't counted once the arena is warm. Arena bytes per iteration could
be a GPUI secondary in a follow-up.

## Validation

- Throwaway seeded bench (inputs `a`/`b`, `iterations = 2`,
`seeds(40)`): correct seed order by default and with `SEED=7`, `SEED=7
ITERATIONS=2`, `ITERATIONS=3`; every routine call drew the same first
value for a seed; the allocations primary reported exactly 1 allocation
/ 64 bytes per iteration for `vec![0u8; 64]`. A second throwaway bench
imported `StdRng` through `gpui::private::rand` with no `rand`
dependency of its own and ran `probe/seed-0` and `probe/seed-1`.
- `inspector_render` ran with the default allocator and with `allocator
= none;` (which prints the note and reports no allocation lines), and
built with `allocator = std::alloc::System;`.
- New unit tests for seed order, process-wide allocation counting, and
exact calling-thread counts that exclude other threads; `bench_metrics`
and `bench_context` tests pass.
- `./script/clippy` and `cargo fmt` clean; `--no-run` builds of
`gpui_platform` and `benchmarks` benches.

Release Notes:

- [GPUI] Added seeded `#[gpui::bench]` benchmarks that take a `StdRng`,
and heap allocation counts in benchmark reports
2026-09-28 13:02:30 +00:00
Anthony Eid e683fd7b46 gpui: Add bench_metrics for multi-metric Criterion benchmarks (#64753)
GPUI benchmarks measured only wall time, which is noisy enough on shared
CI runners that a regression must be large before Criterion flags it.
This PR adds a `bench_metrics` crate that records several metrics per
Criterion sample and reports them alongside Criterion's own analysis,
and wires GPUI's benchmark harness to it. Nothing in `bench_metrics`
depends on GPUI, so any Criterion benchmark in the workspace can adopt
it.

**What a benchmark now prints** (Linux, hardware counters available):

```
closed_inspector_grid/case/100-nodes-0-cycles
                        time:   [273.99 µs 274.78 µs 276.04 µs]
GPUI bench report (all observed iterations): closed_inspector_grid/case/100-nodes-0-cycles
  instructions per iteration: median 5.791 M instructions (min 5.788, max 5.880, samples 22)
  foreground instructions per iteration: median 5.791 M instructions (...)
  cycles per iteration: median 1.453 M cycles (...)
  branch misses per iteration: median 1.856 K branch misses (...)
  cache misses per iteration: median 123.008 cache misses (...)
  foreground context switches per iteration: median 0.000 context switches (...)
  page faults per iteration: median 0.000 page faults (...)
  IPC: median 3.996 (min 3.349, max 4.063, samples 22)
  foreground share of instructions: median 1.000 (...)
```

Criterion analyzes one metric (the *primary*; wall time by default) and
the rest are *secondaries* taken over the same iterations.
`BENCH_MEASUREMENT=instructions` makes process-wide retired instructions
the primary, and `BENCH_MEASUREMENT=foreground-instructions` the
benchmark thread's alone; both are near-deterministic, so a ~0.01%
interval is typical and small regressions are detectable, with wall time
demoted to the report. `BENCH_MEASUREMENT=wall-time` disables all
counters. Counters that can't be opened (no `CAP_PERFMON`, Docker
seccomp, VMs without a PMU, Intel Macs) are skipped with one note, so
`cargo bench` never fails by default; the instruction modes fail fast so
CI can't silently measure the wrong thing.

**Sources.** `HardwareCounter` is platform-neutral over per-OS backends.
On Linux it uses `perf_event_open`: counters are opened once per process
with `inherit` so every later thread is covered, read as deltas per
sample (~150 ns per counter), with hybrid-CPU encodings parsed from
sysfs and multiplexing scaled with a one-time warning. On Apple Silicon
it reads `proc_pid_rusage` and `thread_selfcounts`, which are
unprivileged, for instructions and cycles; branch and cache events need
`kperf` and stay Linux-only. `ResourceCounter` reads `getrusage` for
voluntary context switches on the foreground thread (the waits
instructions can't see) and minor page faults, with no privileges.

Foreground-thread instructions are counted separately from the process
total, so work shifting between GPUI's foreground and background threads
is visible even when the total is flat; with a headless renderer the gap
is driver-thread submission work (about 7% of a small Metal frame).

**Design.** `BenchMeasurement` type-erases the primary so
`#[gpui::bench]` functions and `BenchAppContext` need no type parameter;
`with_secondary`/`with_ratio` add metrics; `bench_group!` still
delegates to `criterion_group!`. Plain Criterion benches opt in with a
`config =` line and `MetricReport::iter` (documented in the gpui-bench
skill; wiring `rope` etc. is a follow-up). Reports are now emitted per
input for `inputs = ...` benchmarks, since per-iteration metrics blended
across inputs are meaningless. Criterion is bumped to 0.8.2, which also
randomizes stack alignment per sample.

**Verified** on Linux (hybrid Alder Lake) and an M-series Mac: every
mode on `inspector_render` and `editor_render`; counter denial via
seccomp on Linux; `bench_metrics` and `gpui` `bench_context` tests
including thread-scope inclusion/exclusion through real counters on both
platforms.

**Follow-ups:** counting-allocator metric (peak heap, allocations),
wiring non-GPUI benches, per-secondary baseline comparison.

Release Notes:

- [GPUI] Improved benchmarks to report retired instructions, cycles,
IPC, foreground-thread share, cache and branch misses, context switches,
and page faults per iteration alongside wall time, with
`BENCH_MEASUREMENT=instructions` or `=foreground-instructions` selecting
near-deterministic instruction counts as the Criterion-analyzed metric
for CI regression detection
2026-09-27 17:37:29 +00:00
Miguel Raz Guzmán Macedo 6efd4950d6 Require clear sections in lint UI fixtures (#62659)
Add `// --- SHOULD FIRE ---- ` and `// ---- SHOULD NOT FIRE ---- ` to
the `/lint-creator` skill.

Release Notes:

- N/A
2026-08-27 05:53:18 +00:00
Miguel Raz Guzmán Macedo f595fd84cf Improve lint-creator skill (#60383)
Rewrote `.agents/skills/lint-creator/SKILL.md` to be more concrete and
concrete.

Release Notes:

- N/A
2026-08-27 05:34:16 +00:00
Anthony Eid 1f7eae7fb7 gpui: Separate benchmark support from test support (#63039)
`gpui::bench` previously required GPUI's full `test-support` feature.
Any benchmark using the supported GPUI harness therefore compiled
test-only APIs and fakes, even when the benchmark otherwise followed
production dependency paths.

This change introduces `bench-support` as GPUI's canonical benchmark
feature and keeps `bench` as a compatibility alias for existing
consumers. The benchmark feature now exposes only the existing GPUI
internals that `BenchAppContext` needs: the real threaded dispatcher,
profiler integration, and a headless platform surface. Benchmark HTTP
requests use the production `BlockedHttpClient`, so accidental network
access fails instead of silently entering a fake response path.

The current headless implementation reuses the cfg-stripped core of
`TestPlatform`. Test-only prompt state, simulation APIs, and test
executors remain unavailable under `bench-support`. Cargo feature-tree
checks confirm that both `bench-support` and the compatibility alias
remain independent of `test-support`.

This also adds a `gpui-bench` agent skill covering production-shaped
fixtures, frame and foreground responsiveness, macOS headless Metal
rendering, deterministic correctness guards, Criterion measurement,
Instruments symbolication, and the requirement that a benchmark's
complete dependency graph contain no `test-support` feature.

Two follow-ups are intentionally deferred to keep this feature-isolation
change reviewable:

- Extract the shared headless implementation into
`HeadlessPlatformCore`, with separate `BenchPlatform` and `TestPlatform`
wrappers. This will remove benchmark-related cfg branches from the
test-facing type and make their supported surfaces explicit.
- Add a shared scripted HTTP client that compiles independently under
either `http_client/test-support` or `http_client/bench-support`. Tests
and benchmarks could then serve predefined responses through the same
deterministic transport boundary without either support feature enabling
the other; the default benchmark context should remain network-blocked.

This PR only isolates GPUI itself. Migrating downstream benchmark
fixtures that explicitly request other crates' `test-support` features
remains separate work.

Testing performed:

- `cargo tree --offline --package gpui --no-default-features --features
bench-support --edges features --invert gpui`
- `cargo tree --offline --package gpui --no-default-features --features
bench --edges features --invert gpui`
- `cargo check -p gpui --no-default-features --features bench-support`
- `cargo check -p gpui --no-default-features --features bench`
- `git diff --check`

Release Notes:

- N/A
2026-08-26 09:51:50 +00:00
Miguel Raz Guzmán Macedo 4b7369481d Add dylint lint library for Zed-specific patterns (#58496)
Adds a dylint library under tooling/lints that flags Zed-specific
anti-patterns:

* shared_string_from_str_literal, 
* async_block_without_await, 
* entity_update_in_render, 
* notify_in_render, 
* owned_string_into_shared, 
* len_in_loop_condition, and 
* blocking_io_on_foreground. 

Includes UI tests, a single-lint helper, and workspace.metadata.dylint
registration so cargo dylint --all discovers it. The library pins its
own nightly toolchain (kept out of the main workspace) and tracks dylint
6.

Release Notes:

- N/A

Self-Review Checklist:

- [ ] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [ ] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [ ] Performance impact has been considered and is acceptable

Closes #ISSUE

Release Notes:

- N/A or Added/Fixed/Improved ...
2026-07-03 22:05:34 +00:00
Richard Feldman 1226e28dd3 Add zed-cherry-pick agent skill (#57833)
Adds a project-scoped agent skill at
`.agents/skills/zed-cherry-pick/SKILL.md` that walks an agent through
cherry-picking a merged PR into Zed's `preview` or `stable` release
branch — including the case where the automated `cherry_pick` GitHub
Actions workflow failed due to a merge conflict.

Captures the conventions enforced by `script/cherry-pick` (branch
naming, PR title/body format) and the gotchas around discovering the
current channel→branch mapping, non-interactive git in agent
environments, and worktree index locks.

Release Notes:

- N/A

---------

Co-authored-by: Martin Ye <martin@zed.dev>
Co-authored-by: Ben Kunkle <ben.kunkle@gmail.com>
2026-05-27 19:46:15 +00:00
Lukas Wirth 9b4737b0d7 Introduce a skill for #[gpui::test] (#57806)
Release Notes:

- N/A or Added/Fixed/Improved ...
2026-05-27 17:06:30 +00:00