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

chore(pkg/database): use atomic for waitingCount in instrumentedRWMutex #1917

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arturmelanchyk
Copy link
Contributor

This change minimizes lock contention when working with instrumentedRWMutex
According to the attached benchmark Lock \ Unlock operations as well as RLock \ RUnlock gain ~50% performance boost while State's performance remains on the same level

@arturmelanchyk arturmelanchyk force-pushed the minimize-instrumented-mutex-lock-contention branch from 18f3d47 to 98de1f1 Compare January 31, 2024 16:23
imux.waitingCount++
imux.trwmutex.Unlock()
atomic.AddInt64(&imux.waitingCount, 1)
defer atomic.AddInt64(&imux.waitingCount, -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think that it makes sense to use defer here, just call after rlock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@tomekkolo
Copy link
Contributor

@arturmelanchyk , please sign commits with GPG

Signed-off-by: Artur Melanchyk <artur.melanchyk@gmail.com>
Signed-off-by: Artur Melanchyk <artur.melanchyk@gmail.com>
@arturmelanchyk arturmelanchyk force-pushed the minimize-instrumented-mutex-lock-contention branch from 2d77b7b to 56010cc Compare March 4, 2024 19:32
@arturmelanchyk
Copy link
Contributor Author

Just a kind reminder @tomekkolo @SimoneLazzaris , branch has been rebased onto latest master and all commits signed with GPG

@tomekkolo
Copy link
Contributor

Just a kind reminder @tomekkolo @SimoneLazzaris , branch has been rebased onto latest master and all commits signed with GPG

Looked at it once more, why using 64 bit ? If you follow the path where it is used (DatabaseHealth), it is anyway cut to 32bits at the end. And generally, no need for such big counter here. Also not sure about real gain, this endpoint is not called so much.

@arturmelanchyk
Copy link
Contributor Author

arturmelanchyk commented Mar 23, 2024

@tomekkolo

  1. As to the real gain, database heavily rely on this instrumentedRWMutex type, its used everywhere instead of regular sync.Mutex and sync.RWMutex
    type db struct {
    st *store.ImmuStore
    sqlEngine *sql.Engine
    documentEngine *document.Engine
    mutex *instrumentedRWMutex

see list of methods that use it:

Screenshot 2024-03-23 at 12 14 42

as a result, even tiny improvement in this type will lead to noticeable speed boost

  1. As to the size, instance of this type is only created once, so comparing 32bit to 64 bit we would only save 32bits once. Also take into consideration that in previous version this type was int which would anyway compile to 64 bits on most of the modern computers, so technically we do not add anything. Moreover, due to how golang aling structs in memory in this specific case it will anyway use 64bit, not 32, even if you change the type to 32bit

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.

None yet

2 participants