Skip to content

Commit 6ed4661

Browse files
committed
cmd/compile: optimize make+copy pattern to avoid memclr
match: m = make([]T, x); copy(m, s) for pointer free T and x==len(s) rewrite to: m = mallocgc(x*elemsize(T), nil, false); memmove(&m, &s, x*elemsize(T)) otherwise rewrite to: m = makeslicecopy([]T, x, s) This avoids memclear and shading of pointers in the newly created slice before the copy. With this CL "s" is only be allowed to bev a variable and not a more complex expression. This restriction could be lifted in future versions of this optimization when it can be proven that "s" is not referencing "m". Triggers 450 times during make.bash.. Reduces go binary size by ~8 kbyte. name old time/op new time/op delta MakeSliceCopy/mallocmove/Byte 71.1ns ± 1% 65.8ns ± 0% -7.49% (p=0.000 n=10+9) MakeSliceCopy/mallocmove/Int 71.2ns ± 1% 66.0ns ± 0% -7.27% (p=0.000 n=10+8) MakeSliceCopy/mallocmove/Ptr 104ns ± 4% 99ns ± 1% -5.13% (p=0.000 n=10+10) MakeSliceCopy/makecopy/Byte 70.3ns ± 0% 68.0ns ± 0% -3.22% (p=0.000 n=10+9) MakeSliceCopy/makecopy/Int 70.3ns ± 0% 68.5ns ± 1% -2.59% (p=0.000 n=9+10) MakeSliceCopy/makecopy/Ptr 102ns ± 0% 99ns ± 1% -2.97% (p=0.000 n=9+9) MakeSliceCopy/nilappend/Byte 75.4ns ± 0% 74.9ns ± 2% -0.63% (p=0.015 n=9+9) MakeSliceCopy/nilappend/Int 75.6ns ± 0% 76.4ns ± 3% ~ (p=0.245 n=9+10) MakeSliceCopy/nilappend/Ptr 107ns ± 0% 108ns ± 1% +0.93% (p=0.005 n=9+10) Fixes #26252 Change-Id: Iec553dd1fef6ded16197216a472351c8799a8e71 Reviewed-on: https://go-review.googlesource.com/c/go/+/146719 Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Martin Möhrmann <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 97240d5 commit 6ed4661

File tree

12 files changed

+1160
-503
lines changed

12 files changed

+1160
-503
lines changed

src/cmd/compile/internal/gc/builtin.go

Lines changed: 315 additions & 311 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/gc/builtin/runtime.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package runtime
1515
import "unsafe"
1616

1717
func newobject(typ *byte) *any
18+
func mallocgc(size uintptr, typ *byte, needszero bool) unsafe.Pointer
1819
func panicdivide()
1920
func panicshift()
2021
func panicmakeslicelen()
@@ -174,6 +175,7 @@ func block()
174175

175176
func makeslice(typ *byte, len int, cap int) unsafe.Pointer
176177
func makeslice64(typ *byte, len int64, cap int64) unsafe.Pointer
178+
func makeslicecopy(typ *byte, tolen int, fromlen int, from unsafe.Pointer) unsafe.Pointer
177179
func growslice(typ *byte, old []any, cap int) (ary []any)
178180
func memmove(to *any, frm *any, length uintptr)
179181
func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)

src/cmd/compile/internal/gc/fmt.go

Lines changed: 90 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,92 +1165,93 @@ func (n *Node) stmtfmt(s fmt.State, mode fmtMode) {
11651165
}
11661166

11671167
var opprec = []int{
1168-
OALIGNOF: 8,
1169-
OAPPEND: 8,
1170-
OBYTES2STR: 8,
1171-
OARRAYLIT: 8,
1172-
OSLICELIT: 8,
1173-
ORUNES2STR: 8,
1174-
OCALLFUNC: 8,
1175-
OCALLINTER: 8,
1176-
OCALLMETH: 8,
1177-
OCALL: 8,
1178-
OCAP: 8,
1179-
OCLOSE: 8,
1180-
OCONVIFACE: 8,
1181-
OCONVNOP: 8,
1182-
OCONV: 8,
1183-
OCOPY: 8,
1184-
ODELETE: 8,
1185-
OGETG: 8,
1186-
OLEN: 8,
1187-
OLITERAL: 8,
1188-
OMAKESLICE: 8,
1189-
OMAKE: 8,
1190-
OMAPLIT: 8,
1191-
ONAME: 8,
1192-
ONEW: 8,
1193-
ONONAME: 8,
1194-
OOFFSETOF: 8,
1195-
OPACK: 8,
1196-
OPANIC: 8,
1197-
OPAREN: 8,
1198-
OPRINTN: 8,
1199-
OPRINT: 8,
1200-
ORUNESTR: 8,
1201-
OSIZEOF: 8,
1202-
OSTR2BYTES: 8,
1203-
OSTR2RUNES: 8,
1204-
OSTRUCTLIT: 8,
1205-
OTARRAY: 8,
1206-
OTCHAN: 8,
1207-
OTFUNC: 8,
1208-
OTINTER: 8,
1209-
OTMAP: 8,
1210-
OTSTRUCT: 8,
1211-
OINDEXMAP: 8,
1212-
OINDEX: 8,
1213-
OSLICE: 8,
1214-
OSLICESTR: 8,
1215-
OSLICEARR: 8,
1216-
OSLICE3: 8,
1217-
OSLICE3ARR: 8,
1218-
OSLICEHEADER: 8,
1219-
ODOTINTER: 8,
1220-
ODOTMETH: 8,
1221-
ODOTPTR: 8,
1222-
ODOTTYPE2: 8,
1223-
ODOTTYPE: 8,
1224-
ODOT: 8,
1225-
OXDOT: 8,
1226-
OCALLPART: 8,
1227-
OPLUS: 7,
1228-
ONOT: 7,
1229-
OBITNOT: 7,
1230-
ONEG: 7,
1231-
OADDR: 7,
1232-
ODEREF: 7,
1233-
ORECV: 7,
1234-
OMUL: 6,
1235-
ODIV: 6,
1236-
OMOD: 6,
1237-
OLSH: 6,
1238-
ORSH: 6,
1239-
OAND: 6,
1240-
OANDNOT: 6,
1241-
OADD: 5,
1242-
OSUB: 5,
1243-
OOR: 5,
1244-
OXOR: 5,
1245-
OEQ: 4,
1246-
OLT: 4,
1247-
OLE: 4,
1248-
OGE: 4,
1249-
OGT: 4,
1250-
ONE: 4,
1251-
OSEND: 3,
1252-
OANDAND: 2,
1253-
OOROR: 1,
1168+
OALIGNOF: 8,
1169+
OAPPEND: 8,
1170+
OBYTES2STR: 8,
1171+
OARRAYLIT: 8,
1172+
OSLICELIT: 8,
1173+
ORUNES2STR: 8,
1174+
OCALLFUNC: 8,
1175+
OCALLINTER: 8,
1176+
OCALLMETH: 8,
1177+
OCALL: 8,
1178+
OCAP: 8,
1179+
OCLOSE: 8,
1180+
OCONVIFACE: 8,
1181+
OCONVNOP: 8,
1182+
OCONV: 8,
1183+
OCOPY: 8,
1184+
ODELETE: 8,
1185+
OGETG: 8,
1186+
OLEN: 8,
1187+
OLITERAL: 8,
1188+
OMAKESLICE: 8,
1189+
OMAKESLICECOPY: 8,
1190+
OMAKE: 8,
1191+
OMAPLIT: 8,
1192+
ONAME: 8,
1193+
ONEW: 8,
1194+
ONONAME: 8,
1195+
OOFFSETOF: 8,
1196+
OPACK: 8,
1197+
OPANIC: 8,
1198+
OPAREN: 8,
1199+
OPRINTN: 8,
1200+
OPRINT: 8,
1201+
ORUNESTR: 8,
1202+
OSIZEOF: 8,
1203+
OSTR2BYTES: 8,
1204+
OSTR2RUNES: 8,
1205+
OSTRUCTLIT: 8,
1206+
OTARRAY: 8,
1207+
OTCHAN: 8,
1208+
OTFUNC: 8,
1209+
OTINTER: 8,
1210+
OTMAP: 8,
1211+
OTSTRUCT: 8,
1212+
OINDEXMAP: 8,
1213+
OINDEX: 8,
1214+
OSLICE: 8,
1215+
OSLICESTR: 8,
1216+
OSLICEARR: 8,
1217+
OSLICE3: 8,
1218+
OSLICE3ARR: 8,
1219+
OSLICEHEADER: 8,
1220+
ODOTINTER: 8,
1221+
ODOTMETH: 8,
1222+
ODOTPTR: 8,
1223+
ODOTTYPE2: 8,
1224+
ODOTTYPE: 8,
1225+
ODOT: 8,
1226+
OXDOT: 8,
1227+
OCALLPART: 8,
1228+
OPLUS: 7,
1229+
ONOT: 7,
1230+
OBITNOT: 7,
1231+
ONEG: 7,
1232+
OADDR: 7,
1233+
ODEREF: 7,
1234+
ORECV: 7,
1235+
OMUL: 6,
1236+
ODIV: 6,
1237+
OMOD: 6,
1238+
OLSH: 6,
1239+
ORSH: 6,
1240+
OAND: 6,
1241+
OANDNOT: 6,
1242+
OADD: 5,
1243+
OSUB: 5,
1244+
OOR: 5,
1245+
OXOR: 5,
1246+
OEQ: 4,
1247+
OLT: 4,
1248+
OLE: 4,
1249+
OGE: 4,
1250+
OGT: 4,
1251+
ONE: 4,
1252+
OSEND: 3,
1253+
OANDAND: 2,
1254+
OOROR: 1,
12541255

12551256
// Statements handled by stmtfmt
12561257
OAS: -1,
@@ -1572,6 +1573,9 @@ func (n *Node) exprfmt(s fmt.State, prec int, mode fmtMode) {
15721573
}
15731574
mode.Fprintf(s, "make(%v)", n.Type)
15741575

1576+
case OMAKESLICECOPY:
1577+
mode.Fprintf(s, "makeslicecopy(%v, %v, %v)", n.Type, n.Left, n.Right)
1578+
15751579
case OPLUS, ONEG, OADDR, OBITNOT, ODEREF, ONOT, ORECV:
15761580
// Unary
15771581
mode.Fprintf(s, "%#v", n.Op)

src/cmd/compile/internal/gc/op_string.go

Lines changed: 81 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)