Skip to content

use Unix Nano timestamp for "now" in poolCommon#378

Merged
panjf2000 merged 1 commit intopanjf2000:devfrom
trace-andreason:dev
Feb 4, 2026
Merged

use Unix Nano timestamp for "now" in poolCommon#378
panjf2000 merged 1 commit intopanjf2000:devfrom
trace-andreason:dev

Conversation

@trace-andreason
Copy link
Contributor

The pool’s ticktock goroutine was storing time.Now() in an atomic.Value, which incurs interface boxing and shows up as runtime.convT in profiles. Switch to storing Unix nanos in an int64 with atomic.Load/StoreInt64, and reconstruct time.Time in nowTime(). This preserves behavior while removing the per‑tick allocation and conversion overhead, especially when many pools are created.

1. Are you opening this pull request for bug-fixs, optimizations or new feature?

This is an optimization.

2. Please describe how these code changes achieve your intention.

Each ants pool runs a ticktock() goroutine that periodically caches time.Now() in an atomic.Value. With many pools, the repeated interface boxing and atomic.Value store shows up as runtime.convT in CPU profiles and adds measurable overhead. By switching the cached value to a plain int64 holding Unix nanoseconds and using atomic.StoreInt64/LoadInt64, we keep the same cached‑time behavior for worker expiry but remove the conversion and allocation cost. This reduces per‑pool CPU overhead while preserving existing semantics for idle worker expiration.

3. Please link to the relevant issues (if any).

4. Which documentation changes (if any) need to be made/updated because of this PR?

4. Checklist

  • I have squashed all insignificant commits.
  • I have commented my code for explaining package types, values, functions, and non-obvious lines.
  • I have written unit tests and verified that all tests passes (if needed).
  • I have documented feature info on the README (only when this PR is adding a new feature).
  • (optional) I am willing to help maintain this change if there are issues with it later.

Copy link
Owner

@panjf2000 panjf2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

The pool’s ticktock goroutine was storing time.Now() in an atomic.Value,
which incurs interface boxing and shows up as runtime.convT in profiles.
Switch to storing Unix nanos in an int64 with atomic.Load/StoreInt64,
and reconstruct time.Time in nowTime(). This preserves behavior while
removing the per‑tick allocation and conversion overhead, especially
when many pools are created.

change name
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.03%. Comparing base (76ddb18) to head (fef4529).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #378   +/-   ##
=======================================
  Coverage   94.03%   94.03%           
=======================================
  Files          14       14           
  Lines         788      788           
=======================================
  Hits          741      741           
  Misses         35       35           
  Partials       12       12           
Flag Coverage Δ
unittests 94.03% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Owner

@panjf2000 panjf2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thx

@panjf2000 panjf2000 self-assigned this Feb 4, 2026
@panjf2000 panjf2000 added the pending merged This PR has been reviewed and approved label Feb 4, 2026
@panjf2000 panjf2000 merged commit 8a24625 into panjf2000:dev Feb 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending merged This PR has been reviewed and approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants