Skip to content

Improve events sdk - #5795

Draft
lisandroct wants to merge 26 commits into
masterfrom
lisandro/improve-events-sdk
Draft

Improve events sdk#5795
lisandroct wants to merge 26 commits into
masterfrom
lisandro/improve-events-sdk

Conversation

@lisandroct

@lisandroct lisandroct commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Description of Changes

This PR updates the C# SDK event callback implementation.

By default, table row callbacks now use regular native C# events. This keeps the common path idiomatic:

conn.Db.User.OnInsert += OnUserInsert;
conn.Db.User.OnInsert -= OnUserInsert;

For users with heavy callback churn, the SDK now supports opting into custom event listener management before creating table handles:

EventHandling.Backend.UseCustomListeners();

This adds an indexed listener backend intended to reduce the cost of large numbers of listener removals, duplicate listener registrations, and attach/detach-heavy integration code.

Before, we had a custom listener as the default as it was requested by the BitCraft team but for some projects that path could have been worse, so it makes sense to make it opt-in. The new custom listener also improves on the old one: it's safer and faster.

This PR also adds optional Sappy (which we use in BitCraft and been wanting to use for row subscriptions) integration. Projects that already use Sappy can opt into Sappy-backed listener storage with:
For correct and performant Sappy usage, callbacks should be registered through OnInsertListeners, OnDeleteListeners, and OnUpdateListeners using AddSapTarget / RemoveSapTarget.

Additional changes:

  • Adds tests for duplicate listener registration and remove-one-at-a-time semantics.
  • Adds a benchmark harness comparing native events vs custom listener management, including time and allocation measurements.
  • Adds customer documentation for the default native event behavior, custom listener opt-in, and Sappy-specific usage.

Benchmark results:
Benchmark results from a local run against spacetime start with 1 warmup and 3 measured iterations showed the main benefit in mass unsubscription scenarios:

Scenario Subscribe Δ ms / bytes First updates Δ ms / bytes Unsubscribe Δ ms / bytes Resubscribe Δ ms / bytes Second updates Δ ms / bytes
subs=10, first=1000 +0.003 / -56 -0.402 / +21,635 +0.000 / 0 +0.001 / 0 +0.000 / 0
subs=1000, first=10 +0.050 / +17,184 -0.001 / -221 +0.001 / 0 +0.000 / 0 +0.000 / 0
subs=1000, first=1000 +0.084 / +17,184 -0.525 / -63,808 +0.000 / 0 +0.000 / 0 +0.000 / 0
subs=1000, first=10, unsub=1000 -0.125 / +17,184 -0.043 / -400 -5.996 / -5,475,424 +0.000 / 0 +0.000 / 0
subs=1000, first=10, unsub=1000, resub=1000, second=10 +0.094 / +17,184 +0.346 / -259 -14.748 / -5,475,424 +0.211 / -80,544 -0.039 / +627
subs=1000, first=1000, unsub=1000, resub=1000, second=1000 +0.048 / +17,184 -1.332 / +1,126 -20.552 / -5,475,424 +0.059 / -80,544 +11.422 / +83,152

Deltas are custom - native, so negative values mean the custom listener backend was faster or allocated less. In this run, removing 1000 native event listeners allocated about 5.48 MB, while the custom backend stayed effectively flat.

API and ABI breaking changes

This PR changes the C# SDK event-listener implementation and adds new public APIs for configuring table callback dispatch:

  • SpacetimeDB.EventHandling.Backend.UseNativeEvents()
  • SpacetimeDB.EventHandling.Backend.UseCustomListeners(...)
  • OnInsertListeners, OnDeleteListeners, and OnUpdateListeners accessors for Sappy/custom listener integrations.

But none of these are breaking changes. The only thing is that current customers will use native events by default and it could be either a positive or negative change for them. We should probably announce it in some way.

Rollback safety impact

n/a

Expected complexity level and risk

2

The implementation is localized to the C# SDK event dispatch path, table callback storage, optional Sappy integration, docs, tests, and benchmarks.

The main risk is behavioral compatibility around callback registration/removal semantics, especially duplicate listeners and callbacks removed during active dispatch. That risk is mitigated by keeping native C# events as the default and adding focused listener tests plus benchmark coverage.

Testing

  • Old tests are passing
  • New tests are passing
  • Run new benchmarks
  • Upgraded BitCraft and everything stayed playable (and faster)

@lisandroct
lisandroct marked this pull request as draft August 24, 2026 17:17
@lisandroct
lisandroct force-pushed the lisandro/improve-events-sdk branch from 8a019ed to b9ce9ba Compare September 3, 2026 17:50
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.

1 participant