Skip to content

perf(lease-read): store lease expiry as atomic.Int64 UnixNano (deferred from #549) #554

@bootjp

Description

@bootjp

Issue

leaseState.expiry is atomic.Pointer[time.Time] (kv/lease_state.go). Every extend() allocates a new time.Time on the heap. Under heavy write load the per-Dispatch allocation rate is 1:1 with throughput — at 50k Dispatch/s, that is 50k allocs/s plus matching GC pressure. Performance reviewer flagged as HIGH on PR #549.

Fix

Store expiry as atomic.Int64 holding UnixNano(). valid() compares against time.Now().UnixNano(). Zero allocation, single 8-byte atomic.

Caveat

Loses Go's monotonic-clock comparison: time.Now().UnixNano() returns wall-clock nanoseconds. A backward NTP step could prematurely expire the lease (safe direction) or extend it (unsafe direction). Mitigation: pair with #issueX (CLOCK_MONOTONIC_RAW) or use runtime.nanotime() via //go:linkname.

Acceptance

  • BenchmarkLeaseStateExtend shows 0 allocs/op.
  • All existing lease tests pass.
  • Document the wall-clock dependency or close together with the monotonic-source decision.

References: PR #549 (deferred).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions