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

Add Serialization.WithTransport overload that takes TState #4764

Merged

Conversation

to11mtm
Copy link
Member

@to11mtm to11mtm commented Feb 7, 2021

This PR adds the following method to Akka.Serialization.Serialization:

public static T WithTransport<TState, T>(ExtendedActorSystem system, TState state, Func<TState, T> action)

The purpose of this overload is to allow a TState value to be passed into the action when executed, allowing consumers to avoid allocations due to delegate captures if present.

It looks like remoting already does something similar to this internally since it has access to the right internals, but there are other aspects of Akka (specifically persistence) that could benefit from this overload.

This PR also contains a benchmark of both versions of WithTransport. It looks like we are a little bit faster:

Method Mean Error StdDev
Serialization_WithTransport_NoState 2.991 us 0.0201 us 0.0178 us
Serialization_WithTransport_State 2.864 us 0.0482 us 0.0427 us

I'm not sure why it comes up slower with [MemoryDiagnoser], but important thing is we save ~90 bytes here:

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
Serialization_WithTransport_NoState 2.849 us 0.0366 us 0.0324 us 0.8087 0.0038 - 3.72 KB
Serialization_WithTransport_State 2.893 us 0.0518 us 0.0485 us 0.7858 0.0038 - 3.63 KB

That may not look like much but since this is called deep in pipelines I'm expecting at least some gains in Persistence benches. Working on looking at that in Sql.Common

@to11mtm to11mtm force-pushed the serialization-withtransport-tstate branch from 1f5031a to cf2691a Compare February 7, 2021 19:06
@to11mtm to11mtm force-pushed the serialization-withtransport-tstate branch from cf2691a to 6a43275 Compare February 7, 2021 19:11
var payloadType = e.Payload.GetType();
var serializer = Serialization.FindSerializerForType(payloadType, Configuration.DefaultSerializer);

var serializer = Serialization.FindSerializerForType(e.Payload.GetType(), Configuration.DefaultSerializer);
Copy link
Member Author

Choose a reason for hiding this comment

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

Idea here is to avoid a local we don't use anywhere else.


// TODO: hack. Replace when https://github.com/akkadotnet/akka.net/issues/3811
string manifest = "";
var binary = Akka.Serialization.Serialization.WithTransport(Serialization.System, () =>
var (binary,manifest) = Akka.Serialization.Serialization.WithTransport(Serialization.System,(e.Payload,serializer) ,(state) =>
Copy link
Member Author

Choose a reason for hiding this comment

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

by deconstructing here, we avoid requiring a capture on manifest.

{

if (serializer is SerializerWithStringManifest stringManifest)
var (thePayload, theSerializer) = state;
string thisManifest = "";
Copy link
Member Author

Choose a reason for hiding this comment

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

We're keeping a local here because changing this into a Ternary at end was both:

  • Harder to read
  • Slower because the complex ternary doesn't optimize well =/

@to11mtm
Copy link
Member Author

to11mtm commented Feb 7, 2021

It looks like in Sql.Common Persist and others appear to be both more consistent and a liiiitle bit faster.

@to11mtm to11mtm marked this pull request as ready for review February 24, 2021 22:30
@to11mtm to11mtm requested a review from Arkatufus February 26, 2021 20:48
Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

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

LGTM - great idea

@Aaronontheweb Aaronontheweb removed the request for review from Arkatufus March 2, 2021 14:09
@Aaronontheweb Aaronontheweb added this to the 1.4.17 milestone Mar 2, 2021
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) March 2, 2021 14:09
@Aaronontheweb Aaronontheweb disabled auto-merge March 2, 2021 17:02
@Aaronontheweb Aaronontheweb merged commit d8ad3c7 into akkadotnet:dev Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants