Skip to content

Micro-optimize decode(type=Struct) - #77

Merged
jcrist merged 1 commit into
masterfrom
faster-decode-struct-call
Mar 9, 2022
Merged

jcrist merged 1 commit into
masterfrom
faster-decode-struct-call

Conversation

@jcrist

@jcrist jcrist commented Feb 28, 2022

Copy link
Copy Markdown
Member

For the common case of msgspec.json.decode(msg, type=Struct), we can
allocate the TypeNode on the stack and have a faster init path,
reducing overhead per call. This still isn't as fast as creating a
Decoder once and using Decoder.decode, but it brings things closer.
On my machine, decoding a simple Point(x=1, y=2) struct takes the
following times:

  • decode (master): 240 ns
  • decode (this PR): 150 ns
  • Decoder.decode: 120 ns

For the common case of `msgspec.json.decode(msg, type=Struct)`, we can
allocate the `TypeNode` on the stack and have a faster init path,
reducing overhead per call. This still isn't as fast as creating a
`Decoder` once and using `Decoder.decode`, but it brings things closer.
On my machine, decoding a simple `Point(x=1, y=2)` struct takes the
following times:

- `decode` (master): 240 ns
- `decode` (this PR): 150 ns
- `Decoder.decode`: 120 ns
@jcrist

jcrist commented Feb 28, 2022

Copy link
Copy Markdown
Member Author

Going to sit on this one for a bit, I'm still not sure I want the (mild) added complexity in favor of the (mild) performance improvement.

@jcrist

jcrist commented Mar 9, 2022

Copy link
Copy Markdown
Member Author

Upon further thought, I do think this is worth it. Merging.

@jcrist
jcrist merged commit eb4f482 into master Mar 9, 2022
@jcrist
jcrist deleted the faster-decode-struct-call branch March 9, 2022 06:11
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