korlibs / korlibs/korlibs-crypto

Hashing Performance Considerations (Allocations)

Open
#9 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
12
Forks
3
PR merge metrics
No merged PRs in 30d

Description

While implementing the two PRs that I just recently made, I noticed that hashing a `ByteArray` via `ByteArray.hash(HasherFactory)` some unnecessary allocations are performed, such as allocating a new `Hasher` every time.

In performance-critical code this is undesirable.
Since the `HasherFactory` is guaranteed to always invoke `.digest()` which finalizes the hash value, perhaps it would be best to pool the `Hasher`s.
- If the pool is empty, a new hasher is created via the `val create: () -> Hasher` property, however if the pool has a hasher in it, then it can be used instead.
- When a hasher is used, it will be removed from the pool.
- After `digest()` is called, `reset()` will always be called before then returning the hasher to the pool.
- The pool could have a 'reasonable' upper cap on the number of entries, and when attempting to insert any entries while already at the cap, they will instead just be discarded.
- The pool would only be local to the `HasherFactory`, and any external users of it would have no awareness of it. `operator fun invoke(): Hasher` will continue to always return a *new* `Hasher`.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating ByteArray.hash(HasherFactory) and HasherFactory, then trace how invoke(), digest(), and reset() interact. Confirm the factory's digest guarantee and determine where pooled hashers can be acquired and returned without changing external invoke() behavior. Done means avoiding repeated hasher allocations while enforcing removal, reset-before-return, and a bounded pool.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
cryptography, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.