pointers/

directory
v0.0.0-...-b3f521c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2017 License: Apache-2.0

README

Pointers

Pointers provide a way to share data across function boundaries. Having the ability to share and reference data with a pointer provides flexbility. It also helps our programs minimize the amount of memory they need and can add some extra performance.

Notes

  • Use pointers to share data.
  • Values in Go are always pass by value.
  • "Value of", what's in the box. "Address of" ( & ), where is the box.
  • The (*) operator declares a pointer variable and the "Value that the pointer points to".

Garbage Collection

The design of the Go GC has changed over the years:

  • Go 1.0, Stop the world mark sweep collector based heavily on tcmalloc.
  • Go 1.2, Precise collector, wouldn't mistake big numbers (or big strings of text) for pointers.
  • Go 1.3, Fully precise tracking of all stack values.
  • Go 1.4, Mark and sweep now parallel, but still stop the world.
  • Go 1.5, New GC design, focusing on latency over throughput.
  • Go 1.6, GC improvements, handling larger heaps with lower latency.
  • Go 1.7, GC improvements, handling larger number of idle goroutines, substantial stack size fluctuation, or large package-level variables.
  • Go 1.8, GC improvements, collection pauses should be significantly shorter than they were in Go 1.7, usually under 100 microseconds and often as low as 10 microseconds.

figure1

STW : Stop The World Phase

Turn the Write Barrier on. The Write Barrier is a little function that inspects the write of pointers when the GC is running. Each goroutine must know this flag is set. This STW pause should be sub-millisecond.

Mark Phase

Find all the objects that can be reclaimed.

  • All objects on the heap are turned WHITE.
  • Scan Stacks : Find all the root objects and place them in the queue.
    • Pause the goroutine while scanning its stack.
    • All root objects found on the stack are turned GREY.
    • The stack is marked BLACK.
  • Mark Phase I : Pop a GREY object from the queue and scan it.
    • Turn the object BLACK.
    • If this BLACK object points to a WHITE object, the WHITE object is turned GREY and added to the queue.
    • The GC and the application are running concurrently.
    • Goroutines executing at this time will find their stack reverted back to GREY.
  • Mark Phase II - STW : Re-scan GREY stacks.
    • Re-scan all GREY stacks and root objects again.
    • Should be quick but large numbers of active goroutines can cause milliseconds of latency.
    • Call any finalizers on BLACK objects.

Sweep Phase

Sweep phase reclaims memory.

  • Left with either WHITE or BLACK objects. No more GREY objects.
  • WHITE objects are reclaimed while BLACK objects stay.

Write Barrier

The Write Barrier is a little function that inspects the write of pointers when the GC is running.

The Write Barrier is in place to prevent a situation where a BLACK object (one that is processed) suddenly finds itself pointing to a WHITE object after the Mark Phases are complete. This could happen if a goroutine changes (writes) a pointer inside a BLACK object to point to a WHITE object while both the program and the GC is running after that BLACK object has been processed. So the Write Barrier will make sure this write changes the object to BLACK so it's not swept away.

Pointers to the heap that exist on a stack can also be changed by goroutines when the GC is running. So stacks are also marked as BLACK once they are scanned and can revert back to GREY during Mark Phase I. A BLACK stack reverts back to GREY when its goroutine executes again. During Mark Phase II, the GC must re-scan GREY stacks to BLACKen them and finish marking any remaining heap pointers. Since it must ensure the stacks don't continue to change during this scan, the whole re-scan process happens with the world stopped.

Pacing

The GC starts a scan based on a feedback loop of information about the running program and the stress on the heap. It is the pacers job to make this decision. Once the decision is made to run, the amount of time the GC has to finish the scan is pre-determined. This time is based on the current size of the heap, the current live heap, and timing calculations about future heap usage while the GC is running.

The GC has a set of goroutines to perform the task of Mark and Sweep. The scheduler will provide these goroutines 25% of the available logical processor time. If your program is using 4 logical processors, that 1 entire logical processor will be given to the GC goroutines for exclusive use.

If the GC begins to believe that it can’t finish the collection within the decided amount of time, it will begin to recruit program goroutines to help. Those goroutines that are causing the slow down will be recruited to help.

Pointer Mechanics

https://golang.org/doc/effective_go.html#pointers_vs_values
http://www.goinggo.net/2013/07/understanding-pointers-and-memory.html
http://www.goinggo.net/2014/12/using-pointers-in-go.html

Stacks

Contiguous Stack Proposal

Escape Analysis and Inlining

Go Escape Analysis Flaws
Compiler Optimizations

Garbage Collection

The Garbage Collection Handbook
Tracing Garbage Collection
Go Blog - 1.5 GC
Go GC: Solving the Latency Problem
Concurrent garbage collection
Go 1.5 concurrent garbage collector pacing
Eliminating Stack Re-Scanning

Static Single Assignment Optimizations

GopherCon 2015: Ben Johnson - Static Code Analysis Using SSA
https://godoc.org/golang.org/x/tools/go/ssa
Understanding Compiler Optimization

Debugging code generation

Debugging code generation in Go

Code Review

Pass by Value (Go Playground)
Sharing data I (Go Playground)
Sharing data II (Go Playground)
Stack vs Heap (Go Playground)
Stack grow (Go Playground)

Exercises

Exercise 1

Part A Declare and initialize a variable of type int with the value of 20. Display the address of and value of the variable.

Part B Declare and initialize a pointer variable of type int that points to the last variable you just created. Display the address of , value of and the value that the pointer points to.

Template (Go Playground) | Answer (Go Playground)

Exercise 2

Declare a struct type and create a value of this type. Declare a function that can change the value of some field in this struct type. Display the value before and after the call to your function.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show the basic concept of pass by value.
Sample program to show the basic concept of pass by value.
Sample program to show the basic concept of using a pointer to share data.
Sample program to show the basic concept of using a pointer to share data.
Sample program to show the basic concept of using a pointer to share data.
Sample program to show the basic concept of using a pointer to share data.
Sample program to show variables stay on or escape from the stack.
Sample program to show variables stay on or escape from the stack.
Sample program to show how stacks grow/change.
Sample program to show how stacks grow/change.
exercises
exercise1
Declare and initialize a variable of type int with the value of 20.
Declare and initialize a variable of type int with the value of 20.
exercise2
Declare a struct type and create a value of this type.
Declare a struct type and create a value of this type.
template1
Declare and initialize a variable of type int with the value of 20.
Declare and initialize a variable of type int with the value of 20.
template2
Declare a struct type and create a value of this type.
Declare a struct type and create a value of this type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL