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

feat(server): add sync subscription API register_subscription_raw #1182

Merged
merged 6 commits into from
Aug 30, 2023

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Aug 14, 2023

The motivation is behind this is because this is #1146 if a subscription is having params that are big or happen very frequently it may allocate.

Thus, we now exposes two different APIs for that and our proc macros API will use the lower-level/sync API to parse the params outside async block/callback to avoid that allocation.

After this PR we will have:

  • register_method (doesn't allocate for the params)
  • register_async_method
  • register_subscription_raw (doesn't not allocate for the params)
  • register_subscription

@niklasad1 niklasad1 requested a review from a team as a code owner August 14, 2023 10:34
// definition and not the as same when the subscription call has been completed.
//
// This runs until the subscription callback has completed.
let res = callback(params, sink, ctx.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

To make sure I get this right, this avoids an allocation because we pass the parameters directly to the callback instead of calling into_owned, which can allocate quite a bit since the params might either be frequent or large?

Copy link
Member Author

Choose a reason for hiding this comment

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

yupp, you got it right.

It's possible to parse the params outside the callback/async block and avoid that allocation with this....

This alone in my substrate storage subscription test brought down the memory allocations from 2.5GB to 2GB

F: (Fn(Params, PendingSubscriptionSink, Arc<Context>) -> R) + Send + Sync + Clone + 'static,
R: IntoSubscriptionCloseResponse + Send + 'static,
{
if subscribe_method_name == unsubscribe_method_name {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Since the beginning of this is similar to register_async_subscription would it be possible to move some code into a shared function? Maybe we could have a internal_verify_and_unsubscribe which does the initial validation and registers the unsubscribe?

Copy link
Member Author

Choose a reason for hiding this comment

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

sure, I just want know what you think about the API but unfortunately a breaking change :(

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, that makes sense! From my perspective, this gives a nice boost in terms of performance, users that may value more ergonomics still the old method 👍

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Personally I'd swap the names around sortof, and have something like:

  • register_subscription - the original one as it was
  • register_subscription_sync/register_subscription_raw - the lower level synchronous/raw method (you still need to .await things inside it but can decide how.

I might be tempted to also #[doc(hidden)] the sync/raw one because maybe better if people don't rely on it in the interface and then we can tune it however we need to work best with the macro without worrying about any breakages.

@niklasad1
Copy link
Member Author

niklasad1 commented Aug 29, 2023

Cool, yeah ok the intention was the methods has this register_async_method and register_method and to provide similar APIs...

I think we can make it register_subscription_raw then, the good thing with that is no breaking change :P

@jsdw
Copy link
Collaborator

jsdw commented Aug 30, 2023

That's cool; register_subscription_raw and register_method_raw could be the "low level" interfaces then if people want to customise how to do the async stuff, and the current ones stay the same :)

@niklasad1
Copy link
Member Author

Yeah, I think that makes sense because this added API isn't really ergonomic to use

@niklasad1 niklasad1 changed the title refactor(server): async and sync subscription APIs feat(server): add sync subscription API register_subscription_raw Aug 30, 2023
@niklasad1 niklasad1 merged commit 62bd0ca into master Aug 30, 2023
15 checks passed
@niklasad1 niklasad1 deleted the na-add-sync-subscription branch August 30, 2023 12:12
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