Skip to content
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

Jeremie/cs module #1719

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

Jeremie/cs module #1719

wants to merge 2 commits into from

Conversation

lcodes
Copy link
Contributor

@lcodes lcodes commented Sep 18, 2024

Updates runtime and codegen to support multiple tables per row type

@lcodes lcodes added the abi-break A PR that makes an ABI breaking change label Sep 18, 2024
@@ -129,14 +162,21 @@ public TableDeclaration(GeneratorAttributeSyntaxContext context)
throw new InvalidOperationException("Tagged enums cannot be tables.");
}

var attrArgs = context.Attributes.Single().NamedArguments;
Visibility = context.TargetSymbol.DeclaredAccessibility switch {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this restriction necessary / what happens if we don't check it ourselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's in the proposal that the generated types match the visibility of their source ones.

So if a table is internal, its handle view should also be internal, and so on.

@RReverser
Copy link
Member

Just to duplicate a note from DMs - I noticed a few things in the PR that would be caught by the codegen tests, and looks like the snapshots haven't been updated in the PR. Can you please run ...\bindings-csharp> dotnet test and fixup the issues it finds and commit the updated snapshots? Otherwise I'm afraid I might be catching things in review that those tests would already catch as well.

Also #1718 might help with some attribute parsing here.

@@ -77,6 +59,14 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
}
}

public abstract class BaseReducerContext<DbView> : DbContext<DbView>, IReducerContext
where DbView : class, new()
Copy link
Member

Choose a reason for hiding this comment

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

Unless we have an interface to uphold, I'd rather not spell out extra constraints. In this case it doesn't seem to add much value in terms of checks.

Suggested change
where DbView : class, new()
where DbView : new()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made it a class because it does carry state; the connection handle in this case. A struct could be copied/defaulted and introduce a silent null connection. This forces it to be a reference type, and given there's only one per application the distinction between value/ref is no longer relevant, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well in this case Local is stateless, but the same base class has to support DbConnection on the client side.

public abstract class BaseReducerContext<DbView> : DbContext<DbView>, IReducerContext
where DbView : class, new()
{
public Identity Sender => Runtime.SenderIdentity!;
Copy link
Member

@RReverser RReverser Sep 18, 2024

Choose a reason for hiding this comment

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

Why are these set on the singleton now? What happens if someone wants to store a ReducerContext? It would now lose its own identity and point to the one from the currently running reducer instead.

I think those should be own fields, like before.

For Random we only used Runtime because we wanted to access the RNG anywhere in the codebase. It only needed reseeding, but wasn't actually tied to the reducer data - the result of RNG is non-deterministic by design, so returning data from the wrong context was not a concern, but for identity/address it will be.

Even for RNG that's changing in #1681 and they're all going to live on the context now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reducer context is created once for the duration of the module, and @gefjon says each reducer invocation should be seen as its own isolated instance with no memory able to outlive that invocation.

Making them fields would need a new ReducerContext per reducer invocation, which means its DbView instance has to either also be created per invocation, or stored somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fine with either approaches; I guess it depends on what semantics we want for memory across reducer invocations, and if we want to leverage isolation in the impl like this ReducerContext does here.

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class ReducerAttribute : Attribute
{
public string? Name;
Copy link
Member

Choose a reason for hiding this comment

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

Reducer kind being a special name was more of an implementation detail, and that will be changing in the new proposal (WIP in #1670) where ReducerKind is stored separately.

AFAIK the SDK proposal also doesn't allow custom #[spacetimedb::reducer(name = "...")], so we shouldn't be doing that in C# either - can you please revert this to a regular attribute parameter, so it would be easier to migrate to non-name-based ReducerKind in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
abi-break A PR that makes an ABI breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants