8000 Optimize internal calls to UnmarshalCBOR() for ByteString, RawTag, SimpleValue by fxamacker · Pull Request #647 · fxamacker/cbor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Optimize internal calls to UnmarshalCBOR() for ByteString, RawTag, SimpleValue #647

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

Merged

Conversation

fxamacker
Copy link
Owner
@fxamacker fxamacker commented Mar 28, 2025

Closes #646

This PR resolves a performance regression introduced and mentioned in PR #636 and #645:

the same data is checked twice for the intended case of the codec calling UnmarshalCBOR() internally. This can be revisited and maybe optimized in the future.

This optimization avoids the redundant 2nd check of input data.

These functions are deprecated (they were created for internal use):

  • ByteString.UnmarshalCBOR()
  • RawTag.UnmarshalCBOR()
  • SimpleValue.UnmarshalCBOR()

Unmarshal() should be used instead of the deprecated functions.

Details

PR #636 and #645 cause the input data to be checked twice when these functions are called internally by Unmarshal():

  • ByteString.UnmarshalCBOR()
  • RawTag.UnmarshalCBOR()
  • SimpleValue.UnmarshalCBOR()

These functions check input data because they can be called by user apps providing bad data. However, the codec already checks input data before internally calling UnmarshalCBOR() so the 2nd check is redundant.

This PR avoids redundant check on the input data by having Unmarshal() call the private unmarshalCBOR() implemented by ByteString, RawTag, SimpleValue:

  • Internally, the codec calls the private unmarshalCBOR() to avoid the redundant check on input data.
  • Externally, UnmarshalCBOR() is available as a wrapper that checks input data before calling the private unmarshalCBOR().

To avoid breaking user apps, the 3 UnmarshalCBOR() functions are deprecated rather than removed.

Currently, unreleased changes in PR #636 and #645 cause the
input data to be checked twice when UnmarshalCBOR() is
called internally by Unmarshal() for:
- ByteString
- RawTag
- SimpleValue

UnmarshalCBOR() checks input data because it can be called by
user apps providing bad data. However, the codec already checks
input data before internally calling UnmarshalCBOR() so the
2nd check is redundant.

This commit avoids redundant check on the input data by having
Unmarshal() call the private unmarshalCBOR() if implemented
by ByteString, RawTag, SimpleValue, etc.:
- Internally, the codec calls the private unmarshalCBOR() to
  avoid the redundant check on input data.
- Externally, UnmarshalCBOR() is available as a wrapper that
  checks input data before calling the private unmarshalCBOR().

UnmarshalCBOR() for ByteString, RawTag, and SimpleValue are marked
as deprecated and Unmarshal() should be used instead.
@fxamacker fxamacker added this to the v2.8.0 milestone Mar 28, 2025
@fxamacker fxamacker self-assigned this Mar 28, 2025
@fxamacker
Copy link
Owner Author

PTAL @x448

@fxamacker fxamacker changed the title Optimize calls to UnmarshalCBOR() for RawTag, etc. Optimize internal calls to UnmarshalCBOR() for ByteString, RawTag, SimpleValue Mar 28, 2025
@fxamacker fxamacker merged commit c9f772d into master Mar 28, 2025
22 checks passed
@fxamacker fxamacker deleted the fxamacker/optimize-unmarshalcbor-for-cbor-builtin-structs branch April 1, 2025 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize internal calls to UnmarshalCBOR() for ByteString, RawTag, and SimpleValue
2 participants
0