Skip to content

perf: cache is_private_key flag to avoid per-call BIGNUM alloc on 3.x#161

Merged
atoomic merged 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-issue-152
Apr 5, 2026
Merged

perf: cache is_private_key flag to avoid per-call BIGNUM alloc on 3.x#161
atoomic merged 1 commit intocpan-authors:mainfrom
atoomic:koan.atoomic/fix-issue-152

Conversation

@Koan-Bot
Copy link
Copy Markdown
Contributor

@Koan-Bot Koan-Bot commented Apr 3, 2026

Summary

On OpenSSL 3.x, _is_private() called EVP_PKEY_get_bn_param() on every invocation, which heap-allocates a full BIGNUM just to test non-nullness and immediately frees it. This hit every sign(), decrypt(), private_encrypt(), and check_key() call.

Fixes #152

Changes

  • Add int is_private_key field to rsaData struct
  • Extract detection logic into _detect_private_key() helper called once from make_rsa_obj()
  • _is_private() now returns the cached flag — zero heap allocations on the hot path

Test plan

  • All 477 existing tests pass (make test)
  • is_private() is exercised in t/rsa.t, t/check_param.t, t/key_lifecycle.t — all green

Generated by Kōan /fix


Quality Report

Changes: 1 file changed, 16 insertions(+), 10 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

…lloc

On OpenSSL 3.x, _is_private() called EVP_PKEY_get_bn_param() which
heap-allocates a full BIGNUM just to test non-nullness, then frees it.
This happened on every sign/decrypt/private_encrypt/check_key call.

Add is_private_key field to rsaData, detected once in make_rsa_obj()
via a new _detect_private_key() helper. _is_private() now returns the
cached flag directly — zero heap allocations on the hot path.

Fixes cpan-authors#152

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@timlegge timlegge left a comment

Choose a reason for hiding this comment

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

looks good

@timlegge timlegge marked this pull request as ready for review April 5, 2026 00:31
@atoomic atoomic merged commit ae488e3 into cpan-authors:main Apr 5, 2026
28 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.

optimization: _is_private() heap-allocates a BIGNUM on every call under OpenSSL 3.x

3 participants