mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
This CL is part of a set of CLs that attempt to reduce how much work the GC must do. See the design in https://go.dev/design/74299-runtime-freegc This CL updates the compiler to examine append calls to prove whether or not the slice is aliased. If proven unaliased, the compiler automatically inserts a call to a new runtime function introduced with this CL, runtime.growsliceNoAlias, which frees the old backing memory immediately after slice growth is complete and the old storage is logically dead. Two append benchmarks below show promising results, executing up to ~2x faster and up to factor of ~3 memory reduction with this CL. The approach works with multiple append calls for the same slice, including inside loops, and the final slice memory can be escaping, such as in a classic pattern of returning a slice from a function after the slice is built. (The final slice memory is never freed with this CL, though we have other work that tackles that.) An example target for this CL is we automatically free the intermediate memory for the appends in the loop in this function: func f1(input []int) []int { var s []int for _, x := range input { s = append(s, g(x)) // s cannot be aliased here if h(x) { s = append(s, x) // s cannot be aliased here } } return s // slice escapes at end } In this case, the compiler and the runtime collaborate so that the heap allocated backing memory for s is automatically freed after a successful grow. (For the first grow, there is nothing to free, but for the second and subsequent growths, the old heap memory is freed automatically.) The new runtime.growsliceNoAlias is primarily implemented by calling runtime.freegc, which we introduced in CL 673695. The high-level approach here is we step through the IR starting from a slice declaration and look for any operations that either alias the slice or might do so, and treat any IR construct we don't specifically handle as a potential alias (and therefore conservatively fall back to treating the slice as aliased when encountering something not understood). For loops, some additional care is required. We arrange the analysis so that an alias in the body of a loop causes all the appends in that same loop body to be marked aliased, even if the aliasing occurs after the append in the IR: func f2() { var s []int for i := range 10 { s = append(s, i) // aliased due to next line alias = s } } For nested loops, we analyse the nesting appropriately so that for example this append is still proven as non-aliased in the inner loop even though it aliased for the outer loop: func f3() { for range 10 { var s []int for i := range 10 { s = append(s, i) // append using non-aliased slice } alias = s } } A good starting point is the beginning of the test/escape_alias.go file, which starts with ~10 introductory examples with brief comments that attempt to illustrate the high-level approach. For more details, see the new .../internal/escape/alias.go file, especially the (*aliasAnalysis).analyze method. In the first benchmark, an append in a loop builds up a slice from nothing, where the slice elements are each 64 bytes. In the table below, 'count' is the number of appends. With 1 append, there is no opportunity for this CL to free memory. Once there are 2 appends, the growth from 1 element to 2 elements means the compiler-inserted growsliceNoAlias frees the 1-element array, and we see a ~33% reduction in memory use and a small reported speed improvement. As the number of appends increases for example to 5, we are at a ~20% speed improvement and ~45% memory reduction, and so on until we reach ~40% faster and ~50% less memory allocated at the end of the table. There can be variation in the reported numbers based on -randlayout, so this table is for 30 different values of -randlayout with a total n=150. (Even so, there is still some variation, so we probably should not read too much into small changes.) This is with GOAMD64=v3 on a VM that gcc reports is cascadelake. goos: linux goarch: amd64 pkg: runtime cpu: Intel(R) Xeon(R) CPU @ 2.80GHz │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ sec/op │ sec/op vs base │ Append64Bytes/count=1-4 31.09n ± 2% 31.69n ± 1% +1.95% (n=150) Append64Bytes/count=2-4 73.31n ± 1% 70.27n ± 0% -4.15% (n=150) Append64Bytes/count=3-4 142.7n ± 1% 124.6n ± 1% -12.68% (n=150) Append64Bytes/count=4-4 149.6n ± 1% 127.7n ± 0% -14.64% (n=150) Append64Bytes/count=5-4 277.1n ± 1% 213.6n ± 0% -22.90% (n=150) Append64Bytes/count=6-4 280.7n ± 1% 216.5n ± 1% -22.87% (n=150) Append64Bytes/count=10-4 544.3n ± 1% 386.6n ± 0% -28.97% (n=150) Append64Bytes/count=20-4 1058.5n ± 1% 715.6n ± 1% -32.39% (n=150) Append64Bytes/count=50-4 2.121µ ± 1% 1.404µ ± 1% -33.83% (n=150) Append64Bytes/count=100-4 4.152µ ± 1% 2.736µ ± 1% -34.11% (n=150) Append64Bytes/count=200-4 7.753µ ± 1% 4.882µ ± 1% -37.03% (n=150) Append64Bytes/count=400-4 15.163µ ± 2% 9.273µ ± 1% -38.84% (n=150) geomean 601.8n 455.0n -24.39% │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ B/op │ B/op vs base │ Append64Bytes/count=1-4 64.00 ± 0% 64.00 ± 0% ~ (n=150) Append64Bytes/count=2-4 192.0 ± 0% 128.0 ± 0% -33.33% (n=150) Append64Bytes/count=3-4 448.0 ± 0% 256.0 ± 0% -42.86% (n=150) Append64Bytes/count=4-4 448.0 ± 0% 256.0 ± 0% -42.86% (n=150) Append64Bytes/count=5-4 960.0 ± 0% 512.0 ± 0% -46.67% (n=150) Append64Bytes/count=6-4 960.0 ± 0% 512.0 ± 0% -46.67% (n=150) Append64Bytes/count=10-4 1.938Ki ± 0% 1.000Ki ± 0% -48.39% (n=150) Append64Bytes/count=20-4 3.938Ki ± 0% 2.001Ki ± 0% -49.18% (n=150) Append64Bytes/count=50-4 7.938Ki ± 0% 4.005Ki ± 0% -49.54% (n=150) Append64Bytes/count=100-4 15.938Ki ± 0% 8.021Ki ± 0% -49.67% (n=150) Append64Bytes/count=200-4 31.94Ki ± 0% 16.08Ki ± 0% -49.64% (n=150) Append64Bytes/count=400-4 63.94Ki ± 0% 32.33Ki ± 0% -49.44% (n=150) geomean 1.991Ki 1.124Ki -43.54% │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ allocs/op │ allocs/op vs base │ Append64Bytes/count=1-4 1.000 ± 0% 1.000 ± 0% ~ (n=150) Append64Bytes/count=2-4 2.000 ± 0% 1.000 ± 0% -50.00% (n=150) Append64Bytes/count=3-4 3.000 ± 0% 1.000 ± 0% -66.67% (n=150) Append64Bytes/count=4-4 3.000 ± 0% 1.000 ± 0% -66.67% (n=150) Append64Bytes/count=5-4 4.000 ± 0% 1.000 ± 0% -75.00% (n=150) Append64Bytes/count=6-4 4.000 ± 0% 1.000 ± 0% -75.00% (n=150) Append64Bytes/count=10-4 5.000 ± 0% 1.000 ± 0% -80.00% (n=150) Append64Bytes/count=20-4 6.000 ± 0% 1.000 ± 0% -83.33% (n=150) Append64Bytes/count=50-4 7.000 ± 0% 1.000 ± 0% -85.71% (n=150) Append64Bytes/count=100-4 8.000 ± 0% 1.000 ± 0% -87.50% (n=150) Append64Bytes/count=200-4 9.000 ± 0% 1.000 ± 0% -88.89% (n=150) Append64Bytes/count=400-4 10.000 ± 0% 1.000 ± 0% -90.00% (n=150) geomean 4.331 1.000 -76.91% The second benchmark is similar, but instead uses an 8-byte integer for the slice element. The first 4 appends in the loop never call into the runtime thanks to the excellent CL 664299 introduced by Keith in Go 1.25 that allows some <= 32 byte dynamically-sized slices to be on the stack, so this CL is neutral for <= 32 bytes. Once the 5th append occurs at count=5, a grow happens via the runtime and heap allocates as normal, but freegc does not yet have anything to free, so we see a small ~1.4ns penalty reported there. But once the second growth happens, the older heap memory is now automatically freed by freegc, so we start to see some benefit in memory reductions and speed improvements, starting at a tiny speed improvement (close to a wash, or maybe noise) by the second growth before count=10, and building up to ~2x faster with ~68% fewer allocated bytes reported. goos: linux goarch: amd64 pkg: runtime cpu: Intel(R) Xeon(R) CPU @ 2.80GHz │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ sec/op │ sec/op vs base │ AppendInt/count=1-4 2.978n ± 0% 2.969n ± 0% -0.30% (p=0.000 n=150) AppendInt/count=4-4 4.292n ± 3% 4.163n ± 3% ~ (p=0.528 n=150) AppendInt/count=5-4 33.50n ± 0% 34.93n ± 0% +4.25% (p=0.000 n=150) AppendInt/count=10-4 76.21n ± 1% 75.67n ± 0% -0.72% (p=0.000 n=150) AppendInt/count=20-4 150.6n ± 1% 133.0n ± 0% -11.65% (n=150) AppendInt/count=50-4 284.1n ± 1% 225.6n ± 0% -20.59% (n=150) AppendInt/count=100-4 544.2n ± 1% 392.4n ± 1% -27.89% (n=150) AppendInt/count=200-4 1051.5n ± 1% 702.3n ± 0% -33.21% (n=150) AppendInt/count=400-4 2.041µ ± 1% 1.312µ ± 1% -35.70% (n=150) AppendInt/count=1000-4 5.224µ ± 2% 2.851µ ± 1% -45.43% (n=150) AppendInt/count=2000-4 11.770µ ± 1% 6.010µ ± 1% -48.94% (n=150) AppendInt/count=3000-4 17.747µ ± 2% 8.264µ ± 1% -53.44% (n=150) geomean 331.8n 246.4n -25.72% │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ B/op │ B/op vs base │ AppendInt/count=1-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=150) AppendInt/count=4-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=150) AppendInt/count=5-4 64.00 ± 0% 64.00 ± 0% ~ (p=1.000 n=150) AppendInt/count=10-4 192.0 ± 0% 128.0 ± 0% -33.33% (n=150) AppendInt/count=20-4 448.0 ± 0% 256.0 ± 0% -42.86% (n=150) AppendInt/count=50-4 960.0 ± 0% 512.0 ± 0% -46.67% (n=150) AppendInt/count=100-4 1.938Ki ± 0% 1.000Ki ± 0% -48.39% (n=150) AppendInt/count=200-4 3.938Ki ± 0% 2.001Ki ± 0% -49.18% (n=150) AppendInt/count=400-4 7.938Ki ± 0% 4.005Ki ± 0% -49.54% (n=150) AppendInt/count=1000-4 24.56Ki ± 0% 10.05Ki ± 0% -59.07% (n=150) AppendInt/count=2000-4 58.56Ki ± 0% 20.31Ki ± 0% -65.32% (n=150) AppendInt/count=3000-4 85.19Ki ± 0% 27.30Ki ± 0% -67.95% (n=150) geomean ² -42.81% │ old-1bb1f2bf0c │ freegc-8ba7421-ps16 │ │ allocs/op │ allocs/op vs base │ AppendInt/count=1-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=150) AppendInt/count=4-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=150) AppendInt/count=5-4 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=150) AppendInt/count=10-4 2.000 ± 0% 1.000 ± 0% -50.00% (n=150) AppendInt/count=20-4 3.000 ± 0% 1.000 ± 0% -66.67% (n=150) AppendInt/count=50-4 4.000 ± 0% 1.000 ± 0% -75.00% (n=150) AppendInt/count=100-4 5.000 ± 0% 1.000 ± 0% -80.00% (n=150) AppendInt/count=200-4 6.000 ± 0% 1.000 ± 0% -83.33% (n=150) AppendInt/count=400-4 7.000 ± 0% 1.000 ± 0% -85.71% (n=150) AppendInt/count=1000-4 9.000 ± 0% 1.000 ± 0% -88.89% (n=150) AppendInt/count=2000-4 11.000 ± 0% 1.000 ± 0% -90.91% (n=150) AppendInt/count=3000-4 12.000 ± 0% 1.000 ± 0% -91.67% (n=150) geomean ² -72.76% ² Of course, these are just microbenchmarks, but likely indicate there are some opportunities here. The immediately following CL 712422 tackles inlining and is able to get runtime.freegc working automatically with iterators such as used by slices.Collect, which becomes able to automatically free the intermediate memory from its repeated appends (which earlier in this work required a temporary hand edit to the slices package). For now, we only use the NoAlias version for element types without pointers while waiting on additional runtime support in CL 698515. Updates #74299 Change-Id: I1b9d286aa97c170dcc2e203ec0f8ca72d84e8221 Reviewed-on: https://go-review.googlesource.com/c/go/+/710015 Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org>
779 lines
13 KiB
Go
779 lines
13 KiB
Go
// errorcheck -0 -d=escapealias=1
|
|
|
|
//go:build goexperiment.runtimefreegc
|
|
|
|
// Copyright 2025 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 recognizing certain patterns of usage,
|
|
// currently focused on whether a slice is aliased.
|
|
|
|
package escapealias
|
|
|
|
import "runtime"
|
|
|
|
// Basic examples.
|
|
//
|
|
// Some of these directly overlap with later tests below, but are presented at the start
|
|
// to help show the big picture (before going into more variations).
|
|
|
|
var alias []int
|
|
|
|
func basic1() {
|
|
// A simple append with no aliasing of s.
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
_ = s
|
|
}
|
|
|
|
func basic2() []int {
|
|
// The slice can escape.
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
return s
|
|
}
|
|
|
|
func basic3() {
|
|
// A simple example of s being aliased.
|
|
// We give up when we see the aliasing.
|
|
var s []int
|
|
alias = s
|
|
s = append(s, 0)
|
|
_ = s
|
|
}
|
|
|
|
func basic4() {
|
|
// The analysis is conservative, giving up on
|
|
// IR nodes it doesn't understand. It does not
|
|
// yet understand comparisons, for example.
|
|
var s []int
|
|
_ = s == nil
|
|
s = append(s, 0)
|
|
_ = s
|
|
}
|
|
|
|
func basic5() {
|
|
// We also give up if s is assigned to another variable.
|
|
var s []int
|
|
s2 := s
|
|
s2 = append(s2, 0)
|
|
_ = s2
|
|
}
|
|
|
|
func basic6() {
|
|
// A self-assigning append does not create an alias,
|
|
// so s is still unaliased when we reach the second append here.
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
_ = s
|
|
}
|
|
|
|
func basic7() {
|
|
// An append can be unaliased if it happens before aliasing.
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
alias = s
|
|
s = append(s, 0)
|
|
_ = s
|
|
}
|
|
|
|
func basic8() {
|
|
// Aliasing anywhere in a loop means we give up for the whole loop body,
|
|
// even if the aliasing is after the append in the loop body.
|
|
var s []int
|
|
for range 10 {
|
|
s = append(s, 0)
|
|
alias = s
|
|
}
|
|
_ = s
|
|
}
|
|
|
|
func basic9() {
|
|
// Aliases after a loop do not affect whether this is aliasing in the loop.
|
|
var s []int
|
|
for range 10 {
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
alias = s
|
|
_ = s
|
|
}
|
|
|
|
func basic10() {
|
|
// We track the depth at which a slice is declared vs. aliased,
|
|
// which helps for example with nested loops.
|
|
// In this example, the aliasing occurs after both loops are done.
|
|
var s []int
|
|
for range 10 {
|
|
for range 10 {
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
alias = s
|
|
}
|
|
|
|
func basic11() {
|
|
// In contrast, here the aliasing occurs in the outer loop body.
|
|
var s []int
|
|
for range 10 {
|
|
for range 10 {
|
|
s = append(s, 0)
|
|
}
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
// Some variations on single appends.
|
|
|
|
func singleAppend1() []int {
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
return s
|
|
}
|
|
|
|
func singleAppend2() {
|
|
var s []int
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func singleAppend3() {
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
alias = s
|
|
}
|
|
|
|
func singleAppend4() {
|
|
var s []int
|
|
p := &s
|
|
_ = p
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func singleAppend5(s []int) {
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func singleAppend6() {
|
|
var s []int
|
|
alias, _ = s, 0
|
|
s = append(s, 0)
|
|
}
|
|
|
|
// Examples with variations on slice declarations.
|
|
|
|
func sliceDeclaration1() {
|
|
s := []int{}
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
|
|
func sliceDeclaration2() {
|
|
s := []int{1, 2, 3}
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
|
|
func sliceDeclaration3() {
|
|
s := make([]int, 3)
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
|
|
func sliceDeclaration4() {
|
|
s := []int{}
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func sliceDeclaration5() {
|
|
s := []int{1, 2, 3}
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func sliceDeclaration6() {
|
|
s := make([]int, 3)
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func sliceDeclaration7() {
|
|
s, x := []int{}, 0
|
|
s = append(s, x) // ERROR "append using non-aliased slice"
|
|
}
|
|
|
|
// Basic loops. First, a single loop.
|
|
|
|
func loops1a() {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
|
|
func loops1b() {
|
|
var s []int
|
|
for i := range 10 {
|
|
alias = s
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
|
|
func loops1c() {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loops1d() {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
alias = s
|
|
}
|
|
|
|
func loops1e() {
|
|
var s []int
|
|
for i := range use(s) {
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
|
|
func loops1f() {
|
|
var s []int
|
|
for i := range use(s) {
|
|
s = append(s, i)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
|
|
// Nested loops with s declared outside the loops.
|
|
|
|
func loops2a() {
|
|
var s []int
|
|
for range 10 {
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
}
|
|
|
|
func loops2b() {
|
|
var s []int
|
|
for range 10 {
|
|
alias = s
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func loops2c() {
|
|
var s []int
|
|
for range 10 {
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
}
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loops2d() {
|
|
var s []int
|
|
for range 10 {
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
alias = s
|
|
}
|
|
|
|
func loops2e() {
|
|
var s []int
|
|
for range use(s) {
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func loops2f() {
|
|
var s []int
|
|
for range 10 {
|
|
for i := range use(s) {
|
|
s = append(s, i)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
|
|
// Nested loops with s declared inside the first loop.
|
|
|
|
func loops3a() {
|
|
for range 10 {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
}
|
|
|
|
func loops3b() {
|
|
for range 10 {
|
|
var s []int
|
|
for i := range 10 {
|
|
alias = s
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func loops3c() {
|
|
for range 10 {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
}
|
|
|
|
func loops3d() {
|
|
for range 10 {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loops3e() {
|
|
for range 10 {
|
|
var s []int
|
|
for i := range use(s) {
|
|
s = append(s, i)
|
|
}
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
// Loops using OFOR instead of ORANGE.
|
|
|
|
func loops4a() {
|
|
var s []int
|
|
for i := 0; i < 10; i++ {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
|
|
func loops4b() {
|
|
var s []int
|
|
for i := 0; i < 10; i++ {
|
|
alias = s
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
|
|
func loops4c() {
|
|
var s []int
|
|
for i := 0; i < 10; i++ {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loops4d() {
|
|
var s []int
|
|
for i := 0; i < 10; i++ {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
alias = s
|
|
}
|
|
|
|
// Loops with some initialization variations.
|
|
|
|
func loopsInit1() {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
|
|
func loopsInit2() {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loopsInit3() {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
for range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
}
|
|
|
|
func loopsInit5() {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
for range 10 {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
}
|
|
|
|
func loopsInit5b() {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
for range 10 {
|
|
s = append(s, i)
|
|
}
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
func loopsInit6() {
|
|
for range 10 {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
}
|
|
|
|
func loopsInit7() {
|
|
for range 10 {
|
|
var i int
|
|
for s := []int{}; i < 10; i++ {
|
|
s = append(s, i)
|
|
alias = s
|
|
}
|
|
}
|
|
}
|
|
|
|
// Some initialization variations with use of s in the for or range.
|
|
|
|
func loopsInit8() {
|
|
var s []int
|
|
for use(s) == 0 {
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func loopsInit9() {
|
|
for s := []int{}; use(s) == 0; {
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func loopsInit10() {
|
|
for s := []int{}; ; use(s) {
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func loopsInit11() {
|
|
var s [][]int
|
|
for _, s2 := range s {
|
|
s = append(s, s2)
|
|
}
|
|
}
|
|
|
|
// Examples of calling functions that get inlined,
|
|
// starting with a simple pass-through function.
|
|
|
|
// TODO(thepudds): we handle many of these starting in https://go.dev/cl/712422
|
|
|
|
func inlineReturn(param []int) []int {
|
|
return param
|
|
}
|
|
|
|
func inline1a() {
|
|
var s []int
|
|
s = inlineReturn(s)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func inline1b() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineReturn(s)
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func inline1c() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineReturn(s)
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func inline1d() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineReturn(s)
|
|
s = append(s, 0)
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
// Examples with an inlined function that uses append.
|
|
|
|
func inlineAppend(param []int) []int {
|
|
param = append(param, 0)
|
|
// TODO(thepudds): could in theory also handle a direct 'return append(param, 0)'
|
|
return param
|
|
}
|
|
|
|
func inline2a() {
|
|
var s []int
|
|
s = inlineAppend(s)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func inline2b() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineAppend(s)
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func inline2c() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineAppend(s)
|
|
alias = s
|
|
s = append(s, 0)
|
|
}
|
|
}
|
|
|
|
func inline2d() {
|
|
var s []int
|
|
for range 10 {
|
|
s = inlineAppend(s)
|
|
s = append(s, 0)
|
|
alias = s
|
|
}
|
|
}
|
|
|
|
// Examples calling non-inlined functions that do and do not escape.
|
|
|
|
var sink interface{}
|
|
|
|
//go:noinline
|
|
func use(s []int) int { return 0 } // s content does not escape
|
|
|
|
//go:noinline
|
|
func escape(s []int) int { sink = s; return 0 } // s content escapes
|
|
|
|
func call1() {
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
use(s)
|
|
}
|
|
|
|
// TODO(thepudds): OK to disallow this for now, but would be nice to allow this given use(s) is non-escaping.
|
|
func call2() {
|
|
var s []int
|
|
use(s)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func call3() {
|
|
var s []int
|
|
s = append(s, use(s))
|
|
}
|
|
|
|
func call4() {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
use(s)
|
|
}
|
|
}
|
|
|
|
func callEscape1() {
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
escape(s)
|
|
}
|
|
|
|
func callEscape2() {
|
|
var s []int
|
|
escape(s)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func callEscape3() {
|
|
var s []int
|
|
s = append(s, escape(s))
|
|
}
|
|
|
|
func callEscape4() {
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
escape(s)
|
|
}
|
|
}
|
|
|
|
// Examples of some additional expressions we understand.
|
|
|
|
func expr1() {
|
|
var s []int
|
|
_ = len(s)
|
|
_ = cap(s)
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
}
|
|
|
|
// Examples of some expressions or statements we do not understand.
|
|
// Some of these we could handle in the future, but some likely not.
|
|
|
|
func notUnderstood1() {
|
|
var s []int
|
|
s = append(s[:], 0)
|
|
}
|
|
|
|
func notUnderstood2() {
|
|
// Note: we must be careful if we analyze slice expressions.
|
|
// See related comment about slice expressions in (*aliasAnalysis).analyze.
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
s = s[1:] // s no longer points to the base of the heap object.
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func notUnderstood3() {
|
|
// The first append is currently the heart of slices.Grow.
|
|
var s []int
|
|
n := 1000
|
|
s = append(s[:cap(s)], make([]int, n)...)[:len(s)]
|
|
s = append(s, 0)
|
|
}
|
|
|
|
func notUnderstood4() []int {
|
|
// A return statement could be allowed to use the slice in a loop
|
|
// because we cannot revisit the append once we return.
|
|
var s []int
|
|
for i := range 10 {
|
|
s = append(s, 0)
|
|
if i > 5 {
|
|
return s
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
func notUnderstood5() {
|
|
// AddCleanup is an example function call that we do not understand.
|
|
// See related comment about specials in (*aliasAnalysis).analyze.
|
|
var s []int
|
|
runtime.AddCleanup(&s, func(int) {}, 0)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
// Examples with closures.
|
|
|
|
func closure1() {
|
|
var s []int // declared outside the closure
|
|
f := func() {
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
}
|
|
}
|
|
_ = f // avoid calling f, which would just get inlined
|
|
}
|
|
|
|
// TODO(thepudds): it's probably ok that we currently allow this. Could conservatively
|
|
// disallow if needed.
|
|
func closure2() {
|
|
f := func() {
|
|
var s []int // declared inside the closure
|
|
for i := range 10 {
|
|
s = append(s, i) // ERROR "append using non-aliased slice"
|
|
}
|
|
}
|
|
_ = f // avoid calling f, which would just get inlined
|
|
}
|
|
|
|
// Examples with goto and labels.
|
|
|
|
func goto1() {
|
|
var s []int
|
|
label:
|
|
s = append(s, 0)
|
|
alias = s
|
|
goto label
|
|
}
|
|
|
|
func goto2() {
|
|
var s []int
|
|
s = append(s, 0) // ERROR "append using non-aliased slice"
|
|
alias = s
|
|
label:
|
|
goto label
|
|
}
|
|
|
|
func goto3() {
|
|
var s []int
|
|
label:
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
}
|
|
goto label
|
|
}
|
|
|
|
func break1() {
|
|
var s []int
|
|
label:
|
|
for i := range 10 {
|
|
s = append(s, i)
|
|
break label
|
|
}
|
|
}
|
|
|
|
// Examples with iterators.
|
|
|
|
func collect[E any](seq Seq[E]) []E {
|
|
var result []E
|
|
for v := range seq {
|
|
result = append(result, v)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func count(yield func(int) bool) {
|
|
for i := range 10 {
|
|
if !yield(i) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func iteratorUse1() {
|
|
var s []int
|
|
s = collect(count)
|
|
_ = s
|
|
}
|
|
|
|
func iteratorUse2() {
|
|
var s []int
|
|
s = collect(count)
|
|
s = append(s, 0)
|
|
}
|
|
|
|
type Seq[E any] func(yield func(E) bool)
|