Skip to main content

Module observation

Module observation 

Source
Expand description

Thread-local counters that make an invisible optimization testable.

Several optimizations change no output at all: reusing one tree_sitter::Parser per thread (#1118), skipping a space-kind lookup on a node that opens no space (#1110), serializing Ops through a borrowed projection rather than an owned clone (#1110), deferring the modeline scan behind a resolving extension (#1111). Every assertion on the result of those paths holds just as well once the optimization is reverted, so a revert is silent unless something counts the work.

§The invariant

The counter and the function that bumps it are unconditional; only the accessor is gated. Gating the counter itself would leave the test observing a build production never ships — the counted branch compiled under cfg(test) and the shipped one under nothing — which is the exact failure these counters exist to catch. Four sites grew that rule independently and each stated it in prose; counter! makes it structural instead, emitting the three items together so the wrong one cannot be gated.

The cost is one Cell increment on a path that already does far more (building a parser, classifying a node, projecting a whole tree), which is why it is affordable to leave in the shipped build.

§Two crates, one macro

big-code-analysis declares counters of its own (in its ops, wire and spaces walks) with the same macro, which is why it is #[macro_export]ed. The accessor’s gate is a parameter because the two crates need different ones: a counter declared here must also be readable by the root crate’s tests, which see this crate as an ordinary dependency (no cfg(test)), so it opens under the test-support feature as well.