Fun with you compile a master string, faster car!

Strings or string (String) is a string of characters consisting of numbers, letters, the underscore. Is generally referred to as s = "a1a2 ··· an" ( n> = 0). It is a programming language indicates the data type of text. In programming, a string (string) is a symbol or a continuous numerical sequence, such as a symbol string (string of characters) or binary digit string (a string of binary digits).
String type you are no strangers, after all, every coder is from var str1 = "Hello World" over.
But it's really just so? Listen to me explain.
First, the thinking
in the Swift developed using the string in the process, whether you have thought about the following issues?
How much memory for a string variable?
String str1, str2 of the underlying storage What is the difference?
Fun with you compile a master string, faster car!
If the splicing operation str1, str2, str1, str2 underlying storage What will change?
Fun with you compile a master string, faster car!
If you can answer these questions accurately, it shows the underlying storage mechanism Swift string is quite aware of.
Second, how much memory a string variable occupation?
Method 1: MemoryLayout
First of all, can use Swift comes MemoryLayout to test
Fun with you compile a master string, faster car!
Method 2: Compilation
In addition, we can also analyze assistant With a strong bottom - assembly language, a peek at the underlying storage String of
fact, other grammatical analysis, the underlying system libraries, you can use assembly language
such as the underlying principle of polymorphism, generic principles, Array, the enumeration of the underlying etc.
in addition, not only Swift, C, C ++, OC underlying analysis, can still help assembly language
, after all, you write each line of code is valid, will eventually need to turn into machine instructions (0 and 1)
And the machine instruction is a one to one correspondence with the assembly instructions, each instruction can be translated into a machine assembly instructions corresponding thereto
can read assembler instructions, machine instructions can read equivalent, to know the specific CPU doing (operated What registers, operating a piece of memory)
code for this tutorial is directly run on the Mac command line (CommandLineTools) project
thus showing assembly code is based on the aT & T format X64 assembler, not iOS real machine equipment ARM assembler
in fact different species there are great similarities between the assembly, but not the same name for some commands
like Microsoft Visual Studio, Xcode has built a very convenient disassembly function, you can easily view each a code corresponding assembly instructions, open the anti step compilation interface follows
marked with a breakpoint in a row need to debug code (disassemble interface will show up in the breakpoint debugging state)
menu: debug> debug Workflow> Always show the disassembly
Assembly translated compilation, disassembly translated disassembly
Fun with you compile a master string, faster car!
operation program, see disassemble interface
Fun with you compile a master string, faster car!
if you disassemble the full experience, under subsection Compilation lines 16 and 17 will be weighing out, String occupancy is 16 bytes
because it took rax, rdx register holds the contents of the string str, and rax, rdx are 8-byte
compilation of content too much, because of the time and length of the relationship, the article will not have every one of the assembly instructions explain in detail, more is to illustrate the importance of compilation.
Third, the strings underlying storage
snoop memory
after I wrote a variable memory can spy gadgets Swift:https://github.com/CoderMJLee/Mems
now use it to pry the 16-byte string which, exactly what data is stored
Mems.memStr (ofVal :) default according to a set of 8 bytes of memory to display case data
transfer parameters alignment: .one in accordance with a set of 1-byte memory data to display
Fun with you compile a master string, faster car!
the character '0' - '9' ASCII value 0x30 ~ 0x39, str1 carefully observe the initial 16 bytes of data, what you find ?
It directly to the ASCII value is stored in 16 bytes of all the characters in str1
is the number of characters in the last 1 byte 0xea in 0xa, but also a total of 10 characters
stitching
Fun with you compile a master string, faster car!
can be found on the str1 when stitching "ABCDE" of
it the final is the ASCII value "0123456789ABCDE" fifteen characters are stored in 16 bytes of str1
is the number of characters in the last 1 byte 0xef in 0xf, but also a total of 15 characters
can be seen, the current 16 byte is full already, that if a character and then stitching it?
Fun with you compile a master string, faster car!
You can see, the data stored str1 which took place very big change, ASCII value of each character's gone,
that there is a 16-byte What exactly is the meaning of it?
All characters ( '0' ~ '9' , 'A' to 'F' ) ASCII value and keep it where to go?
Other situations
If you start initialization time (not before stitching), the contents of the string is more than 15 characters?
Fun with you compile a master string, faster car!
I believe you can guess the result
This 16-byte ASCII value of any character which did not occur
and that with 16 bytes of str1 line 27 is differentiated
though their string content is "0123456789ABCDEF"
If str2 splicing operation
Fun with you compile a master string, faster car!
is not difficult It found: At this time str2 16-byte changed again, with str1 line 27 is somewhat similar to
how to solve these questions?
Above these questions, look at the print out of the data memory can not be solved, but can use [! ! ! compilation! ! ! ] To solve, analyze assembly instructions, immediately came to the conclusion, because of the limited space of the article, usually too busy working, I put a detailed analysis of the process of recording the issue has become of up to over two hours of video, interested friends you can use 1.5 to 2 times the speed of viewing
links: https://pan.baidu.com/s/1AkS3K1ZKP8zyxhlhLRaBkA
extraction code: kzrk
video compilation no basis for a friend, it may be a bit difficult, it is best to pick a sober time to watch
after watching the video, I hope you can feel exactly the importance of language compilation, do not always stop at the code written in high-level language, indulge in syntactic sugar level.
Fourth, and finally
people often think assembly language application range is very small, its importance and neglect. In fact, the assembly language for each person want to learn computer science and technology are very important, and that can not fail to learn the language.
Compilation give the value you bring far more than the bottom of this article said snoop string, program impact on your career is definitely a lifetime of (data structures and algorithms, too). After compilation learn, when you use high-level language programming would be more understanding of the machine in the end do what you can to improve the algorithm can improve efficiency by modifying the high-level language code.
Development of IT technology with each passing day, new technologies, has a good ability to learn, to acquire new knowledge in a timely manner, at any time to supplement and enrich themselves, has become the core competitiveness of professional development programmers.
As a programmer, but also we need to constantly enrich their knowledge base. We know something, like a white circle outside of the circle it is a dark unknown world. When the more the larger the circle, the dark portion to the contact. We only keep learning, in order to break more darkness, find out more light.
If you want to improve yourself, learn more about programming skills such as iOS, data structures and algorithms, there are free study materials, please add micro letter: 19950277730 for more technical upgrade cheats. It is not only small like-minded partners, more numerous free programming skills, learning videos and information, plus micro-channel technology to learn to discuss it! !

Guess you like

Origin blog.51cto.com/14606540/2448956
car
Recommended