1/9/2024 0 Comments Gosync 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. Change the element status from expunged to nil.//nil: means to be deleted, call Delete() to set the elements in the read map to nil.It will perform the following operations. If it is found that the element exists in read, but it has been marked as expunged, it means that dirty is not equal to nil (the element definitely does not exist in dirty).It is divided into the following three processing branches: Since we have reached the dirty process, we directly called the mutex lock Lock method at the beginning to ensure data security and highlight the first act performance of 16153f2dcb8970 deteriorated. Let's verify it together.įirst we define the basic data structure: // 代表互斥锁įooMap = &FooMap) Performance TestingĪfter listening to the official documents introduced a bunch of benefits, he did not talk about the disadvantages, whether the said advantages after performance optimization are true and credible. In these two cases, compared with Go map with a separate Mutex or RWMutex, using the Map type can greatly reduce lock contention. When multiple goroutines read, write, and overwrite entries of irrelevant key sets.For example, in a cache that only grows, there will be such business scenarios. When an entry for a given key is written only once but read multiple times.Most code should use native maps instead of separate locking or coordinated control for better type safety and maintainability.Īt the same time, the Map type has also been optimized for the following scenarios:.The concurrent use of multiple goroutines is safe and does not require additional locking or coordinated control.Some suggestions for the Map type are clearly pointed out in the official Go documentation: Let's happily start the road of fish-sucking together. Then, according to the results of the performance analysis of each map, the source code of sync.map is dissected to understand WHY. We will first understand what scenarios, how to use the various types of Go map, and who has the best performance! Today, Fried Fish will take you to reveal the secret of Go sync.map. two types of 16153f2dcb7910, and who has the better ? 16153f2dcb7914? I have a friend who said that the standard library sync.Map has very good performance, don't use it. With choice, there is always a choice problem. Standard library sync.Map (Go1.9 and later).Native map + mutex lock or read-write lock mutex.Based on this, we derived the two modes of map with the most concurrency support currently used in the industry. In the previous " why are Go maps and slices not thread-safe?" "16153f2dcb77c9" article, we discussed the non-thread-safety issues of map and slice in Go language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |