Skip to content

Add some developer docs #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

kazimuth
Copy link
Contributor

Description of Changes

Add some developer docs.

API

  • This is an API breaking change to the SDK

Requires SpacetimeDB PRs

Testsuite

SpacetimeDB branch name: master

Testing

N/A

@github-actions github-actions bot changed the base branch from release/latest to master July 15, 2025 20:42
@kazimuth
Copy link
Contributor Author

See also clockworklabs/SpacetimeDB#2938

@kazimuth kazimuth requested a review from gefjon July 15, 2025 20:43
Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion and two responses that don't require action. Thanks for writing this, it's gonna be really helpful!


`DbConnection` in the generated code inherits from `DbConnectionBase<...>` in the SDK code, which lives in [`src/SpacetimeDBClient.cs`](./src/SpacetimeDBClient.cs). This is a general pattern. Similar inheritance patterns are used for tables and indexes: the generated code defines a class that inherits most of its behavior from a class in the SDK.

We require that **a DbConnection is only accessed from a single thread**, which should call the `DbConnection.FrameTick()` method frequently. See [threading model](#threading-model), below.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do? Ew...


The C# SDK, unlike the [Rust SDK](https://github.com/clockworklabs/SpacetimeDB/tree/master/crates/sdk), **assumes a DbConnection is only accessed from a single thread**. This thread is referred to as the "main thread". The "main thread" is:
- Whichever thread is repeatedly calling `DbConnection.FrameTick()` in a loop.
It is **only safe to call `FrameTick()` from a single thread**. It is **only safe to access the DbConnection from this thread**.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this solvable/fixable with a lock?

Copy link
Contributor Author

@kazimuth kazimuth Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would require locking on all accesses of conn.Db. I don't think there's a nice way to do this without an API break. The type system is just much less expressive RE: sharing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you, say, require conn.Db to be accessed from a callback that has a lock on the database, the user can still just store the Db you passed outside the callback. In the end I think the only way to do it would be to have the user manage the lock.

We could think about doing something MVCC-y, where each conn.Db you see is actually immutable, and you're just getting new copies of the Db on changes, but I would be worried about the perf implications there.

Copy link
Contributor Author

@kazimuth kazimuth Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might allow more work to be done in the background threads, but it would naively involve a lot more allocations since you'd be creating a new copy of the indices each frame. It might work to just have pools of Dictionary<...> lying around that we reuse. Alternatively, we find some sort of haskelly immutable dictionary that can share state between copies.

Thanks Phoebe

Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org>
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.

3 participants