Skip to content

ASN structure reading optimizations#107

Merged
microshine merged 8 commits intomasterfrom
low-speed
Sep 8, 2025
Merged

ASN structure reading optimizations#107
microshine merged 8 commits intomasterfrom
low-speed

Conversation

@microshine
Copy link
Copy Markdown
Contributor

Overview

This update introduces significant performance improvements focused on optimizing the reading and parsing of ASN.1 structures, particularly for large certificates, CRLs, and certificate requests. The changes implement lazy loading patterns and leverage existing raw ASN.1 data to reduce computational overhead.

Key Changes

1. Lazy Property Loading

  • X509Certificate: Converted all public properties to lazy getters using private fields (#serialNumber, #subjectName, etc.)
  • X509Crl: Implemented lazy loading for issuer, dates, entries, and extensions
  • X509CrlEntry: Added lazy getters for serial number, revocation date, reason, invalidity, and extensions
  • Pkcs10CertificateRequest: Converted properties to lazy loading pattern

Impact: Expensive computations (parsing extensions, creating Name objects) are deferred until properties are actually accessed, improving initialization time for large structures.

2. Raw ASN.1 Data Utilization

  • X509Certificate: Uses asn.tbsCertificateRaw for ToBeSigned block
  • X509Crl: Uses asn.tbsCertListRaw for ToBeSigned block
  • Pkcs10CertificateRequest: Uses asn.certificationRequestInfoRaw for ToBeSigned block

Impact: Eliminates redundant serialization of already-parsed ASN.1 data, reducing memory usage and CPU time for large certificates.

3. Optimized CRL Entry Search

  • X509Crl.findRevoked(): Changed from iterating through parsed entries to direct ASN.1 structure search
  • Uses BufferSourceConverter.isEqual() for byte-level serial number comparison
  • Only creates X509CrlEntry objects when matches are found

Impact: Dramatically improves performance for large CRLs by avoiding full parsing of all revoked certificates during search operations.

4. DER Detection Optimization

  • PemData.toArrayBuffer(): Added early DER detection by checking first byte (0x30)
  • Skips expensive string conversion for large DER buffers

Impact: Faster processing of large DER-encoded data by avoiding unnecessary conversions.

5. Constructor Improvements

  • Updated constructors to handle both encoded data and ASN.1 objects efficiently
  • Simplified initialization logic with lazy loading

Backward Compatibility

All changes maintain full backward compatibility. Existing code will continue to work without modifications, but will benefit from the performance improvements automatically.

Version Recommendation

It is recommended to release this update as a minor version bump due to the internal changes from public properties to getters. While the public API remains the same, the type declarations have changed, which may cause minor TypeScript compatibility issues when upgrading from older versions. This ensures users are aware of the potential need to update their type definitions or rebuild their projects.

@microshine microshine self-assigned this Sep 8, 2025
@microshine microshine added the enhancement New feature or request label Sep 8, 2025
@microshine microshine merged commit 89785e8 into master Sep 8, 2025
6 checks passed
@microshine microshine deleted the low-speed branch September 8, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant