Skip to content

Inline common JSON encode dispatch - #294

Merged
jcrist merged 1 commit into
mainfrom
inline-common-json-encode-dispatch
Feb 4, 2023
Merged

Inline common JSON encode dispatch#294
jcrist merged 1 commit into
mainfrom
inline-common-json-encode-dispatch

Conversation

@jcrist

@jcrist jcrist commented Feb 4, 2023

Copy link
Copy Markdown
Member

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 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).
@jcrist
jcrist merged commit 41d148a into main Feb 4, 2023
@jcrist
jcrist deleted the inline-common-json-encode-dispatch branch February 4, 2023 04:01
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