Skip to content

sync: RWMutex.RLock is 2x slower than Mutex.Lock #38813

@gaurav1086

Description

@gaurav1086

What version of Go are you using (go version)?

go version go1.14 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/gaurav/.cache/go-build"
GOENV="/home/gaurav/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/gaurav/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build924554981=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Ran a benchmark with sync.Mutex vs sync.RWMutex and found the mutex.Lock() to be 2x faster than the RWMutex.RLock()

Description: Two scenarios (with lock and rwlock) with 1000 goroutines contending to read data from a slice (size = 10) and write to independent files.

https://play.golang.com/p/RZ-bE3OJ4H5

Output: Lock vs RWLocks() in seconds
Locks RWLocks
2.589610687s 5.52346426s
2.693844854s 6.13264221s
2.574230731s 6.857576762s

What did you expect to see?

Expected to see RWLock() to be atleast twice as fast than Lock()

What did you see instead?

Lock() is faster than RWLock() by almost 2X, then what's the point of having the RWMutex in the first place ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.PerformanceWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions