Skip to content

cmd/compile: global vars initialized with func calls like net.IPv4 allocate at init time #51773

@mvdan

Description

@mvdan

See https://go-review.googlesource.com/c/go/+/391114, which replaces lines like:

var IPv4bcast = IPv4(255, 255, 255, 255) // limited broadcast

with:

var IPv4bcast = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 255, 255, 255, 255} // limited broadcast

The latter is certainly more verbose, which is the reason why the CL isn't submitted, but the former does allocate at init time as reported by GODEBUG=inittrace=1. Below are three samples before and after the 7 var changes:

init net @0.98 ms, 0.016 ms clock, 2840 bytes, 74 allocs
init net @1.0 ms, 0.017 ms clock, 3048 bytes, 75 allocs
init net @0.96 ms, 0.018 ms clock, 3048 bytes, 75 allocs

init net @1.1 ms, 0.020 ms clock, 2760 bytes, 67 allocs
init net @1.6 ms, 0.017 ms clock, 2760 bytes, 67 allocs
init net @0.98 ms, 0.018 ms clock, 2760 bytes, 67 allocs

The funcs in question are tiny, and they allocate fixed-size slices. As far as I can tell, they are getting inlined. I imagine it should be possible for the compiler to avoid the allocations even with the function calls.

cc @randall77 @josharian

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Triage Backlog

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions