cmd/compile: enable deadcode of unreferenced large global maps

This patch changes the compiler's pkg init machinery to pick out large
initialization assignments to global maps (e.g.

   var mymap = map[string]int{"foo":1, "bar":2, ... }

and extract the map init code into a separate outlined function, which is
then called from the main init function with a weak relocation:

   var mymap map[string]int   // KEEP reloc -> map.init.0

   func init() {
      map.init.0() // weak relocation

   func map.init.0() {
     mymap = map[string]int{"foo":1, "bar":2}

The map init outlining is done selectively (only in the case where the
RHS code exceeds a size limit of 20 IR nodes).

In order to ensure that a given map.init.NNN function is included when
its corresponding map is live, we add dummy R_KEEP relocation from the
map variable to the map init function.

This first patch includes the main compiler compiler changes, and with
the weak relocation addition disabled. Subsequent patch includes the
requred linker changes along with switching to the call to the
outlined routine to a weak relocation. See the later linker change for
associated compile time performance numbers.

Updates #2559.
Updates #36021.
Updates #14840.

Change-Id: I1fd6fd6397772be1ebd3eb397caf68ae9a3147e9
Run-TryBot: Than McIntosh <>
Reviewed-by: Cherry Mui <>
TryBot-Result: Gopher Robot <>
7 files changed
tree: 43aafa46b76ee6f8d1302475e4a331c83c6716ff
  1. .github/
  2. api/
  3. doc/
  4. lib/
  5. misc/
  6. src/
  7. test/
  8. .gitattributes
  9. .gitignore
  10. codereview.cfg
  12. go.env

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 4.0 Attributions license.

Our canonical Git repository is located at There is a mirror of the repository at

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at

After downloading a binary release, visit for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit for source installation instructions.


Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at

Note that the Go project uses the issue tracker for bug reports and proposals only. See for a list of places to ask questions about the Go language.