From a2f28a48415adb8b8736aa1d5c335f99375b2f3a Mon Sep 17 00:00:00 2001 From: thepudds Date: Wed, 21 Jun 2023 14:18:56 -0400 Subject: [PATCH] fmt: avoid reflect.Value.Pointer to help escape analysis MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 for (*pp).fmtPointer with derefs=0: ./print.go:551:39: flow: ← 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 Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot Run-TryBot: t hepudds Reviewed-by: Matthew Dempsky --- src/fmt/print.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fmt/print.go b/src/fmt/print.go index 50381f785f..9225e2e28c 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -550,7 +550,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune) { var u uintptr switch value.Kind() { case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer: - u = value.Pointer() + u = uintptr(value.UnsafePointer()) default: p.badVerb(verb) return @@ -916,7 +916,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) { case reflect.Pointer: // pointer to array or slice or struct? ok at top level // but not embedded (avoid loops) - if depth == 0 && f.Pointer() != 0 { + if depth == 0 && f.UnsafePointer() != nil { switch a := f.Elem(); a.Kind() { case reflect.Array, reflect.Slice, reflect.Struct, reflect.Map: p.buf.writeByte('&') -- 2.50.0