Skip to content

perf: Optimize statCache expiration timestamp by using int64 instead of time.Time#4857

Open
vedantdas-source wants to merge 7 commits into
masterfrom
timestamp-optimize
Open

perf: Optimize statCache expiration timestamp by using int64 instead of time.Time#4857
vedantdas-source wants to merge 7 commits into
masterfrom
timestamp-optimize

Conversation

@vedantdas-source

Copy link
Copy Markdown
Collaborator

Description

In the StatCache the entry struct tracks TTL expiration using a standard Go time.Time object. This time.Time struct consumes 24 bytes of memory which can be reduced to 8 bytes using int64 for the expiration timestamp. This saves 16 bytes of memory per object.

Changes Made

  • Changed expiration field in the internal cache entry from time.Time to int64
  • Cache insertion methods (Insert, InsertImplicitDir, AddNegativeEntry, etc.) now convert the incoming expiration time.Time parameter to seconds using .Unix()
  • Lookup methods now evaluate expiration against now.Unix()

Benchmarking results

Using time.Time :

goos: linux
goarch: amd64
pkg: github.com/googlecloudplatform/gcsfuse/v3/internal/cache/metadata
cpu: Intel(R) Xeon(R) CPU @ 2.60GHz
BenchmarkStatCache_Insert-48                     3033651               409.2 ns/op           272 B/op          4 allocs/op
BenchmarkStatCache_AddNegativeEntry-48           7874757               149.2 ns/op            80 B/op          2 allocs/op
BenchmarkStatCache_LookUp-48                    19632806                61.15 ns/op           17 B/op          0 allocs/op

Using int64 :

goos: linux
goarch: amd64
pkg: github.com/googlecloudplatform/gcsfuse/v3/internal/cache/metadata
cpu: Intel(R) Xeon(R) CPU @ 2.60GHz
BenchmarkStatCache_Insert-48                     3174098               382.8 ns/op           256 B/op          4 allocs/op
BenchmarkStatCache_AddNegativeEntry-48           8443263               141.3 ns/op            64 B/op          2 allocs/op
BenchmarkStatCache_LookUp-48                    22825635                54.30 ns/op           11 B/op          0 allocs/op

All existing tests pass successfully.

Link to the issue in case of a bug fix.

Testing details

  1. Manual - NA
  2. Unit tests - Added stat_cache_benchmark_test.go to measure memory and CPU performance of StatCache operations.
  3. Integration tests - NA

Any backward incompatible change? If so, please explain.

@vedantdas-source vedantdas-source requested a review from a team as a code owner July 9, 2026 08:57
@vedantdas-source vedantdas-source changed the title Timestamp optimize perf: Optimize statCache expiration timestamp by using int64 instead of time.Time Jul 9, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the entry struct in stat_cache.go by changing the expiration field from time.Time to int64 (using Unix timestamps) and adds a benchmark test suite to measure cache performance. The reviewer suggests using UnixMilli() instead of Unix() across all insertions and lookups to preserve sub-second precision while still maintaining the memory benefits of the int64 representation.

Comment thread internal/cache/metadata/stat_cache.go
Comment thread internal/cache/metadata/stat_cache.go
Comment thread internal/cache/metadata/stat_cache.go
Comment thread internal/cache/metadata/stat_cache.go
Comment thread internal/cache/metadata/stat_cache.go
Comment thread internal/cache/metadata/stat_cache.go
@github-actions github-actions Bot added the remind-reviewers Auto remind reviewers in attention set for review post 24hrs of inactivity on PR. label Jul 9, 2026
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 62.50000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.68%. Comparing base (b61c988) to head (f2acd33).

Files with missing lines Patch % Lines
internal/cache/metadata/stat_cache.go 62.50% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4857       +/-   ##
===========================================
+ Coverage        0   83.68%   +83.68%     
===========================================
  Files           0      170      +170     
  Lines           0    20958    +20958     
===========================================
+ Hits            0    17539    +17539     
- Misses          0     2762     +2762     
- Partials        0      657      +657     
Flag Coverage Δ
unittests 83.68% <62.50%> (?)

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

☔ View full report in Codecov by Harness.
📢 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.

Comment thread internal/cache/metadata/stat_cache.go
@vedantdas-source vedantdas-source added the execute-integration-tests Run only integration tests label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

execute-integration-tests Run only integration tests remind-reviewers Auto remind reviewers in attention set for review post 24hrs of inactivity on PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants