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

Optimize message parsing #79

Merged
merged 2 commits into from
Jun 28, 2023
Merged

Optimize message parsing #79

merged 2 commits into from
Jun 28, 2023

Conversation

corneliusweig
Copy link
Collaborator

So far, the message was parsed three times:

  1. To determine if it is a request/response/event (->Type) 2) To determine the command (->Command/Event/Success) to determine the
    right ctor
  2. A final pass with the right ctor

Steps 1+2 can be combined into a single step by using a union type of the fields that are required to determine the right ctor. This PR does exactly that. It combines steps 1+2 so that the message needs to be parsed only once to determine the ctor.
The final parsing step (3) with the right ctor can of course not be elided. So the number of parsing operations is reduced from 3 to 2. This is a big win, because JSON parsing is slow, especially for large messages.

@corneliusweig
Copy link
Collaborator Author

@suzmue Can you take a look if this optimization of message parsing makes sense to you?

codec.go Show resolved Hide resolved
codec.go Outdated Show resolved Hide resolved
So far, the message was parsed three times:
1) To determine if it is a request/response/event (->Type)
2) To determine the command (->Command/Event/Success) to determine the
   right ctor
3) A final pass with the right ctor

Steps 1+2 can be combined into a single step by using a union type of
the fields that are required to determine the right ctor. This PR does
exactly that. It combines steps 1+2 so that the message needs to be
parsed only once to determine the ctor.
The final parsing step (3) with the right ctor can of course not be
elided. So the number of parsing operations is reduced from 3 to 2. This
is a big win, because JSON parsing is slow, especially for large
messages.

Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
codec.go Show resolved Hide resolved
codec.go Outdated Show resolved Hide resolved
@suzmue suzmue requested a review from hyangah June 27, 2023 18:49
@suzmue suzmue merged commit 9ced64e into google:main Jun 28, 2023
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