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

Introduce Thread Safe Map Data Structure #11940

Merged
merged 10 commits into from
Feb 1, 2023
Merged

Conversation

rauljordan
Copy link
Contributor

This PR introduces a basic, thread-safe map data structure that is type-safe due to generics. It has a dead-simple API, and performance is very close to a concrete data structure. We should aim to use this anywhere where we create locks for the sole purpose of securing a lonely map in Prysm

Bench:

goos: linux
goarch: amd64
pkg: github.com/prysmaticlabs/prysm/v3/container/thread-safe
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkMap_Concrete-8            19357             62184 ns/op
BenchmarkMap_Generic-8             19231             64920 ns/op
PASS
func BenchmarkMap_Concrete(b *testing.B) {
	mm := &safeMap{
		items: make(map[int]string),
	}
	for i := 0; i < b.N; i++ {
		for j := 0; j < 1000; j++ {
			mm.Put(j, "foo")
			mm.Get(j)
			mm.Delete(j)
		}
	}
}

func BenchmarkMap_Generic(b *testing.B) {
	items := make(map[int]string)
	mm := NewThreadSafeMap(items)
	for i := 0; i < b.N; i++ {
		for j := 0; j < 1000; j++ {
			mm.Put(j, "foo")
			mm.Get(j)
			mm.Delete(j)
		}
	}
}

@rauljordan rauljordan requested a review from a team as a code owner January 31, 2023 02:00
defer m.lock.RUnlock()
r := make([]K, 0, len(m.items))
for k := range m.items {
r = append(r, k)
Copy link
Member

Choose a reason for hiding this comment

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

Depending on what type K is you might run into the for loop reference bug, where the reference changes with each iteration and in the slice. Since this is generic and to safeguard against all types, you should copy k before appending it to r

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let's just get rid of this method because there is no way of copying k generically unless it implements some Copy method

Copy link
Member

Choose a reason for hiding this comment

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

sorry not a deep copy, but just copy the value . Ex:

copiedK := k
r = append(r, copiedK)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed

container/thread-safe/map.go Outdated Show resolved Hide resolved
@prylabs-bulldozer prylabs-bulldozer bot merged commit b8bbeae into develop Feb 1, 2023
@delete-merged-branch delete-merged-branch bot deleted the thread-safe-map branch February 1, 2023 14:32
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.

2 participants