Source file src/sync/map.go
1 // Copyright 2024 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sync 6 7 import ( 8 isync "internal/sync" 9 ) 10 11 // Map is like a Go map[any]any but is safe for concurrent use 12 // by multiple goroutines without additional locking or coordination. 13 // Loads, stores, and deletes run in amortized constant time. 14 // 15 // The Map type is specialized. Most code should use a plain Go map instead, 16 // with separate locking or coordination, for better type safety and to make it 17 // easier to maintain other invariants along with the map content. 18 // 19 // The Map type is optimized for two common use cases: (1) when the entry for a given 20 // key is only ever written once but read many times, as in caches that only grow, 21 // or (2) when multiple goroutines read, write, and overwrite entries for disjoint 22 // sets of keys. In these two cases, use of a Map may significantly reduce lock 23 // contention compared to a Go map paired with a separate [Mutex] or [RWMutex]. 24 // 25 // The zero Map is empty and ready for use. A Map must not be copied after first use. 26 // 27 // In the terminology of [the Go memory model], Map arranges that a write operation 28 // “synchronizes before” any read operation that observes the effect of the write, where 29 // read and write operations are defined as follows. 30 // [Map.Load], [Map.LoadAndDelete], [Map.LoadOrStore], [Map.Swap], [Map.CompareAndSwap], 31 // and [Map.CompareAndDelete] are read operations; 32 // [Map.Delete], [Map.LoadAndDelete], [Map.Store], and [Map.Swap] are write operations; 33 // [Map.LoadOrStore] is a write operation when it returns loaded set to false; 34 // [Map.CompareAndSwap] is a write operation when it returns swapped set to true; 35 // and [Map.CompareAndDelete] is a write operation when it returns deleted set to true. 36 // 37 // [the Go memory model]: https://go.dev/ref/mem 38 type Map struct { 39 _ noCopy 40 41 m isync.HashTrieMap[any, any] 42 } 43 44 // Load returns the value stored in the map for a key, or nil if no 45 // value is present. 46 // The ok result indicates whether value was found in the map. 47 func (m *Map) Load(key any) (value any, ok bool) { 48 return m.m.Load(key) 49 } 50 51 // Store sets the value for a key. 52 func (m *Map) Store(key, value any) { 53 m.m.Store(key, value) 54 } 55 56 // Clear deletes all the entries, resulting in an empty Map. 57 func (m *Map) Clear() { 58 m.m.Clear() 59 } 60 61 // LoadOrStore returns the existing value for the key if present. 62 // Otherwise, it stores and returns the given value. 63 // The loaded result is true if the value was loaded, false if stored. 64 func (m *Map) LoadOrStore(key, value any) (actual any, loaded bool) { 65 return m.m.LoadOrStore(key, value) 66 } 67 68 // LoadAndDelete deletes the value for a key, returning the previous value if any. 69 // The loaded result reports whether the key was present. 70 func (m *Map) LoadAndDelete(key any) (value any, loaded bool) { 71 return m.m.LoadAndDelete(key) 72 } 73 74 // Delete deletes the value for a key. 75 // If the key is not in the map, Delete does nothing. 76 func (m *Map) Delete(key any) { 77 m.m.Delete(key) 78 } 79 80 // Swap swaps the value for a key and returns the previous value if any. 81 // The loaded result reports whether the key was present. 82 func (m *Map) Swap(key, value any) (previous any, loaded bool) { 83 return m.m.Swap(key, value) 84 } 85 86 // CompareAndSwap swaps the old and new values for key 87 // if the value stored in the map is equal to old. 88 // The old value must be of a comparable type. 89 func (m *Map) CompareAndSwap(key, old, new any) (swapped bool) { 90 return m.m.CompareAndSwap(key, old, new) 91 } 92 93 // CompareAndDelete deletes the entry for key if its value is equal to old. 94 // The old value must be of a comparable type. 95 // 96 // If there is no current value for key in the map, CompareAndDelete 97 // returns false (even if the old value is the nil interface value). 98 func (m *Map) CompareAndDelete(key, old any) (deleted bool) { 99 return m.m.CompareAndDelete(key, old) 100 } 101 102 // Range calls f sequentially for each key and value present in the map. 103 // If f returns false, range stops the iteration. 104 // 105 // Range does not necessarily correspond to any consistent snapshot of the Map's 106 // contents: no key will be visited more than once, but if the value for any key 107 // is stored or deleted concurrently (including by f), Range may reflect any 108 // mapping for that key from any point during the Range call. Range does not 109 // block other methods on the receiver; even f itself may call any method on m. 110 // 111 // Range may be O(N) with the number of elements in the map even if f returns 112 // false after a constant number of calls. 113 func (m *Map) Range(f func(key, value any) bool) { 114 m.m.Range(f) 115 } 116