Improve events sdk - #5795
Draft
lisandroct wants to merge 26 commits into
Draft
Conversation
lisandroct
marked this pull request as draft
August 24, 2026 17:17
lisandroct
force-pushed
the
lisandro/improve-events-sdk
branch
from
September 3, 2026 17:50
8a019ed to
b9ce9ba
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
For users with heavy callback churn, the SDK now supports opting into custom event listener management before creating table handles:
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, andOnUpdateListenersusingAddSapTarget/RemoveSapTarget.Additional changes:
Benchmark results:
Benchmark results from a local run against
spacetime startwith 1 warmup and 3 measured iterations showed the main benefit in mass unsubscription scenarios: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, andOnUpdateListenersaccessors 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