Skip to content

Avoid Option allocation in hook duration path via HookData.getOrNull#114

Closed
EtaCassiopeia wants to merge 1 commit into
mainfrom
perf/hookdata-getornull
Closed

Avoid Option allocation in hook duration path via HookData.getOrNull#114
EtaCassiopeia wants to merge 1 commit into
mainfrom
perf/hookdata-getornull

Conversation

@EtaCassiopeia
Copy link
Copy Markdown
Owner

@EtaCassiopeia EtaCassiopeia commented May 12, 2026

Summary

The structuredLogging.elapsed, metricsDetailed.after, and metricsDetailed.error hooks read the recorded start time from HookData to compute evaluation duration. The existing code used hookData.get(key).getOrElse(end) which allocates an Option on every flag evaluation — a hot path.

Adds HookData.getOrNull(key): Any (internal, private[openfeature]) that returns the raw value or null, and updates the three duration call sites to pattern-match on null. As a side benefit, when no start time was recorded (defensive case), the read of the end-time clock is skipped entirely and Duration.Zero is returned.

Clock.nanoTime is preserved (consistent with both before and tests), so TestClock interactions remain correct.

Changes

  • Hook.scala: add HookData.getOrNull (internal API)
  • Hook.scala: rewrite structuredLogging.elapsed, metricsDetailed.after, metricsDetailed.error to use null-first pattern match

@EtaCassiopeia

Credit

Spotted in #110 by @guizmaii. The original PR also switched Clock.nanoTimeSystem.nanoTime; this PR keeps Clock.nanoTime for testability.

@EtaCassiopeia
Copy link
Copy Markdown
Owner Author

Tested independently; closing in favor of #110.

@EtaCassiopeia EtaCassiopeia deleted the perf/hookdata-getornull branch May 13, 2026 01:52
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.

1 participant