-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/compile: don't write back unchanged slice results
Don't write back parts of a slicing operation if they are unchanged from the source of the slice. For example: x.s = x.s[0:5] // don't write back pointer or cap x.s = x.s[:5] // don't write back pointer or cap x.s = x.s[:5:7] // don't write back pointer There is more to be done here, for example: x.s = x.s[:len(x.s):7] // don't write back ptr or len This CL can't handle that one yet. Fixes #14855 Change-Id: Id1e1a4fa7f3076dc1a76924a7f1cd791b81909bb Reviewed-on: https://go-review.googlesource.com/20954 Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Keith Randall <[email protected]>
- Loading branch information
Showing
5 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// run | ||
|
||
// This test makes sure that t.s = t.s[0:x] doesn't write | ||
// either the slice pointer or the capacity. | ||
// See issue #14855. | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
const N = 1000000 | ||
|
||
type T struct { | ||
s []int | ||
} | ||
|
||
func main() { | ||
done := make(chan struct{}) | ||
a := make([]int, N+10) | ||
|
||
t := &T{a} | ||
|
||
go func() { | ||
for i := 0; i < N; i++ { | ||
t.s = t.s[1:9] | ||
} | ||
done <- struct{}{} | ||
}() | ||
go func() { | ||
for i := 0; i < N; i++ { | ||
t.s = t.s[0:8] // should only write len | ||
} | ||
done <- struct{}{} | ||
}() | ||
<-done | ||
<-done | ||
|
||
ok := true | ||
if cap(t.s) != cap(a)-N { | ||
fmt.Printf("wanted cap=%d, got %d\n", cap(a)-N, cap(t.s)) | ||
ok = false | ||
} | ||
if &t.s[0] != &a[N] { | ||
fmt.Printf("wanted ptr=%p, got %p\n", &a[N], &t.s[0]) | ||
ok = false | ||
} | ||
if !ok { | ||
panic("bad") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters