Skip to content

Commit a2f28a4

Browse files
thepuddsgopherbot
authored andcommitted
fmt: avoid reflect.Value.Pointer to help escape analysis
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt. Prior to this change, one reason arguments escape is because fmtPointer calls reflect.Value.Pointer: ./print.go:551:39: parameter value leaks to <heap> for (*pp).fmtPointer with derefs=0: ./print.go:551:39: flow: <heap> ← value: ./print.go:551:39: from reflect.Value.Pointer(value) (call parameter) at ./print.go:555:20 printValue also has its value argument escape for this reason, among others. This CL changes those uses to reflect.Value.UnsafePointer instead, which does not cause an escape. Arguments still escape for other reasons. Change-Id: I81c4f737f11fe835c5ccb122caee40a39b553451 Reviewed-on: https://go-review.googlesource.com/c/go/+/524939 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: t hepudds <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 0eed32b commit a2f28a4

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/fmt/print.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune) {
550550
var u uintptr
551551
switch value.Kind() {
552552
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
553-
u = value.Pointer()
553+
u = uintptr(value.UnsafePointer())
554554
default:
555555
p.badVerb(verb)
556556
return
@@ -916,7 +916,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
916916
case reflect.Pointer:
917917
// pointer to array or slice or struct? ok at top level
918918
// but not embedded (avoid loops)
919-
if depth == 0 && f.Pointer() != 0 {
919+
if depth == 0 && f.UnsafePointer() != nil {
920920
switch a := f.Elem(); a.Kind() {
921921
case reflect.Array, reflect.Slice, reflect.Struct, reflect.Map:
922922
p.buf.writeByte('&')

0 commit comments

Comments
 (0)