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

fix(inflight): reduce memory footprint by using struct{} instead of bool #291

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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