mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-10-15 20:49:57 +00:00
## Problem Every `StoreTx` carried two maps for observation de-duplication: - `obsKeys`, keyed by `observerID + "|" + pathJSON` (a concatenated string allocated for every observation); - `observerSet`, for `UniqueObserverCount`. On a busy regional network's 7-day window (64k transmissions, 400k observations), these maps and their concatenated keys held about 60 MB of heap. Most transmissions are heard a handful of times; the busiest in that week was heard 40 times. The maps cost a few hundred bytes per transmission to de-duplicate lists that are usually under ten long. ## Change - `hasObservation` and `hasObserver` scan `tx.Observations`, comparing the observer ID and path strings the observations already hold. These are the same two values the old map key was built from, so the answers are identical. - Once a transmission passes 64 observations (`obsIndexThreshold`), it builds the maps. They're keyed by a struct that shares the observations' strings, so no key bytes are allocated, and busy transmissions keep O(1) checks (the case #543 / #355 addressed). - The five places that appended an observation now call `addObservation`, which keeps `UniqueObserverCount` and `ObservationCount` in step. Those three lines were previously copied at each site. - The memory estimate no longer charges for the per-transmission maps or the concatenated keys. ## Perf justification (AGENTS.md rule 0) - **Complexity.** - Below the threshold, a duplicate check is O(k) for a transmission with k < 64 observations, so building it costs at most 63·64/2 = 2016 string-header comparisons. - At and above 64 observations, checks are O(1) map lookups, as before. - Nothing is O(n²) in the number of transmissions or observations overall: the scan is bounded per transmission. - **Scale.** Real data peaks at 40 observations per transmission. The scan compares string headers (length first, then bytes), and 64-character observer IDs differ early. `BenchmarkObsDedup` (a miss, the common case on ingest): | observations on tx | check used | time | |---|---|---| | 4 | scan | 15 ns | | 16 | scan | 70 ns | | 40 | scan | 151 ns | | 63 | scan | 233 ns | | 64+ | map (struct key) | 15 ns | For comparison, the old map lookup was about 15 ns too, plus building and allocating the `observerID|path` key string on every check. At 40 observations that's about 3 µs extra to build a whole transmission, against roughly 2 KB of maps no longer allocated for it. **Measured on a copy of that 7-day window** (`GOMEMLIMIT=750MiB`, same queries): | | before | after | |---|---|---| | in-use heap after load | 612 MB | 568 MB | | HeapAlloc after load | 660 MB | 564 MB | | peak RSS | 1077 MB | 1009 MB | | startup to ready | 31 s | 21 s | It has also run on a staging instance (live MQTT ingest) for about nine hours alongside other store changes, with no change in observation or observer counts. ## Tests - `obs_dedup_test.go`: - duplicates (same observer and path) are rejected, while a new path or a new observer is kept; - a transmission under the threshold carries no maps; - across the threshold the maps take over with the same answers; - empty observer IDs aren't counted as observers; - `BenchmarkObsDedup`. - Red commit first (stubs, failing on assertions), then green. - `cd cmd/server && go test ./...` passes. - SonarQube: quality gate passed, no issues on added lines. Backend only: no UI, API shape or config change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01KaR4xu8gVykQBkH8yG8m6H --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
2.4 KiB
Go
71 lines
2.4 KiB
Go
package main
|
|
|
|
// obsDedupKey identifies an observation within its transmission: the same
|
|
// observer reporting the same path is a duplicate.
|
|
type obsDedupKey struct{ observerID, pathJSON string }
|
|
|
|
// obsIndexThreshold is the observation count at which a transmission starts
|
|
// keeping maps for its duplicate and distinct-observer checks. Below it the
|
|
// checks scan tx.Observations, which compares string headers and costs
|
|
// nothing to keep; the maps cost a few hundred bytes per transmission, and
|
|
// most transmissions are heard a handful of times (a week of a busy regional
|
|
// network peaked at 40). A miss scans in about 150ns at 40 observations and
|
|
// 240ns at 63; past the threshold a map lookup is about 15ns. The maps' keys
|
|
// share the observations' strings, so building them allocates no key bytes.
|
|
const obsIndexThreshold = 64
|
|
|
|
// hasObservation reports whether tx already holds an observation from
|
|
// observerID with pathJSON.
|
|
func (tx *StoreTx) hasObservation(observerID, pathJSON string) bool {
|
|
if tx.obsKeys != nil {
|
|
_, ok := tx.obsKeys[obsDedupKey{observerID, pathJSON}]
|
|
return ok
|
|
}
|
|
for _, o := range tx.Observations {
|
|
if o.ObserverID == observerID && o.PathJSON == pathJSON {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// hasObserver reports whether any observation on tx came from observerID.
|
|
func (tx *StoreTx) hasObserver(observerID string) bool {
|
|
if tx.observerSet != nil {
|
|
_, ok := tx.observerSet[observerID]
|
|
return ok
|
|
}
|
|
for _, o := range tx.Observations {
|
|
if o.ObserverID == observerID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// addObservation appends obs to tx and keeps UniqueObserverCount and
|
|
// ObservationCount in step. Callers check hasObservation first.
|
|
func (tx *StoreTx) addObservation(obs *StoreObs) {
|
|
if obs.ObserverID != "" && !tx.hasObserver(obs.ObserverID) {
|
|
tx.UniqueObserverCount++
|
|
}
|
|
tx.Observations = append(tx.Observations, obs)
|
|
tx.ObservationCount++
|
|
switch {
|
|
case tx.obsKeys != nil:
|
|
tx.obsKeys[obsDedupKey{obs.ObserverID, obs.PathJSON}] = struct{}{}
|
|
if obs.ObserverID != "" {
|
|
tx.observerSet[obs.ObserverID] = struct{}{}
|
|
}
|
|
case len(tx.Observations) >= obsIndexThreshold:
|
|
tx.obsKeys = make(map[obsDedupKey]struct{}, 2*len(tx.Observations))
|
|
tx.observerSet = make(map[string]struct{}, len(tx.Observations))
|
|
for _, o := range tx.Observations {
|
|
tx.obsKeys[obsDedupKey{o.ObserverID, o.PathJSON}] = struct{}{}
|
|
if o.ObserverID != "" {
|
|
tx.observerSet[o.ObserverID] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
}
|