With a choice, there is always a choice difficulty, which is difficult How to choose the two? Whose performance is better? A friend of mine said that the standard library sync Map performance is very good. Standard library sync Map (go1.9 and later).Native map + mutex or read / write lock.The ok result indicates whether the value was found in the map.Before《 Why are go map and slice non thread safe?》In this paper, we discuss the non thread safety of map and slice in go language, and based on this, we extend the two most used concurrency support modes of map in the industry. Load returns the key value stored in the map, or nil if there is no value. Priority is given to reading, updating, and deleting from read, because the reading of read does not require a lock.Deleting a key value is just a mark, and the deleted data is cleaned only when the dirty is promoted. Dynamic adjustment, after more miss times, the dirty data is upgraded to read.Use read-only data (read) to avoid read-write conflicts.Two redundant data structures (read, dirty) are used to realize the effect of locking on performance. There is also the element that both the read map and the dirty map exist at the same time: the element in the read map is set to nil, because both the read map and the dirty map use element addresses, so they are both set to nil. The other is that the element has just been written into the dirty map and has not been upgraded to the read map: directly call the golang built-in function delete to delete the element of the dirty map If you need to delete Z, it needs to be divided into several situations:Ī read map has the element and read amended=false: directly set the element in read to nil. If the dirty map is directly upgraded to the read map after the misses reaches the threshold and the dirty map is empty map (read amended=false), the dirty map needs Copy data from the read map. The newly added element K=5, the newly added element needs to operate the dirty map. Now there is a need to modify the Z element Z=4, sync.Map will directly modify the elements of the read map. At this time, the read map and dirty map data are as follows. When misses>=len(dirty map), the dirty map is directly upgraded to read map, Here, the address is copied directly to the dirty map and the dirty map is cleared, and misses is set to 0. Miss records the number of failed reads from the read map. When reading data, read from the read map. At this time, the read map and dirty map data are as shown in the figure below. The dirty map mainly accepts write requests, and the read map has no data. From the definition of the previous structure, it can be found that although two maps are introduced, the underlying data is stored as a pointer, which points to the same value. When the number of misses is greater than or equal to the dirty map Length, the dirty map is raised to read map. When no value is read in the read map, lock is added for subsequent reads, and the number of misses is accumulated. In this way, the read map can read concurrently without locking. The read map provides concurrent reading and atomic writing of stored elements, while the dirty map is responsible for reading and writing. Separate the read and write into different maps by introducing two maps. It uses a space-for-time strategy, and realizes the impact of locking on performance through two redundant data structures (read, dirty). So does Golang's sync.Map also use the same principle? The principle of sync.Map is very simple. If you have been exposed to large Java, then you must have a deep memory of the principle that CocurrentHashMap uses lock segmentation technology to increase the number of locks, so that the number of threads competing for the same lock is controlled. Other: indicates that there is real data This situation occurs when copying read to dirty, that is, the process of copying will first mark nil as expunged, and then not copy it To dirty expunged: It also means that it is deleted, but the key is only in read and not in dirty. nil: means to be deleted, call Delete() to set the elements in the read map to nil
0 Comments
Leave a Reply. |