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

chore: Remove unneeded allocations in serializers #6

Merged
merged 1 commit into from
Jul 7, 2023

Conversation

oblique
Copy link
Member

@oblique oblique commented Jul 7, 2023

This PR reduces the allocations when deserializing in half.

Most of the implemented deserializers was doing a two step deserialization, the first step was to get string value of the field and the second was to convert it to the actual type (i.e. decode base64 to vec). Until now the first step was using String which allocates a new string for the value, however it could just borrow it.

The usual way to do this is <&str>::deserialize(deserializer) whoever, in the case of JSON this will fail if the borrowed string contains the escape character \. Another issue was that Cow::<str>::deserialize always allocates.

A solution was to implement the wrapper CowStr which wraps Cow<str> and uses #[serde(borrow)] which can handle the borrowing of the string.

@oblique oblique requested a review from zvolin July 7, 2023 06:49
Copy link
Member

@zvolin zvolin left a comment

Choose a reason for hiding this comment

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

LGTM

@zvolin zvolin merged commit 91d4566 into eigerco:v0.32.x-celestia Jul 7, 2023
@oblique oblique deleted the chore/rm-unneeded-allocs branch July 18, 2023 06:07
This was referenced Jan 10, 2024
@zvolin zvolin mentioned this pull request Jan 12, 2024
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.

None yet

2 participants