Skip to content

perf(monitors): remove std::function heap allocs and use direct map find in pub-sub fanout#224

Merged
hoytech merged 1 commit intohoytech:masterfrom
archief2910:perf/Activemonitors-hot-path
Apr 28, 2026
Merged

perf(monitors): remove std::function heap allocs and use direct map find in pub-sub fanout#224
hoytech merged 1 commit intohoytech:masterfrom
archief2910:perf/Activemonitors-hot-path

Conversation

@archief2910
Copy link
Copy Markdown
Contributor

Description

ActiveMonitors::process runs for every new event that is applied to live subscriptions. It previously looked up candidate monitor sets with upper_bound + prev + a while loop and a std::function<bool(const T&)> built from a lambda for each of id, author, each tag, and kind.

Monitor indexes are installed with one btree_map entry per exact key (event id, author pubkey, tag spec string, kind). Keys are unique, and every call site only ever used an equality predicate (id == val, etc.). For such a map, the only matching node is m.find(key); the old walk could touch at most that same entry.

This change replaces that path with a small templated lookupMonitors helper that calls find and processes the MonitorSet when present. It removes per-event std::function construction (and associated type erasure / allocation behavior) and simplifies the lookup logic with no change to which monitors are considered.

Files: src/ActiveMonitors.h

Motivation and context

Live subscription fanout is on the hot path when the relay is under write load. Fewer temporary objects and a direct map lookup improve CPU and allocator pressure under realistic workloads, without changing Nostr semantics.

How has this been tested?

  • make -j4 (full tree)
  • Existing relay / subscription behavior unchanged by construction (same keys, same processMonitorSet body); no new tests added.
  • script automated testing

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix
  • New feature / improvement (performance)
  • Breaking change

Checklist

  • Matches project style
  • Build passes locally

@hoytech
Copy link
Copy Markdown
Owner

hoytech commented Apr 28, 2026

Nice one, thanks! I think this is a vestigial feature for when we supported prefix lookup.

@hoytech hoytech merged commit ddcc7e7 into hoytech:master Apr 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants