Skip to content

perf(stat-cache memory): storing expiry nanoseconds (int64) instead of time.Time#4825

Closed
raj-prince wants to merge 1 commit into
masterfrom
time_mem_optimization
Closed

perf(stat-cache memory): storing expiry nanoseconds (int64) instead of time.Time#4825
raj-prince wants to merge 1 commit into
masterfrom
time_mem_optimization

Conversation

@raj-prince

@raj-prince raj-prince commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Description

  • Stat cache entries stores expiration time as time.Time struct (24 bytes), which is used to check expiry. This seems redundant and only storing the expirationNs-int64 (8 bytes) will be enough to check expiration. Saves 16bytes per entry.
  • Without change:
goos: linux
goarch: amd64
pkg: github.com/googlecloudplatform/gcsfuse/v3/internal/cache/metadata
cpu: AMD EPYC 7B13
BenchmarkStatCache_Insert-64                     1724638               749.4 ns/op           299 B/op          6 allocs/op
BenchmarkStatCache_AddNegativeEntry-64           1679168               677.1 ns/op           224 B/op          5 allocs/op
BenchmarkStatCache_LookUp-64                     5474361               214.5 ns/op            71 B/op          2 allocs/op
  • With change:
goos: linux
goarch: amd64
pkg: github.com/googlecloudplatform/gcsfuse/v3/internal/cache/metadata
cpu: AMD EPYC 7B13
BenchmarkStatCache_Insert-64                     1714723               745.1 ns/op           283 B/op          6 allocs/op
BenchmarkStatCache_AddNegativeEntry-64           2020422               647.2 ns/op           204 B/op          5 allocs/op
BenchmarkStatCache_LookUp-64                     6030410               195.4 ns/op            55 B/op          2 allocs/op

Link to the issue in case of a bug fix.

Testing details

  1. Manual - NA
  2. Unit tests - NA
  3. Integration tests - NA

Any backward incompatible change? If so, please explain.

@raj-prince raj-prince marked this pull request as ready for review June 30, 2026 07:31
@raj-prince raj-prince requested a review from a team as a code owner June 30, 2026 07:31
@raj-prince raj-prince requested a review from abhishek10004 June 30, 2026 07:31
@github-actions github-actions Bot added the remind-reviewers Auto remind reviewers in attention set for review post 24hrs of inactivity on PR. label Jun 30, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the memory usage of the stat cache by storing expiration times as int64 nanosecond timestamps rather than full time.Time objects. This change reduces the size of each cache entry and improves performance for insertion and lookup operations, as confirmed by the included benchmark results.

Highlights

  • Memory Optimization: Replaced the time.Time struct (24 bytes) with an int64 representing nanoseconds (8 bytes) in the stat cache entry structure to reduce memory footprint.
  • Performance Improvement: Updated expiration checks to use integer comparison instead of time.Time methods, resulting in improved latency and reduced allocations as demonstrated by the provided benchmarks.
  • Benchmarking: Added a new benchmark file to track and verify performance gains for insertion, negative entry addition, and lookup operations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@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 metadata stat cache by changing the expiration field in the cache entry from time.Time to an int64 nanosecond timestamp, which reduces memory overhead and improves comparison performance. It also introduces benchmark tests for the cache operations. The feedback suggests optimizing the lookup benchmark by pre-allocating the keys to avoid string allocation overhead inside the benchmark loop.

Comment on lines +75 to +83
now := time.Now()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
name := fmt.Sprintf("file-%d", i%10000)
sc.LookUp(name, now)
}

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.

medium

In BenchmarkStatCache_LookUp, calling fmt.Sprintf inside the benchmark loop introduces unnecessary allocations. Per repository guidelines, use strconv.AppendInt with a pre-allocated byte slice to build strings efficiently and minimize memory allocations.

	names := make([]string, 10000)
	buf := make([]byte, 0, 20)
	for i := 0; i < 10000; i++ {
		buf = append(buf[:0], "file-"...)
		buf = strconv.AppendInt(buf, int64(i), 10)
		names[i] = string(buf)
	}

	now := time.Now()

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		sc.LookUp(names[i%10000], now)
	}
References
  1. In performance-sensitive Go code, avoid string concatenation with the + operator. Instead, build the string using a pre-allocated byte slice and append functions (e.g., strconv.AppendInt) to minimize memory allocations.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.65%. Comparing base (37fe2eb) to head (8335885).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4825      +/-   ##
==========================================
- Coverage   83.68%   83.65%   -0.04%     
==========================================
  Files         168      168              
  Lines       20864    20864              
==========================================
- Hits        17461    17454       -7     
- Misses       2753     2758       +5     
- Partials      650      652       +2     
Flag Coverage Δ
unittests 83.65% <100.00%> (-0.04%) ⬇️

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.

@kislaykishore kislaykishore left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Gave some comments for the first method. Please incorporate them into other methods as well.

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Use b.Loop()

b.ReportAllocs()

for i := 0; i < b.N; i++ {
name := fmt.Sprintf("file-%d", i)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Using sprintf affects the benchmarking results. Prefer using strings.Builder with strconv for formatting integers.

expiration := time.Now().Add(time.Hour)

b.ResetTimer()
b.ReportAllocs()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why is this needed? You could achieve the same using benchmem

sc := metadata.NewStatCacheBucketView(lc, "")
expiration := time.Now().Add(time.Hour)

b.ResetTimer()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you use b.loop, this will not be needed.

expiration time.Time
m *gcs.MinObject
f *gcs.Folder
expirationNS int64

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need nanoseconds precision here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Our ttls are in seconds. Wdyt?

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

5 similar comments
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

Hi @abhishek10004, your feedback is needed to move this pull request forward. This automated reminder was triggered because there has been no activity for over 24 hours. Please provide your input when you have a moment. Thank you!

@raj-prince

Copy link
Copy Markdown
Collaborator Author

Closing in favor of - #4857

@raj-prince raj-prince closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants