Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The compilation Mystery behind String-- exploring data structure

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

String, I believe everyone is familiar with, we write programs, the use of String type is relatively large. So you use it often, do you really "know" it? Please take the question, step by step to lift its mysterious veil, see what it is "human" also!

I. Thinking

During Swift development using strings, have you thought about the following questions?

How much memory does a string variable take up?

What is the difference between str1 and str2?

If str1 and str2 are spliced, what happens to the underlying storage of str1 and str2?

If you can answer the above questions accurately, then you know the underlying storage mechanism of Swift strings.

How much memory does one string variable occupy? Method 1: MemoryLayout

First, you can test it with Swift's built-in MemoryLayout.

Method 2: Compilation

In addition, we can also use a powerful low-level analysis assistant-assembly language, to peek into the underlying storage of String

In fact, the analysis of other syntax, system library of the bottom layer, can be with the help of assembly language

For example, the principle of polymorphism, the principle of generics, the bottom layer of Array, the bottom layer of enumeration, and so on

In addition, not only Swift, C, C++, OC underlying analysis, can still rely on assembly language

After all, every valid line of code you write will eventually be converted into machine instructions (0 and 1), and machine instructions are one-to-one corresponding to assembly instructions. Each machine instruction can be translated into its corresponding assembly instruction. Being able to read assembly instructions is equivalent to being able to read machine instructions and know what the CPU is doing.(What registers are operated, which memory is operated) The code in this tutorial runs directly on the Mac CommandLineTools project. Therefore, the assembly code shown is based on the AT&T format assembly of X64, not the ARM assembly of the real iOS device. In fact, there are great similarities between different kinds of assemblies, except that some instructions are called differently.

Just like Microsoft Visual Studio, Xcode also has built-in very convenient disassembly function, which can easily view the assembly instructions corresponding to each code. The steps to open the disassembly interface are as follows

Break a line of code that needs debugging (the disassembly interface is displayed in breakpoint debugging state)

Debug > Debug Workflow > Always Show DisassemblyAssembly

Run the program and see the disassembly interface

If you are experienced in disassembly, you can deduce from the assembly on lines 16 and 17 that String takes 16 bytes.

Because it uses the rax, rdx registers to store the contents of the string str, and rax, rdx are 8 bytes.

The compilation content is too much, because of the time and space relationship, the article will not explain in detail each sentence of the compilation instruction, more is to explain the importance of the compilation.

Third, the underlying storage of strings snooping memory

Previously I wrote a widget that can snoop Swift variable memory: github.com/CoderMJLee/Mems

Now use it to see what data is stored in the 16 bytes of the string.

Mems.memStr(ofVal:) displays memory data in 8 byte groups by default

Transfer parameter alignment: .one is to display memory data in groups of 1 byte

ASCII values for characters '0' to '9' are 0x30 to 0x39. Look carefully at the original 16 bytes of str1. What do you find?

It stores the ASCII values of all characters directly in the 16 bytes of str1

0xa in the last byte 0xea is the number of characters, which is also a total of 10 characters

You can see that when str1 is spliced "ABCDE"

It ends up storing the ASCII values of the fifteen characters "0123456789ABCDE" in the 16 bytes of str1.

0xf in the last byte 0xef is the number of characters, also a total of 15 characters

As you can see, the current 16 bytes are full, so what if you splice another character?

You can see that the data stored in str1 has changed greatly. The ASCII value of each character is missing.

What exactly does the 16 bytes in there mean?

Where are the ASCII values for all characters ('0 'to' 9','A' to 'F')? other cases

What if at initial initialization (before concatenation), the contents of a string are more than 15 characters long?

I'm sure you'll guess that's the result.

None of these 16 bytes have ASCII values, and these 16 bytes are different from str1 on line 27, even though they all have the string content "0123456789ABCDEF."

If you splice str2,

It is not difficult to see that the 16 bytes of str2 have changed again, which is somewhat similar to str1 on line 27.

How to solve the above questions?

The above questions cannot be solved by looking at the printed memory data, but they can all be solved by using [!!!] Compilation!!!] To solve, analyze the compilation instructions, immediately draw conclusions, because the length of the article is limited, usually busy work, I recorded the detailed analysis process of the above problems into a video of more than 2 hours, interested friends can use 1.5~2 times the speed to watch

Link: pan.baidu.com/s/1AkS3K1ZKP8zyxhlhLRaBkA

Extraction code: kzrk

Video for friends who do not have a compilation basis, may be a bit difficult, it is best to pick a clear-headed time to watch

After watching the video, I hope you can feel the importance of assembly language exactly, and don't stay at the level of writing high-level language code and indulging in syntax sugar forever. IV. FINAL

Although assembly language is the basic language in programming, it is indeed the computer language we use most, and the application field is not only in your work, but also in daily life.

As a person who has been in the IT industry for several years, I have indeed accumulated a lot of experience and resources. I will also share them with you at ordinary times. If you want to get more free programming learning resources and dry goods, you can manually add WeChat: 19950277730! Come explore the world of programming with me!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report