The data structure

Thread-safe: Thread-safe is memory safe. Threads share heap space and access to the same space can cause concurrency problems. Parts of go are thread-safe, but some are not.

Slicing: dynamic arrays. Slicing can be run as data,len,caps(Pointers to contiguous memory areas, length, and capacity). Capacity expansion causes new memory copy requests. A possible pitfall is that a slice is initialized with a large chunk of data that ends up being used very little, but that portion of memory cannot be reclaimed because a small portion is referenced. Create a new slice for the part used.

Map: hash, zipper method, bucket size is 8, load factor has exceeded 6.5, hash using too many overflow buckets will result in hash expansion, add more buckets, thread unsafe.

Sync. Map Thread-safe map,read,dirty Two maps,read is the number of times that the cache misses, and each value has expunged and nil. When the dirty, the misses, the misses+1. Specific implementation is complex, another article.

Channel: circular queue with two Pointers

compile

  1. Lexical analysis: Character flow into Token sequence, finite automaton form.
  2. Grammar: LALR(1), establish abstract syntax tree
  3. Type check: Static type check. Type information is introduced during the runtime. Dynamic type check is performed when the interface changes to a specific type. Handle OTARRAY check right node, handle hash check key value type, make branch check
  4. Intermediate code generation
  5. Machine code generation

Memory management

Fundamentals: Linear allocation, linked list allocation (first adaptation, optimal adaptation, etc.)

Memory allocator: according to the size of the application, it can be divided into micro objects (16B), small objects (32KB), large objects, and most objects below 32KB. Memory is divided into thread cache, central cache, and page heap. Over 32KB is allocated directly from the page heap, with small objects cached first by thread and then by center.

Heap space

  1. Version 1.10 continuous memory space

Spans the spans region stores Pointers to memory management cells runtime.mspan. Each cell spans several pages of memory, each 8KB.

Bitmaps are used to identify which addresses in the arena region hold objects, and each byte in the bitmap indicates whether 32 bytes in the heap are free;

The Arena area is the true heap area. The runtime treats 8KB as a page. These pages store all objects initialized on the heap.

  1. 1.11 Sparse Memory

The status of the address space

  1. None: Memory is not reserved or mapped
  2. Reserved is held by the runtime but not accessible
  3. Prepared memory is reserved, but there is generally no physical page
  4. Ready is securely accessible

More details will be covered in another article

GC

The root objects collected from the heap and goroutine are marked in gray, the references to marked gray objects are also marked in gray, all reference objects are marked in black, and the unmarked white is cleared when all are finished. The GC program is separate, so there is the problem of white objects being referenced and not being cleared because the program is running. The program stopped during the earlier GC, and now shields deleted and added nodes (marked gray).

Stack memory management

Escape analysis analyzes the pointer scope and determines where variables are allocated.

  1. Pointers to stack objects cannot be stored in the heap

  2. Pointers to stack objects cannot survive the collection of stack objects

  3. Construct a weighted directed graph, of which the vertices CMD/compile/internal/gc EscLocation said assigned variable, edge CMD/compile/internal/gc EscEdge said variable, the distribution of the relationship between the weight said addressing and address the number of times

  4. Traverse the object allocation graph and look for variable assignments that violate two invariants. If a variable on the heap refers to a variable on the stack, that variable needs to be allocated on the heap

  5. Enhances escape analysis of function parameters by recording the flow of data from function call parameters to the heap and return values

Continuous stack and segmented stack

csp

Channel: circular queue implementation, such as first out, communication between goroutines

Context: context, synchronization expiration time, cancel signal, data.