Skip to content
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

Unnecessary contention in stats implementation #181

Open
diegommm opened this issue Jul 29, 2022 · 0 comments
Open

Unnecessary contention in stats implementation #181

diegommm opened this issue Jul 29, 2022 · 0 comments

Comments

@diegommm
Copy link

In file stats.go, all code paths accessing (*counter).prev are protected with a mutex, yet every time the field is being accessed with an atomic Load/Store operation.
These are the possible code paths accessing the field, in reverse order:

  • (*counter).prev > (*counter).value [ReadWrite] >
    • (*counter).report > (*scope).report, protected by (*scope).cm.RLock
    • (*counter).cachedReport > (*scope).cachedReport, protected by (*scope).cm.RLock
    • (*histogram).report > (*scope).report, protected by (*scope).hm.RLock
    • (*histogram).cachedReport > (*scope).cachedReport, protected by (*scope).hm.RLock
  • (*counter).prev > (*counter).snapshot [Read] >
    • (*scope).Snapshot, protected by (*scope).cm.RLock
    • (*histogram).snapshotValues > (*scope).Snapshot, protected by (*scope).hm.RLock
    • (*histogram).snapshotDurations > (*scope).Snapshot, protected by (*scope).hm.RLock

Note that:

  • A (*histogram) can have multiple (*counter) but only its methods access them, i.e. clients of (*histogram) only use its methods, not its fields.
  • There's no background forking in any of the methods, so e.g. (*scope).Snapshot would work sequentially inside the (R)Lock/(R)Unlock blocks of each mutex.

So (*counter).prev is always protected by a mutex, there's no need for atomic operations to access it.

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

No branches or pull requests

1 participant