Skip to content
This repository was archived by the owner on Jun 5, 2026. It is now read-only.
This repository was archived by the owner on Jun 5, 2026. It is now read-only.

collector: Bucket::add continues iterating after finding a match (no early exit) #20

Description

⚠️ AI-generated issue — requires human investigation before acting on it.

Summary

Bucket::add in src/collector.rs uses for_each to scan all existing entries for a matching key, but does not short-circuit after finding a match. Since bucket entries are unique, at most one match can exist, so all iterations after the match are wasted work. This runs in the hot signal-handler path on every sample.

Location

src/collector.rs, lines 50–55:

self.entries[0..self.length].iter_mut().for_each(|ele| {
    if ele.item == key {
        ele.count += count;
        done = true;     // ← sets flag but loop continues
    }
});

Impact

With BUCKETS_ASSOCIATIVITY = 4, the maximum wasted iterations is 3 per sample in the hit case. This is minor in isolation but accumulates at high sampling frequencies (e.g. 999 Hz in the test suite) and could be avoided at zero cost.

Expected behaviour

Use a find/position-style loop with an early exit, e.g.:

if let Some(ele) = self.entries[0..self.length].iter_mut().find(|e| e.item == key) {
    ele.count += count;
    return None;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions