diff options
| author | Keith Randall <khr@google.com> | 2018-11-26 14:33:32 -0800 |
|---|---|---|
| committer | Keith Randall <khr@golang.org> | 2018-11-26 22:50:48 +0000 |
| commit | 6fff980cf1f9af9f4b11e7fc7ead4987cc5fc560 (patch) | |
| tree | f1cfe6e6af658a8dc55bd42240b6c4f68b452dce /src | |
| parent | 1602e497012a27071e200d54ebfe13ca23d1f8af (diff) | |
| download | go-6fff980cf1f9af9f4b11e7fc7ead4987cc5fc560.tar.xz | |
cmd/compile: initialize sparse slice literals dynamically
When a slice composite literal is sparse, initialize it dynamically
instead of statically.
s := []int{5:5, 20:20}
To initialize the backing store for s, use 2 constant writes instead
of copying from a static array with 21 entries.
This CL also fixes pathologies in the compiler when the slice is
*very* sparse.
Fixes #23780
Change-Id: Iae95c6e6f6a0e2994675cbc750d7a4dd6436b13b
Reviewed-on: https://go-review.googlesource.com/c/151319
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/compile/internal/gc/sinit.go | 11 | ||||
| -rw-r--r-- | src/cmd/compile/internal/gc/syntax.go | 2 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go index 28ea72b715..acd8550ee3 100644 --- a/src/cmd/compile/internal/gc/sinit.go +++ b/src/cmd/compile/internal/gc/sinit.go @@ -612,6 +612,15 @@ func getdyn(n *Node, top bool) initGenType { if !top { return initDynamic } + if n.Right.Int64()/4 > int64(n.List.Len()) { + // <25% of entries have explicit values. + // Very rough estimation, it takes 4 bytes of instructions + // to initialize 1 byte of result. So don't use a static + // initializer if the dynamic initialization code would be + // smaller than the static value. + // See issue 23780. + return initDynamic + } case OARRAYLIT, OSTRUCTLIT: } @@ -902,7 +911,7 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) { continue } - if isLiteral(value) { + if vstat != nil && isLiteral(value) { // already set by copy from static value continue } diff --git a/src/cmd/compile/internal/gc/syntax.go b/src/cmd/compile/internal/gc/syntax.go index 0be52f1271..c7becf53e5 100644 --- a/src/cmd/compile/internal/gc/syntax.go +++ b/src/cmd/compile/internal/gc/syntax.go @@ -622,7 +622,7 @@ const ( OMAPLIT // Type{List} (composite literal, Type is map) OSTRUCTLIT // Type{List} (composite literal, Type is struct) OARRAYLIT // Type{List} (composite literal, Type is array) - OSLICELIT // Type{List} (composite literal, Type is slice) + OSLICELIT // Type{List} (composite literal, Type is slice) Right.Int64() = slice length. OPTRLIT // &Left (left is composite literal) OCONV // Type(Left) (type conversion) OCONVIFACE // Type(Left) (type conversion, to interface) |
