In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the principle of sync.map in golang analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Several key points needed to understand the diagram var expunged = unsafe.Pointer (new (interface {}))
Type Map struct
Type readOnly struct
Type entry struct
Func (m * Map) Load (key interface {}) (value interface {}, ok bool)
Func (m * Map) Store (key, value interface {})
Func (m * Map) Delete (key interface {})
Func (m * Map) Range (f func (key, value interface {}) bool)
Here, we should focus on the numerical states of readOnly.amended, Map.misses and entry.p, which are used to judge the trend in the topology diagram.
Next, the code and comments of the structure are listed in detail to make it easy to read and understand the topology diagram.
Sync.Map main structure and comments type Map struct {
/ / Mutex lock, used to lock dirty map
Mu Mutex
/ / read map first and support atomic operations. ReadOnly in the comments does not mean that read is read-only, but its structure. Read actually has a write operation.
Read atomic.Value
/ / dirty is a current map that allows you to read and write
Dirty map [interface {}] * entry
/ / the main record is the number of times that read cannot read data and lock read map and dirty map. When misses is equal to the length of dirty, dirty will be copied to read.
Misses int
}
/ / readOnly is mainly used for storage, storing elements in Map.read through atomic operations.
Type readOnly struct {
/ / map of read, used to store all read data
M map [interface {}] * entry
/ / if the data is in dirty but not in read, the value is true as the modification ID
Amended bool
}
/ / entry is the specific map value of Map.dirty
Type entry struct {
/ / nil: indicates that it is deleted. Call Delete () to set the element in read map to nil.
/ / expunged: also means that the key is deleted, but the key is only in read, not in dirty. This occurs when read is copied to dirty, that is, the process of copying marks nil as expunged first, and then does not copy it to dirty.
/ / other: indicates that real data is stored
P unsafe.Pointer / / * interface {}
} the principle of sync.Map is very simple, using the space-for-time strategy, through two redundant data structures (read, dirty), to achieve the impact of locking on performance.
Read and write are separated into different map by introducing two map, where read map provides concurrent read and atomic write of existing elements, while dirty map is responsible for reading and writing.
In this way, the read map can read concurrently without locking. When no value is read in the read map, the lock is added for subsequent reading and the number of misses is accumulated.
When the number of misses is greater than or equal to the length of dirty map, raise dirty map to read map.
From the definition of the structure, we can see that although two map are introduced, the underlying data stores pointers and points to the same value. The above is all the contents of the article "Analysis of the principles of sync.map in golang". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.