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

Fix formatted json encode #10

Merged
merged 7 commits into from
Sep 9, 2021
Merged

Fix formatted json encode #10

merged 7 commits into from
Sep 9, 2021

Conversation

FZambia
Copy link
Member

@FZambia FZambia commented Sep 9, 2021

Currently Javascript client parser can't delimit one message from another if we send formatted JSON with new lines as we are not compacting JSON since recent update (i.e. using easyjson).

This pull request fixes this removing new lines in JSON if any. Also some helpers introduced to further reduce allocations in both Protobuf and JSON cases – we now reusing byte slices where it's safe to do, so we avoid extra byte slice copy and thus saving one allocation.

Before

BenchmarkReplyProtobufMarshal-12              	 5600274	       214.7 ns/op	     864 B/op	       3 allocs/op
BenchmarkReplyProtobufMarshalParallel-12      	 7303191	       168.9 ns/op	     864 B/op	       3 allocs/op
BenchmarkReplyJSONMarshal-12                  	 2644131	       455.6 ns/op	     928 B/op	       3 allocs/op
BenchmarkReplyJSONMarshalParallel-12          	 4816104	       252.9 ns/op	     928 B/op	       3 allocs/op

After, without new line

BenchmarkReplyProtobufMarshal-12              	 5865307	       199.0 ns/op	     576 B/op	       2 allocs/op
BenchmarkReplyProtobufMarshalParallel-12      	10106815	       121.4 ns/op	     576 B/op	       2 allocs/op
BenchmarkReplyJSONMarshal-12                  	 2558091	       465.9 ns/op	     640 B/op	       2 allocs/op
BenchmarkReplyJSONMarshalParallel-12          	 5936446	       216.2 ns/op	     641 B/op	       2 allocs/op

After, with new line

BenchmarkReplyProtobufMarshal-12              	 5929026	       199.1 ns/op	     576 B/op	       2 allocs/op
BenchmarkReplyProtobufMarshalParallel-12      	10129752	       121.9 ns/op	     576 B/op	       2 allocs/op
BenchmarkReplyJSONMarshal-12                  	 2119388	       568.3 ns/op	     928 B/op	       3 allocs/op
BenchmarkReplyJSONMarshalParallel-12          	 4530106	       268.6 ns/op	     929 B/op	       3 allocs/op

So Protobuf is now faster regardless new lines - since it does not matter as we are using length-prefixed format. JSON encode is faster for already compact input and slightly slower (and +1 alloc) with input which contains new line.

@FZambia FZambia merged commit 2cd0129 into master Sep 9, 2021
@FZambia FZambia deleted the fix_formatted_json_encode branch September 9, 2021 11:25
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.

1 participant