Translation GO Data structure

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Translation GO Data structure

http://mikespook.com/2013/12/%e7%bf%bb%e8%af%91go-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84/#more-1775

2013/12/05 Mikespook Post a reply

The memory structure of Go has been introduced in the Go memory model, but the content is relatively simple and many details are also around. Ross Cox's article on Go Data Structure is a very comprehensive piece of the comparison system. I hope it will be helpful to all of you.

The old article of the 2009 found that he had not finished translating. So the additions and modifications were made again. If I remember correctly, someone should have already published the same article in the OSC. Let's read it against the reference.

———— Translation Divider Line ————

GO Data structure

Whenever I introduce a novice to go, I find it helpful to see how go can allocate memory for its value in order to establish the correct idea of which operating cost is more expensive. This article describes the underlying types, structs, arrays, and slices (slice).

Basic type

Let's take a look at a few simple examples:

The i type of the variable is int a 32-bit word that is represented in memory. (All figures show a 32-bit memory structure; In the current implementation, only the pointer will be larger in the 64-bit architecture, int still 32 bits, but 64 bits may also be chosen as an alternative implementation.) )

Because of the explicit conversion, j the type of the variable is int32 . Although i j they have the same memory layout, they are different types: assignments i = j produce a type error, so you must explicitly convert: i = int(j) .

The type of the variable f is float that the current implementation is a 32-bit floating-point type. It has the same memory footprint int32 , but the internal layout is different.

struct and pointer

Next, the type of the variable bytes is [5]byte a 5-byte array. Its memory performance is the 5 bytes, and the same as the C array next to each other. Similarly, primes there is an int array of 4.

Go, which is closer to C than Java, provides programmers with the power to be pointers. For example, this type defines:

type Point struct{ X, Yint}

Defines a Point simple structure type called, which behaves as two contiguous in memory int .

The compound grammar statement Point{10, 20} pair Point was initialized. Taking an address to a compound grammar represents a pointer to the one that was just allocated and initialized Point . The former is two words in memory, and the latter is a pointer to two characters of memory.

The fields in the struct are arranged in memory one by one.

type Rect1 struct{ Min, Max Point }
type Rect2 struct{ Min, Max *Point }

Rect1, a struct with two Point fields, expressed as two, or four, in a row Point int . Rect2, a structure with two *point fields, expressed as two *point.

Programmers who have used C may not be surprised by the difference between the Point field and the *point field, and which only uses Java or Python (and other ...). ) may be surprised by the type of decision to use. By providing the programmer with basic memory layout control, Go provides the ability to control the overall size, number of allocations, and memory access patterns of a set of data structures. All are key to building a system that works well.

String

With this in front, we can continue to understand the more interesting types of data.

(A gray arrow indicates a pointer that exists in the implementation but cannot be seen directly in the program.) )

A string double-word structure in memory that contains a pointer to the string data and its length. Because string is immutable, it is safe to share the same storage space with multiple strings. Then if the pair s is cut, the slice makes it a new double-word struct, generating another pointer and length internally, but still pointing to the same sequence of bytes. This means that the slices can be done without any allocation and duplication, so the slices are as efficient as the specified ordinal round string.

(On the other hand, when slicing strings into smaller fragments in Java and other languages, there is a well-known problem, even if only a small fragment is used, the original reference will retain the entire original string in memory.) Go also has the same problem. We've tried but rejected an alternative to using allocation and replication, which makes it more expensive to slice strings, and most programs want to avoid this. )

Slice

A slice is a reference to a fragment of an array. In memory, it is a three-word structure that contains pointers to the first element, the length of the slice, and the capacity. The length is the x[i] upper bound of an index operation like this, and the capacity is the x[i:j] upper limit of such a slice operation.

As with a string slice, an array slice does not produce replication: it simply creates a new structure that holds different pointers, lengths, and capacities. In this example, the compound grammar []int{2, 3, 5, 7, 11} creates a new array with five values, and then sets the slice x field to describe the array. Slice expression X[1:3] No data is allocated: it is simply populated with a new slice structure that points to the same underlying storage. In the example, the length is 2, y[0] and y[1] is the only valid ordinal number, and the capacity is 4, which y[0:4] is a valid slice expression. (see effective Go for more information on slice length and capacity, and how to use it.) )

Since Slice is a multi-word structure, in the absence of pointers, the slicing operation does not need to allocate memory, even the slice header is not required, it is usually stored on the stack. This makes the use of slice as inexpensive as the cost of passing the specified pointer and length in C. Go initially takes slice as a pointer to the struct shown above, but this means that each slice operation allocates a new memory object. Even the use of quick allocations creates a lot of extra work for the garbage collector. We have found this situation, as mentioned earlier in the string section, in which case the program might avoid slicing operations and use the wheel finder. By removing these indirect and memory allocations, the cost of slice is low enough that in most cases there is no need to find a wheel.

New and make

Go has two data structure creation functions: new and make . Their differences may initially cause confusion, but they will soon feel normal. The basic difference is that a new(T) *T go program can implicitly discard a pointer (the black arrow in the image), but make(T, args) returns a primitive instead of a T pointer. Tthere is usually a pointer to its internal implicit implementation (the gray arrow in the figure). newreturns a pointer to a null-filled memory, and make returns a complex struct.

There is one way to unify these two situations, but it is possible to subvert the tradition from C and C + +: Define make (*t) to return a memory that points to the newly allocated T, then the current new point can be written as "*point". We've tried this for a few days, but feel that this is quite different from the memory allocation function that people usually want.

Coming soon

That's long enough. The interface values, map, and channel will only wait for future articles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.