Skip to content

der: optimize compile-time: code reuse with encode_value_to_slice #1940

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

dishmaker
Copy link
Contributor

before:

formats$ cargo llvm-lines -p der --all-features --release | head -n 8
  Lines                Copies             Function name
  -----                ------             -------------
  53357                920                (TOTAL)
   6352 (11.9%, 11.9%)  29 (3.2%,  3.2%)  der::asn1::any::AnyRef::decode_as_encoding
   2330 (4.4%, 16.3%)   10 (1.1%,  4.2%)  der::asn1::integer::value_cmp
    915 (1.7%, 18.0%)    5 (0.5%,  4.8%)  der::asn1::integer::int::decode_to_array
    904 (1.7%, 19.7%)   16 (1.7%,  6.5%)  std::thread::local::LocalKey<T>::try_with
    872 (1.6%, 21.3%)    1 (0.1%,  6.6%)  <der::error::ErrorKind as core::fmt::Display>::fmt

after:

formats$ cargo llvm-lines -p der --all-features --release | head -n 8
  Lines                Copies             Function name
  -----                ------             -------------
  52897                930                (TOTAL)
   6352 (12.0%, 12.0%)  29 (3.1%,  3.1%)  der::asn1::any::AnyRef::decode_as_encoding
   1350 (2.6%, 14.6%)   10 (1.1%,  4.2%)  der::asn1::integer::value_cmp
    915 (1.7%, 16.3%)    5 (0.5%,  4.7%)  der::asn1::integer::int::decode_to_array
    904 (1.7%, 18.0%)   16 (1.7%,  6.5%)  std::thread::local::LocalKey<T>::try_with
    872 (1.6%, 19.6%)    1 (0.1%,  6.6%)  <der::error::ErrorKind as core::fmt::Display>::fmt

Comment on lines 36 to 43
fn encode_value_to_slice<'a, T>(buf: &'a mut [u8], value: &T) -> Result<&'a [u8]>
where
T: EncodeValue,
{
let mut encoder = SliceWriter::new(buf);
value.encode_value(&mut encoder)?;
encoder.finish()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just a helper function for / implementation detail of value_cmp, can you nest it inside there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's useful in few other places.

@dishmaker dishmaker force-pushed the dishmaker/integer_value_cmp_llvm branch from 93fba92 to eb18a0f Compare July 15, 2025 08:08
@dishmaker dishmaker changed the title der: optimize compilation time of integer::value_cmp der: optimize compile-time: code reuse with encode_value_to_slice Jul 15, 2025
@dishmaker
Copy link
Contributor Author

Optimized from 53357 to 52248 lines

$ cargo llvm-lines -p der --all-features --release | head -n 8
  Lines                Copies             Function name
  -----                ------             -------------
  52248                930                (TOTAL)
   6352 (12.2%, 12.2%)  29 (3.1%,  3.1%)  der::asn1::any::AnyRef::decode_as_encoding
   1350 (2.6%, 14.7%)   10 (1.1%,  4.2%)  der::asn1::integer::value_cmp
    915 (1.8%, 16.5%)    5 (0.5%,  4.7%)  der::asn1::integer::int::decode_to_array
    904 (1.7%, 18.2%)   16 (1.7%,  6.5%)  std::thread::local::LocalKey<T>::try_with
    872 (1.7%, 19.9%)    1 (0.1%,  6.6%)  <der::error::ErrorKind as core::fmt::Display>::fmt

@tarcieri tarcieri merged commit 3aaf6ae into RustCrypto:master Jul 15, 2025
107 checks passed
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.

2 participants