Inline common JSON encode dispatch - #294
Merged
Merged
Conversation
This inlines and reorganizes code in the `json_encode` dispatch. The main benefits of this are: - Move less common types into a separate function - Inline the common type dispatch in collection types where the value is likely to be uniformly typed. This includes `list`, `set`, `dict`, and `tuple`, but excludes structured data types like `struct`/`dataclass`. This results in a 12% perf improvement on common benchmark datasets. I think this is due to a mix of improved locality and branch prediction. Inlining results in each collection type having its own copy of the common branches, which the branch predictor will track separately. This means that encoding a `dict[str, list[int]]` type will have `json_encode_dict` predicting to take the `list` branch and `json_encode_list` predicting to take the `int` branch. This change is definitely biased towards benchmark gaming, but I've also tested it on some real world workflows and at worst it doesn't result in a regression (and usually it results in an improvement).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This inlines and reorganizes code in the
json_encodedispatch. The main benefits of this are:list,set,dict, andtuple, but excludes structured data types likestruct/dataclass.This results in a 12% perf improvement on common benchmark datasets.
I think this is due to a mix of improved locality and branch prediction. Inlining results in each collection type having its own copy of the common branches, which the branch predictor will track separately. This means that encoding a
dict[str, list[int]]type will havejson_encode_dictpredicting to take thelistbranch andjson_encode_listpredicting to take theintbranch.This change is definitely biased towards benchmark gaming, but I've also tested it on some real world workflows and at worst it doesn't result in a regression (and usually it results in an improvement).