Skip to content

Commit

Permalink
Adding SMutex128
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jun 23, 2021
0 parents commit cdc5742
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [kelindar]
Binary file added .github/logo.pdn
Binary file not shown.
Binary file added .github/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Test
on: [push, pull_request]
env:
GITHUB_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
GO111MODULE: "on"
jobs:
test:
name: Test with Coverage
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: "1.16"
- name: Check out code
uses: actions/checkout@v2
- name: Install dependencies
run: |
go mod download
- name: Run Unit Tests
run: |
go test -race -covermode atomic -coverprofile=profile.cov ./...
- name: Upload Coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Roman Atachiants

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<p align="center">
<img width="330" height="110" src=".github/logo.png" border="0" alt="kelindar/smutex">
<br>
<img src="https://img.shields.io/github/go-mod/go-version/kelindar/smutex" alt="Go Version">
<a href="https://pkg.go.dev/github.com/kelindar/smutex"><img src="https://pkg.go.dev/badge/github.com/kelindar/smutex" alt="PkgGoDev"></a>
<a href="https://goreportcard.com/report/github.com/kelindar/smutex"><img src="https://goreportcard.com/badge/github.com/kelindar/smutex" alt="Go Report Card"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="License"></a>
<a href="https://coveralls.io/github/kelindar/smutex"><img src="https://coveralls.io/repos/github/kelindar/smutex/badge.svg" alt="Coverage"></a>
</p>


# Sharded Mutex in Go

This package contains a sharded mutex which *should* do better than a traditional `sync.RWMutex` in certain cases where you want to protect resources that are well distributed. For example, you can use this to protect a hash table as keys have no relation to each other. That being said, for the hash table use-case you should probably use `sync.Map`.

The `SMutex128` works by actually creating 128 `sync.RWMutex` and providing `Lock()`, `Unlock()` methods that accept a shard argument. A shard argument can overflow the actual number of shards, and mutex uses a modulus operation to wrap around.

## Benchmarks

```
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkLock/single/procs=64-8 64495 184600 ns/op
BenchmarkLock/single/procs=256-8 75350 161236 ns/op
BenchmarkLock/single/procs=1024-8 85765 161982 ns/op
BenchmarkLock/single/procs=4096-8 86328 160925 ns/op
BenchmarkLock/single/procs=16384-8 85803 153741 ns/op
BenchmarkLock/single/procs=65536-8 85806 152246 ns/op
BenchmarkLock/sharded/procs=64-8 342633 35435 ns/op
BenchmarkLock/sharded/procs=256-8 390313 30818 ns/op
BenchmarkLock/sharded/procs=1024-8 416959 30493 ns/op
BenchmarkLock/sharded/procs=4096-8 443528 30246 ns/op
BenchmarkLock/sharded/procs=16384-8 427383 30118 ns/op
BenchmarkLock/sharded/procs=65536-8 451612 30922 ns/op
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/kelindar/smutex

go 1.16

require github.com/stretchr/testify v1.7.0
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 40 additions & 0 deletions smutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

package smutex

import "sync"

const shards = 128

// SMutex128 represents a sharded RWMutex that supports finer-granularity concurrency
// contron hence reducing potential contention.
type SMutex128 struct {
mu [shards]struct {
sync.RWMutex
_ [40]byte // Padding to prevent false sharing
}
}

// Lock locks rw for writing. If the lock is already locked for reading or writing,
// then Lock blocks until the lock is available.
func (rw *SMutex128) Lock(shard uint) {
rw.mu[shard%shards].Lock()
}

// Unlock unlocks rw for writing. It is a run-time error if rw is not locked for
// writing on entry to Unlock.
func (rw *SMutex128) Unlock(shard uint) {
rw.mu[shard%shards].Unlock()
}

// RLock locks rw for reading. It should not be used for recursive read locking; a
// blocked Lock call excludes new readers from acquiring the lock.
func (rw *SMutex128) RLock(shard uint) {
rw.mu[shard%shards].RLock()
}

// RUnlock undoes a single RLock call and does not affect other simultaneous readers.
func (rw *SMutex128) RUnlock(shard uint) {
rw.mu[shard%shards].RUnlock()
}
161 changes: 161 additions & 0 deletions smutex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

package smutex

import (
"fmt"
"math/rand"
"runtime"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

// Store represents a concurrent store for testing
type Store interface {
Set(int64, string)
Get(int64) string
}

func BenchmarkLock(b *testing.B) {
size := int64(10000000)

single := newLocked()
for i := int64(64); i <= (1 << 16); i *= 4 {
runBenchmark(b, "single", single, size, i)
}

sharded := newSharded()
for i := int64(64); i <= (1 << 16); i *= 4 {
runBenchmark(b, "sharded", sharded, size, i)
}
}

func runBenchmark(b *testing.B, name string, store Store, size, procs int64) {
rand.Seed(1)
b.Run(fmt.Sprintf("%v/procs=%v", name, procs), func(b *testing.B) {
b.SetParallelism(int(procs))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
for i := 0; i < 5; i++ {
store.Set(rand.Int63n(size), "value")
}
for i := 0; i < 5; i++ {
store.Get(rand.Int63n(size))
}
}
})
})
}

func TestMutex(t *testing.T) {
var mu SMutex128
var wg sync.WaitGroup
var resource, out string

// Acquire a write lock
mu.Lock(1)

// Concurrently, start a reader
wg.Add(1)
go func() {
mu.RLock(1)
defer mu.RUnlock(1)
out = resource
wg.Done()
}()

// Write the resource
resource = "hello"
mu.Unlock(1)

// Wait for the reader to finish
wg.Wait()
assert.Equal(t, "hello", out)
}

// --------------------------- Locked Map ----------------------------

const work = 1000

// An implementation of a locked map using a mutex
type lockedMap struct {
mu sync.RWMutex
data map[int64]string
}

func newLocked() *lockedMap {
return &lockedMap{data: make(map[int64]string)}
}

// Set sets the value into a locked map
func (l *lockedMap) Set(k int64, v string) {
l.mu.Lock()
for i := 0; i < work; i++ {
l.data[k] = v
}
runtime.Gosched()
for i := 0; i < work; i++ {
l.data[k] = v
}
l.mu.Unlock()
}

// Get gets a value from a locked map
func (l *lockedMap) Get(k int64) (v string) {
l.mu.RLock()
for i := 0; i < work; i++ {
v, _ = l.data[k]
}
runtime.Gosched()
for i := 0; i < work; i++ {
v, _ = l.data[k]
}
l.mu.RUnlock()
return
}

// --------------------------- Sharded Map ----------------------------

// An implementation of a locked map using a smutex
type shardedMap struct {
mu SMutex128
data []map[int64]string
}

func newSharded() *shardedMap {
m := &shardedMap{}
for i := 0; i < shards; i++ {
m.data = append(m.data, map[int64]string{})
}
return m
}

// Set sets the value into a locked map
func (l *shardedMap) Set(k int64, v string) {
l.mu.Lock(uint(k))
for i := 0; i < work; i++ {
l.data[k%shards][k] = v
}
runtime.Gosched()
for i := 0; i < work; i++ {
l.data[k%shards][k] = v
}
l.mu.Unlock(uint(k))
}

// Get gets a value from a locked map
func (l *shardedMap) Get(k int64) (v string) {
l.mu.RLock(uint(k))
for i := 0; i < work; i++ {
v, _ = l.data[k%shards][k]
}
runtime.Gosched()
for i := 0; i < work; i++ {
v, _ = l.data[k%shards][k]
}
l.mu.RUnlock(uint(k))
return
}

0 comments on commit cdc5742

Please sign in to comment.