Skip to content

Commit

Permalink
Merge pull request #291 from scylladb/reduce_inflight_footprint
Browse files Browse the repository at this point in the history
fix(inflight): reduce memory footprint by using struct{} instead of bool
  • Loading branch information
dkropachev authored May 1, 2023
2 parents 70694a3 + 1002c2c commit 6eea6ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ check: check-golangci
# fix make all static code analysis tools to fix the issues
.PHONY: fix
fix: fix-golangci

.PHONY: test
test: go test -v -race ./...
17 changes: 7 additions & 10 deletions inflight/inflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func New() InFlight {

func newSyncU64set(limit uint64) *syncU64set {
return &syncU64set{
values: make(map[uint64]bool),
values: make(map[uint64]struct{}),
limit: limit,
deleted: 0,
lock: sync.RWMutex{},
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *shardedSyncU64set) AddIfNotPresent(v uint64) bool {
}

type syncU64set struct {
values map[uint64]bool
values map[uint64]struct{}
deleted uint64
limit uint64
lock sync.RWMutex
Expand All @@ -95,7 +95,7 @@ func (s *syncU64set) AddIfNotPresent(u uint64) bool {
if ok {
return false
}
s.values[u] = true
s.values[u] = struct{}{}
return true
}

Expand Down Expand Up @@ -127,17 +127,14 @@ func (s *syncU64set) addDeleted(n uint64) {
func (s *syncU64set) shrink() {
s.lock.Lock()
defer s.lock.Unlock()
var newValues map[uint64]bool
mapLen := uint64(0)
if uint64(len(s.values)) >= s.deleted {
newValues = make(map[uint64]bool, uint64(len(s.values))-s.deleted)
} else {
newValues = make(map[uint64]bool, 0)
mapLen = uint64(len(s.values)) - s.deleted
}
newValues := make(map[uint64]struct{}, mapLen)

for key, val := range s.values {
if val {
newValues[key] = val
}
newValues[key] = val
}
s.values = newValues
s.deleted = 0
Expand Down
20 changes: 20 additions & 0 deletions inflight/inflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"reflect"
"testing"
"testing/quick"
"time"
)

func TestAddIfNotPresent(t *testing.T) {
Expand Down Expand Up @@ -82,6 +83,25 @@ func TestInflight(t *testing.T) {
}
}

//go:norace
func TestAutoShrink(t *testing.T) {
t.Parallel()
flight := newSyncU64set(10)
for x := uint64(0); x < 11; x++ {
flight.AddIfNotPresent(x)
}
if len(flight.values) != 11 {
t.Fatal("expect 11 records in flight, but got ", len(flight.values))
}
for x := uint64(0); x < 11; x++ {
flight.Delete(x)
}
time.Sleep(time.Second / 2)
if flight.deleted != 0 {
t.Fatal("expect that shrink is been executed and deleted dropped back to 0, but got ", flight.deleted)
}
}

func TestInflightSharded(t *testing.T) {
t.Parallel()
flight := newShardedSyncU64set()
Expand Down

0 comments on commit 6eea6ba

Please sign in to comment.