Skip to content

perf(keys): borrow key material instead of cloning on every encode/decode #121

Closed

Description

@ZhuchkaTriplesix

Problem

EncodingKeyMaterial::encoding_key() and DecodingKeyMaterial::decoding_key() return self.key.clone() (rust/src/keys.rs:166, rust/src/keys.rs:189), and jsonwebtoken's verifier/signer factory clones the key again into a Box. For RSA that is two ~1.2 KB DER heap copies per call; for HMAC it also leaves non-zeroized copies of the secret behind (only the intermediate buffer is Zeroizing).

Proposal

Mark EncodingKey / DecodingKey as #[pyclass(frozen)], borrow via Bound::get() (no PyRef borrow-flag check), and pass &Jwt*Key into py.detach instead of an owned clone. Combines naturally with the cached-parsed-key work (#120).

Acceptance criteria

  • No key clone on the encode / decode / decode_verified_complete hot paths when a prebuilt key object is passed
  • Key classes are frozen
  • No behavior change; tests green

Files

  • rust/src/keys.rs, rust/src/api.rs

Branch

issue/<N>-borrow-keys from dev

Activity

  1. added a commit that references this issue on Sep 28, 2026
  2. ZhuchkaTriplesix commented on Sep 28, 2026

    @ZhuchkaTriplesix
    MemberAuthor

    Fixed in #127 (merged to dev).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p1Should ship in 0.4.0performanceThroughput and latency

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions