Skip to content

perf: hot-path optimizations for enqueue!, poll!, and ack! - #1

Closed
mpenet with Copilot wants to merge 1 commit into
mainfrom
copilot/implement-feature-1-2-3-4-5a-6-7
Closed

perf: hot-path optimizations for enqueue!, poll!, and ack!#1
mpenet with Copilot wants to merge 1 commit into
mainfrom
copilot/implement-feature-1-2-3-4-5a-6-7

Conversation

Copilot AI commented Apr 13, 2026

Copy link
Copy Markdown

Implements items 1–7 from the performance improvement plan targeting the enqueue/poll/ack hot paths.

Changes

  • Branchless frame alignment — replace if (zero? r) with (bit-and (+ total frame-align-mask) (bit-not frame-align-mask))
  • Binary segment searchfind-segment-for-offset uses binary search instead of linear scan over the sorted segments vector
  • Cached current segmentQueue gains a ^:volatile-mutable ^Segment current-seg field, eliminating (peek @segments) atom deref + vector peek on every enqueue!/poll!
  • ByteBuffer.slice(index, length)try-read-one! uses the 2-arg slice (Java 13+), dropping the .duplicate + position/limit dance (one fewer allocation per message)
  • Fused lrbFrontier — single lrbFrontier(read-head) interface method replaces lrb-empty? + lrb-first two-dispatch sequence in ack!
  • Volatile cursor-dirty^:volatile-mutable ^boolean cursor-dirty replaces the atom (AtomicReference CAS) on every async ack
  • Segment pre-creation — at 75% segment fill, pre-creates the next segment file + mmap using a temp path; roll-segment! does an atomic rename and wraps the existing resources, avoiding file allocation/mmap latency on the roll-triggering write

Pre-creation design

enqueue! at 75% fill → precreate-next-segment!
  creates "seg-precreated.k7.tmp", opens FileChannel, mmaps

enqueue! triggers roll → roll-segment!
  Files/move(tmp → seg-{offset}.k7, ATOMIC_MOVE)
  wraps existing channel/mmap in new Segment with correct base-offset

close-queue! with unused pre-created segment
  close-segment! + Files/deleteIfExists

All 30 existing tests pass unchanged.

- Item 1: Segment pre-creation with 75% fill threshold
- Item 2: Branchless alignment in aligned-frame-size
- Item 3: Binary search in find-segment-for-offset
- Item 4: Cache current segment in volatile field on Queue
- Item 5: Use ByteBuffer.slice(index, length) in try-read-one!
- Item 6: Fused lrbFrontier method on ILongRingBuffer
- Item 7: Volatile boolean for cursor-dirty instead of atom

fix: address code review feedback - fix pre-creation base-offset, use integer threshold, clearer binary search

- Fix critical bug: pre-create segment with temp file + atomic rename instead of
  wrong base-offset. Pre-created segment now always usable at roll time.
- Replace float division threshold with integer arithmetic (capacity - capacity/4)
- Use bit-shift-right instead of unsigned-bit-shift-right in binary search
- Remove unused segment-precreate-fill-ratio constant

Agent-Logs-Url: https://github.com/mpenet/k7/sessions/cb0fbabb-c116-4070-bd98-89de2903848d

Co-authored-by: mpenet <106390+mpenet@users.noreply.github.com>

refactor: extract atomic-move-opts constant to avoid per-roll allocation

Agent-Logs-Url: https://github.com/mpenet/k7/sessions/cb0fbabb-c116-4070-bd98-89de2903848d

Co-authored-by: mpenet <106390+mpenet@users.noreply.github.com>
@mpenet
mpenet force-pushed the copilot/implement-feature-1-2-3-4-5a-6-7 branch from 3007e89 to d93c0c1 Compare April 13, 2026 07:18
@mpenet mpenet closed this Apr 13, 2026
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.

2 participants