DO NOT USE sync.Map in Go!

  Рет қаралды 7,265

rwxrob

rwxrob

2 жыл бұрын

People never read past the first paragraph of the docs. So here is the rest (which says "never use sync.Map unless you are doing this one really obscure thing that most never do"):
The Map type is specialized. Most code should use a plain Go map
instead, with separate locking or coordination, for better type
safety and to make it easier to maintain other invariants along with
the map content.
The Map type is optimized for two common use cases: (1) when the
entry for a given key is only ever written once but read many times,
as in caches that only grow, or (2) when multiple goroutines read,
write, and overwrite entries for disjoint sets of keys. In these two
cases, use of a Map may significantly reduce lock contention
compared to a Go map paired with a separate Mutex or RWMutex.
Tags:
#golang #tips
Thanks sponsors, subs, and followers:
- GitHub: github.com/sponsors/rwxrob
- Twitch: / rwxrob
- Twitter: / rwxrob
- Discord: / discord
See rwxrob.tv/schedule for scheduled streams.
[ZETID:20211002170439]
github.com/rwxrob/zet/tree/ma...

Пікірлер: 12
@noof456
@noof456 2 жыл бұрын
sync.Map is perfectly fine for read-heavy workloads, it uses runtime techniques internally (such as processor pinning and thread-local storage) that aren't available to us. If you have a 90% read workload, sync.Map will almost always outperform a regular map wrapped with a sync.RWMutex due to the increased locking granularity (and less contention). I agree that it should never be someone's default choice, but I can think of many domains where read-heavy workloads (or even workloads where write performance isn't that important) would be a perfect match for sync.Map. If you just want the best overall performance, wrapping a map with a single sync.RWMutex is the way to go. And if you need more performance, you could always shard for increased lock granularity.
@rwxrob
@rwxrob 2 жыл бұрын
I've seen sync.Map abused to many times to not have made this, but yes, agreed. I'm inclined to try sync.Map with a Context to explore some read-only data passing.
@szymon3928
@szymon3928 2 жыл бұрын
Exactly what I was looking for, thanks!
@rastafadev
@rastafadev Жыл бұрын
Thanks !
@ivanstrelkov4202
@ivanstrelkov4202 2 жыл бұрын
I see you telling a lot about sync.Mutex for map. Why not sync.RWMutex? Is it something wrong with it?
@jeremygiaco
@jeremygiaco 9 ай бұрын
thinking the same. for gets ,i use rwmutex.rlock/runlock
@LemuriaGames
@LemuriaGames Жыл бұрын
is that code available for reference?
@remove1516
@remove1516 2 ай бұрын
man why you don't use rwmutex and why you check var has for returning zero value?
@DavidFregoli
@DavidFregoli Жыл бұрын
yeah I was surprised how useless this is for most cases. basically case A is "well why not just use a constant" and case B is "well why not just use multiple maps". of course it must have its place but you'll know it when you'll know it.
@marcusaurelius6607
@marcusaurelius6607 Жыл бұрын
This is so unprofessional, opinionated and misleading i don't even know where to start. To whoever is watching this: don't listen to this man. Cheers from google.
@andherium
@andherium 10 ай бұрын
If you could explain why this is misleading instead of blunt accusation then I could take you seriously. cheers from NOT Google.
@virtualriot4062
@virtualriot4062 Жыл бұрын
Спасибо за видео, согласен с вашим выводом. Вы действительно знаете русский язык?
A Practical Example How To Use Interfaces In Golang
14:42
Anthony GG
Рет қаралды 20 М.
Concurrency in Go
18:40
Jake Wright
Рет қаралды 608 М.
That's how money comes into our family
00:14
Mamasoboliha
Рет қаралды 9 МЛН
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 53 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 117 МЛН
How To Use The Context Package In Golang?
17:03
Anthony GG
Рет қаралды 55 М.
Go Map Internals / Егор Гришечко (Uber)
33:21
HighLoad Channel
Рет қаралды 4,5 М.
Improve Go Concurrency Performance With This Pattern
34:16
Kantan Coding
Рет қаралды 11 М.
How to Start a Golang Project Right
32:44
rwxrob
Рет қаралды 40 М.
How To Use Goroutines For Aggregating Data In Golang?!
17:15
Anthony GG
Рет қаралды 36 М.
Go (Golang) Fan-In Fan-Out Pattern
26:21
Golang Cafe
Рет қаралды 7 М.
Rust makes you feel like a GENIUS
10:48
No Boilerplate
Рет қаралды 401 М.
Как на самом деле устроен тип Map в Golang? | Golang под капотом
34:33
iPhone 16 с инновационным аккумулятором
0:45
ÉЖИ АКСЁНОВ
Рет қаралды 7 МЛН
Собери ПК и Получи 10,000₽
1:00
build monsters
Рет қаралды 2,4 МЛН
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,6 МЛН
Здесь упор в процессор
18:02
Рома, Просто Рома
Рет қаралды 247 М.
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 2,9 МЛН
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 3,3 МЛН