Art of Hacking Golang compiler

UPDATE 03/19/2018

CL and are committed and will be a part of Go 1.11.

UPDATE 06/25/2018

Found a bug Fixed by CL


map and append

When I just started to learn Golang, found this example:


You may see two likes[l] that are obviously not free if final machine code is doing it. There is no other way to write the same code without double lookup. I have decided to investigate if there is a Compiler optimization. In Go


is a built-in map type that implements a hash table You may generate Go’s Asm and find there are two map’s calls that look in my system like that:

go tool compile -S test_file.go
CALL runtime.mapaccess1_fast64(SB)
CALL runtime.mapassign_fast64(SB)

Ok, no surprise since this is not a “hidden cost” and you get what you see. But my next thought was – how does it work with Compound-Assignment Operators?

map and Compound-Assignment Operators

What is “Compound-Assignment Operators”?


This time I had to look at Go compiler code. Note that it is not hard because Golang compiler is written by Go! Eventually I found the code that performs ordering of nodes of Abstract Syntax Tree and it answered my question in comments


Well, this time it was unexpected that m[key] += x  becomes m[key] = m[key] + x internally.

This is obvious that the code can be optimized. You may see that some optimizations are also done by mutating AST tree. I decided to implement it.

AST mutations Before optimization


AST mutations required for optimization


Exception for /= and %=

This optimization fails on a test f6 at

m := map[int]int{}
func() { // wrapper to scope the defer.
defer func() {
var z int
m[0] /= z // Will panic. Shouldn't modify m.

if len(m) != 0 {
fmt.Printf("map insert happened, that is not expected\n", i)

In this case the original code works because it uses runtime.mapassign to read from map and it does not insert a new key in the hash map, instead it returns a default value for map’s value type.

Contributing to Golang

Configuring environment for contribution is mostly automated and the rest is documented very well


Changes committed at 03/12/2018 committed at 03/19/2018


Inside the Map Implementation

2 thoughts on “Art of Hacking Golang compiler

  1. Hi Iskander,

    Thank you for feedback. I really tried to keep this article short, mostly explained motivation and how this optimization can be achieved and then thought that code talks more.The review has comments and questions, even final implementation is not exactly the same as it was published initially.

    If you want to understand it and feel gaps, I am willing to address it. First look at the link where Go map design is explained It may close some holes. (btw., slide 39 mentions that m[k] += requires 2 map operations).

    Then send questions to particular part of article/code where you need more details. Also you can ask question in review itself even it’s submitted, I guess it still open for comments.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s