当前位置:网站首页>New solutions to go points (II) map solution
New solutions to go points (II) map solution
2022-07-20 15:20:00 【51CTO】
Review Prospects
In the last section , We left a code :
Then you can see the result of this output , And left a why , I don't know if youmu and friends have thought in place , Let's sum up this problem today .
map Structural analysis of
Our first contact GO When , Has been clearly informed ,go In language map It's a pointer , You have to use make After initialization, you can use , Let's pass map When , It's also about map This pointer to , Does not copy map Internal data content , So this map What is the structure of , This piece of , stay go Source code runtime\map.go You can peek into one or two , For this piece of source code analysis , There are also more detailed information available online .
–
But due to the Go A lot of things were done during compilation , For example, when compiling, it is based on map Type to generate the actual map structure , Fill in the data inside , This piece is actually done during compilation , The source code does not completely contain these , It's just a shell that can abstract all the data , therefore , The foundation is relatively weak , Friends without corresponding knowledge may look confused , It's over , May also be confused , for instance , I have said many times before ,go The string type of is actually a structure , that map What is the actual type .
–
Here's the right map Make a one-to-one decomposition , And the corresponding data structure , And the corresponding data types after compilation are decomposed one by one in the form of code .
map Actual type
map The format of is pointer , This is the first element , Then let's take the first step , Get it directly first ,map Content size of , This can be used unsafe.Sizeof To get to the .
We said earlier string It is actually a structure as follows
therefore , We get string What is the data length of 16, Then let's try map Of
Let's decide first ,mp Whether it is saved is a map Address value of , If it is an address value , Then it should be and uintptr Of the same size , Then we get this mp The actual address value of , If it's not initialized , Then this address must be empty , That is to say 0, then make after , There must be an address value , Through this code , We can directly determine , stay go In language , We wrote map What is stored in variables is actually map The address pointer of .
–
Get... On it map There is a trick in the actual address value of , It is by taking the address of the address , Then the result is deduced , So I got map Actual address value , because go The compiler of defines , It can't be like C,C++ And so on , Direct conversion , therefore , Only after getting the address , Use the address to cast , This is the advantage of pointer class , After obtaining the memory structure , The pointer doesn't care about the data form , Whatever you want him to be , It's just a piece of data in memory .
deconstruction map deconstruction hmap
combination runtime Medium map.go, We can know , actually map The structure of is hmap, So , actually , We are go It's written in the code map, Namely *hmap The pointer value of . Let's deconstruct it , It says so ,go Due to the limitation of the compiler, it cannot be directly cast , therefore , We have to get the address first , Then transfer through the address , that go In code map It's actually *hmap, So the first step is to get the address &mp What you get is actually the address of the address, that is **hmap, therefore , Then you can get the actual structure by solving the pointer , First , We will go Of runtime/map.go Medium hmap Copy in the relevant structure , Then transform and try
The transformation place inside , It's just bmap In structure , We have completed the types we need , The others haven't changed much
map Data storage structure and map The type structure of
map The essence of is actually a hash table , And the corresponding key Different , Hash functions are definitely different , meanwhile , Stored in the hash table key,value The structure of is certainly dynamic , however runtime Of map.go It just gives a general element storage structure bmap, And you can see the code above key yes string,value It's also string, So in runtime/map.go Of bmp On the basis of the structure of keys [8]string and values [8]string as well as overflow uintptr Several structures , This shows that in fact, this piece of data content is dynamically filled in during compilation , Detailed content , Don't elaborate. , There are corresponding instructions on the Internet , Just mark a little , If it's another type , Then the corresponding data types here are other data types , At the same time, for each map structure , They all have one mapType structure , Record the type structure of this hash table
This structure records key type , Element type , And hash functions and key size ,value size , Hash bucket size, etc
A query
This piece of , Basically, it's right key Conduct hash Calculation , Use after calculation low bits And high 8 position hash Find the location of the corresponding hash bucket , Then look in the bucket , This piece of map.go There is , You can directly move out the relevant code , That's it , The main code element here is to find this key Computed hash function , And the hash function is mapType It's recorded in , therefore , The most important thing is to find map Corresponding mapType, Give me the simplest way , Just use interface Make a transit , And then through interface You can get the structure type , We can write a simple query about a key The value code of is as follows
Break the question
Through the corresponding disassembly of the above series , Let's take a look at the first question is why
- First , Deposit to map When , In fact, I will calculate a key Of hash value
- By calculating the hash value , Then find the corresponding hash bucket
- Store the key value data into the hash bucket
And if we put a string that has been stored in the hash table key The data value of the address of has been changed , And then key I don't know that his value has changed , So the position of this key will not change at this time , It is still in the original hash bucket . Then if the original string is used at this time key visit , here hash The calculated result is consistent with the original result , So we can find the corresponding hash bucket , But after finding the hash bucket , Compare the elements in the hash bucket key When , Can't match , So I can't find it at this time . Then if you use the changed string key To visit map, At this time, if the calculated hash value is then found, the hash bucket is the same as the original hash bucket , Then we can find this new value , If the calculated hash bucket is different from the original hash bucket , Then you can't find this value . So I got the proof
Add a hash collision , Attach the code located in the same hash bucket
In order to further clarify the above explanation , Here we add a modification implementation method that can be located in the same hash bucket , therefore , Adjust the code , Plus a hash collision adjustment , In this code, different strings are collided into the same hash , Then search and locate .
边栏推荐
- 使用Masonry实现控件(包括UITableView)根据内容进行宽度自适应和高度自适应
- Comparison of eolink and JMeter interface tests
- && 运算符的使用
- Can't Oracle's $sqlarea find out which host is executing these SQL?
- JS class 并不只是简单的语法糖!
- Flink SQL configures Kafka to chain a topic with multiple partitions, and there is no error. There is no problem with a single partition
- Import word document pictures kernel synchronization and mutual exclusion
- 中信证券是国企嘛?安全嘛?
- C# 在Word中添加多行多列文字水印
- EXCRT/扩展中国剩余定理学习笔记
猜你喜欢
Feign 最佳实现方案探究
全球1000+研究员在Twitter直播训练超大模型??
机器学习(1)环境配置
组合数学学习笔记(未完待续
First knowledge of C language
Uncover the data enhancement potential of MAE, and Shanghai Communications & Huawei proposes masking reconstruction data enhancement based on MAE
C语言初识
Provincial selected and specially trained [haoi2009] caterpillar
省选专练之【PKUSC2018】主斗地
集合之Arraylist
随机推荐
C#/VB.NET 添加多行文本水印到Word文档
容斥【BZOJ3622】已经没有什么好害怕的了
25. Inheritance and derivation
机器学习基础篇(1)之照片视频显示,鼠标,控件操作
ACL 2022 | text to table: a new information extraction task
小程序技术解决桌面应用敏捷迭代的一种思路
省选专练之文艺计算姬
npm warn config global `--global`, `--local` are deprecated. use `--location 解决方法
C# 在Word中添加多行多列文字水印
Example | apicloud AVM framework encapsulates the cascade selection bullet frame of provinces and cities
中科方德系统适配产品(容器类)后谈
机器翻译做到头了?Meta开源NLLB翻译模型,支持200种语言互译
[mindspore] [read graph data] cannot read graph data in mindrecord format
Function recursion exercises (easy version)
中信证券是国企嘛?安全嘛?
unet和mask rcnn
软件测试如何快速入门
Southern Cass 10.1 software installation package download and installation tutorial
Love segment tree's good child [nine schools 2d1t3] beautiful sequence
Is there a function similar to ifnull in flinksql?