Go: Inside sync.Map — How does sync.Map work internally?

Contents

Introduction

A brief introduction to concurrency and how it applies in this context

The problem with using a map with sync.RWMutex

Source 1: RWMutexMap

Introducing sync.Map

Source 2: sync.Map methods documentation

Where is sync.Map used?

sync.Map: the implementation details

source 3: Map structure
Source 4: entry structure
Source 5: readOnly structure

How does load, store and delete work at a high level?

The difference between storing expunged and just simply nil

Store(key, value interface{}):

Source 6: the Store method
Source 7: the tryStore method
Source 8: dirtyLocked() — creates a new map by copying contents of the readable map and stores it in dirty
Source 9: tryExpungeLocked() — updates the entry’s pointer with expunged if it was nil

Load(key interface{}) (value interface{}, ok bool) :

Source 10: Load

LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)

LoadAndDelete(key interface{}) (value interface{}, loaded bool):

Delete()(value interface{}, ok bool):

Range(f func(key, value interface{}) bool)

Discussion: sync.Map performance vs RWMutex guarded map’s performance. A quick guide on optimization

What does an extreme level of optimization look like?

Source 11: super efficient sync map

Letter to the reader

--

--

I design and build scalable web apps. I’m also into building AI and ML solutions. Let’s connect to chat! https://www.linkedin.com/in/k-sreram-a04a90b7/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sreram K

I design and build scalable web apps. I’m also into building AI and ML solutions. Let’s connect to chat! https://www.linkedin.com/in/k-sreram-a04a90b7/