Skip to content

Commit

Permalink
internal/abi, runtime, reflect, cmd: merge maxZero const into interna…
Browse files Browse the repository at this point in the history
…l/abi

For #59670
  • Loading branch information
qiulaidongfeng committed Nov 21, 2023
1 parent 74993bf commit 89475c8
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/cmd/compile/internal/walk/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package walk

import (
"go/constant"
"internal/abi"

"cmd/compile/internal/base"
"cmd/compile/internal/ir"
Expand Down Expand Up @@ -168,7 +169,7 @@ func walkAssignMapRead(init *ir.Nodes, n *ir.AssignListStmt) ir.Node {
a := n.Lhs[0]

var call *ir.CallExpr
if w := t.Elem().Size(); w <= zeroValSize {
if w := t.Elem().Size(); w <= abi.MaxZero {
fn := mapfn(mapaccess2[fast], t, false)
call = mkcall1(fn, fn.Type().ResultsTuple(), init, reflectdata.IndexMapRType(base.Pos, r), r.X, key)
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/compile/internal/walk/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package walk
import (
"fmt"
"go/constant"
"internal/abi"
"internal/buildcfg"
"strings"

Expand Down Expand Up @@ -825,7 +826,7 @@ func walkIndexMap(n *ir.IndexExpr, init *ir.Nodes) ir.Node {
switch {
case n.Assigned:
mapFn = mapfn(mapassign[fast], t, false)
case t.Elem().Size() > zeroValSize:
case t.Elem().Size() > abi.MaxZero:
args = append(args, reflectdata.ZeroAddr(t.Elem().Size()))
mapFn = mapfn("mapaccess1_fat", t, true)
default:
Expand Down
1 change: 0 additions & 1 deletion src/cmd/compile/internal/walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

// The constant is known to runtime.
const tmpstringbufsize = 32
const zeroValSize = 1024 // must match value of runtime/map.go:maxZero

func Walk(fn *ir.Func) {
ir.CurFunc = fn
Expand Down
2 changes: 2 additions & 0 deletions src/internal/abi/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ const (
MapMaxKeyBytes = 128 // Must fit in a uint8.
MapMaxElemBytes = 128 // Must fit in a uint8.
)

const MaxZero = 1024
11 changes: 4 additions & 7 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ func (v Value) IsZero() bool {
}
typ := (*abi.ArrayType)(unsafe.Pointer(v.typ()))
// If the type is comparable, then compare directly with zero.
if typ.Equal != nil && typ.Size() <= maxZero {
if typ.Equal != nil && typ.Size() <= abi.MaxZero {
// v.ptr doesn't escape, as Equal functions are compiler generated
// and never escape. The escape analysis doesn't know, as it is a
// function pointer call.
Expand Down Expand Up @@ -1631,7 +1631,7 @@ func (v Value) IsZero() bool {
}
typ := (*abi.StructType)(unsafe.Pointer(v.typ()))
// If the type is comparable, then compare directly with zero.
if typ.Equal != nil && typ.Size() <= maxZero {
if v.typ().Equal != nil && v.typ().Size() <= abi.MaxZero {
// See noescape justification above.
return typ.Equal(noescape(v.ptr), unsafe.Pointer(&zeroVal[0]))
}
Expand Down Expand Up @@ -3277,7 +3277,7 @@ func Zero(typ Type) Value {
fl := flag(t.Kind())
if t.IfaceIndir() {
var p unsafe.Pointer
if t.Size() <= maxZero {
if t.Size() <= abi.MaxZero {
p = unsafe.Pointer(&zeroVal[0])
} else {
p = unsafe_New(t)
Expand All @@ -3287,11 +3287,8 @@ func Zero(typ Type) Value {
return Value{t, nil, fl}
}

// must match declarations in runtime/map.go.
const maxZero = 1024

//go:linkname zeroVal runtime.zeroVal
var zeroVal [maxZero]byte
var zeroVal [abi.MaxZero]byte

// New returns a Value representing a pointer to a new zero value
// for the specified type. That is, the returned Value's Type is PointerTo(typ).
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,7 @@ func reflectlite_maplen(h *hmap) int {
return h.count
}

const maxZero = 1024 // must match value in reflect/value.go:maxZero cmd/compile/internal/gc/walk.go:zeroValSize
var zeroVal [maxZero]byte
var zeroVal [abi.MaxZero]byte

// mapinitnoop is a no-op function known the Go linker; if a given global
// map (of the right size) is determined to be dead, the linker will
Expand Down

0 comments on commit 89475c8

Please sign in to comment.