CodeBlanks Logo Icon CodeBlanks

Summary - Compiler

Combine SSA, escape and inline to write a compilation analysis.

Compiler summary

Source → type checking → IR (escape analysis + inlining) → SSA (constant folding, dead code, BCE) → machine code → linking. Your inspection tools: -gcflags=-m (escape and inlining), -gcflags=-S (assembly), GOSSAFUNC (SSA passes), -d=ssa/check_bce (bounds checks). With directives (//go:build, //go:embed, //go:noinline) you instruct the compiler.

Readability comes first

Compiler knowledge should be applied at the hot spots a profile shows. Adding BCE hints and noinline everywhere hurts readability and may become unnecessary in later Go versions, as the compiler gets smarter with each release. The rule: measure, find the hot spot, confirm the change with a benchmark.

Practice A buffer that stays on the stack

Write an allocation-light function using a fixed-size array: inside hex(b byte) string fill var buf [2]byte and return string(buf[:]). Print hex(255) (output ff).

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

const rakam = "0123456789abcdef"

func hex(b byte) string {
    var buf [2]byte
    buf[0] = rakam[b>>4]
    buf[1] = rakam[b&0x0f]
    return string(buf[:])
}

func main() {
    fmt.Println(hex(255))
}

Expected output

ff

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If a hot function allocates on the heap every call, which output do you check first?

  • go mod tidy
  • gofmt -d
  • go build -gcflags=-m Correct answer
  • go vet

Explanation: The -m output tells which variable escapes to the heap and why, with line numbers.

2 SSA visualization

Question
____=topla go build .
Answer
GOSSAFUNC=topla go build .

Explanation: GOSSAFUNC draws all SSA passes of the given function into ssa.html.

3 Group summary

Term Meaning
Escape analysis Stack or heap?
Inlining Replace call with body
SSA Optimization form
//go:build Conditional compilation

All lessons in Expert