Skip to content

Use System.Threading.Lock in ConcurrentLfu #697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged

Conversation

bitfaster
Copy link
Owner

Take the ConcurrentLfu lock optimization from #638.

Benchmark shows lock try enter is slightly faster:

Method Mean Error StdDev Ratio Allocated
UseMonitor 17.88 ns 0.235 ns 0.220 ns 1.00 -
UseLock 14.79 ns 0.315 ns 0.309 ns 0.83 -

@bitfaster bitfaster changed the title lock Use System.Threading.Lock in ConcurrentLfu Jun 30, 2025
@coveralls
Copy link

Coverage Status

coverage: 99.089% (-0.08%) from 99.169%
when pulling ae7ae9b on users/alexpeck/lock9
into fa14abd on main.

@bitfaster bitfaster requested a review from Copilot June 30, 2025 20:40
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes the LFU cache’s maintenance lock by using the new System.Threading.Lock API when targeting .NET 9+, and extends project targets to .NET 9.0.

  • Use Lock with TryEnter/Exit under NET9_0_OR_GREATER instead of Monitor.TryEnter
  • Update main project to target net9.0 and bump C# language version to 13.0
  • Extend benchmarks to run on net9.0

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
BitFaster.Caching/Lfu/ConcurrentLfuCore.cs Added conditional compilation to switch from Monitor locks to the new Lock API
BitFaster.Caching/BitFaster.Caching.csproj Added net9.0 target and updated <LangVersion> to 13.0, bumped validation baseline
BitFaster.Caching.Benchmarks/...Benchmarks.csproj Included net9.0 in benchmarks’ <TargetFrameworks>
Comments suppressed due to low confidence (1)

BitFaster.Caching/BitFaster.Caching.csproj:5

  • [nitpick] Bumping the language version to 13.0 might be ahead of official C# releases; consider using a supported preview or the latest stable version to ensure compatibility for all users.
    <LangVersion>13.0</LangVersion>

@bitfaster bitfaster merged commit eef629a into main Jun 30, 2025
16 checks passed
@bitfaster bitfaster deleted the users/alexpeck/lock9 branch June 30, 2025 20:43
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