1
0
mirror of https://github.com/golang/go synced 2024-11-05 20:26:13 -07:00
go/test/initialize.go

106 lines
2.2 KiB
Go
Raw Normal View History

// run
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test initialization of package-level variables.
package main
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
import (
"fmt"
"reflect"
)
type S struct {
A, B, C, X, Y, Z int
}
type T struct {
S
}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a1 = S{0, 0, 0, 1, 2, 3}
var b1 = S{X: 1, Z: 3, Y: 2}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a2 = S{0, 0, 0, 0, 0, 0}
var b2 = S{}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a3 = T{S{1, 2, 3, 0, 0, 0}}
var b3 = T{S: S{A: 1, B: 2, C: 3}}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a4 = &[16]byte{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
var b4 = &[16]byte{4: 1, 1, 1, 1, 12: 1, 1}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a5 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
var b5 = &[16]byte{1, 4: 1, 1, 1, 1, 12: 1, 1}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var a6 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
var b6 = &[...]byte{1, 4: 1, 1, 1, 1, 12: 1, 1, 0, 0}
func f7(ch chan int) [2]chan int { return [2]chan int{ch, ch} }
var a7 = f7(make(chan int))
func f8(m map[string]string) [2]map[string]string { return [2]map[string]string{m, m} }
func m8(m [2]map[string]string) string {
m[0]["def"] = "ghi"
return m[1]["def"]
}
var a8 = f8(make(map[string]string))
var a9 = f8(map[string]string{"abc": "def"})
func f10(s *S) [2]*S { return [2]*S{s, s} }
var a10 = f10(new(S))
var a11 = f10(&S{X: 1})
func f12(b []byte) [2][]byte { return [2][]byte{b, b} }
var a12 = f12([]byte("hello"))
var a13 = f12([]byte{1, 2, 3})
var a14 = f12(make([]byte, 1))
func f15(b []rune) [2][]rune { return [2][]rune{b, b} }
var a15 = f15([]rune("hello"))
var a16 = f15([]rune{1, 2, 3})
type Same struct {
a, b interface{}
}
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
var same = []Same{
{a1, b1},
{a2, b2},
{a3, b3},
{a4, b4},
{a5, b5},
{a6, b6},
{a7[0] == a7[1], true},
{m8(a8) == "ghi", true},
{m8(a9) == "ghi", true},
{a10[0] == a10[1], true},
{a11[0] == a11[1], true},
{&a12[0][0] == &a12[1][0], true},
{&a13[0][0] == &a13[1][0], true},
{&a14[0][0] == &a14[1][0], true},
{&a15[0][0] == &a15[1][0], true},
{&a16[0][0] == &a16[1][0], true},
}
func main() {
ok := true
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
for i, s := range same {
if !reflect.DeepEqual(s.a, s.b) {
ok = false
cmd/compile: handle simple inlined calls in staticinit Global variable initializers like var myErr error = &myError{"msg"} have been converted to statically initialized data from the earliest days of Go: there is no init-time execution or allocation for that line of code. But if the expression is moved into an inlinable function, the static initialization no longer happens. That is, this code has always executed and allocated at init time, even after we added inlining to the compiler, which should in theory make this code equivalent to the original: func NewError(s string) error { return &myError{s} } var myErr2 = NewError("msg") This CL makes the static initialization rewriter understand inlined functions consisting of a single return statement, like in this example, so that myErr2 can be implemented as statically initialized data too, just like myErr, with no init-time execution or allocation. A real example of code that benefits from this rewrite is all globally declared errors created with errors.New, like package io var EOF = errors.New("EOF") Package io no longer has to allocate and initialize EOF each time a program starts. Another example of code that benefits is any globally declared godebug setting (using the API from CL 449504), like package http var http2server = godebug.New("http2server") These are no longer allocated and initialized at program startup either. The list of functions that are inlined into static initializers when compiling std and cmd (along with how many times each occurs) is: cmd/compile/internal/ssa.StringToAux (3) cmd/compile/internal/walk.mkmapnames (4) errors.New (360) go/ast.NewIdent (1) go/constant.MakeBool (4) go/constant.MakeInt64 (3) image.NewUniform (4) image/color.ModelFunc (11) internal/godebug.New (12) vendor/golang.org/x/text/unicode/bidi.newBidiTrie (1) vendor/golang.org/x/text/unicode/norm.newNfcTrie (1) vendor/golang.org/x/text/unicode/norm.newNfkcTrie (1) For the cmd/go binary, this CL cuts the number of init-time allocations from about 1920 to about 1620 (a 15% reduction). The total executable code footprint of init functions is reduced by 24kB, from 137kB to 113kB (an 18% reduction). The overall binary size is reduced by 45kB, from 15.335MB to 15.290MB (a 0.3% reduction). (The binary size savings is larger than the executable code savings because every byte of executable code also requires corresponding runtime tables for unwinding, source-line mapping, and so on.) Also merge test/sinit_run.go, which had stopped testing anything at all as of CL 161337 (Feb 2019) and initempty.go into a new test noinit.go. Fixes #30820. Change-Id: I52f7275b1ac2a0a32e22c29f9095071c7b1fac20 Reviewed-on: https://go-review.googlesource.com/c/go/+/450136 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Joedian Reid <joedian@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Auto-Submit: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-11-13 07:22:35 -07:00
fmt.Printf("#%d not same: %v and %v\n", i+1, s.a, s.b)
}
}
if !ok {
fmt.Println("BUG: test/initialize")
}
}