]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/abi/switch.go
cmd/compile: add a cache to interface type switches
[gostls13.git] / src / internal / abi / switch.go
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package abi
6
7 type InterfaceSwitch struct {
8         Cache  *InterfaceSwitchCache
9         NCases int
10
11         // Array of NCases elements.
12         // Each case must be a non-empty interface type.
13         Cases [1]*InterfaceType
14 }
15
16 type InterfaceSwitchCache struct {
17         Mask    uintptr                      // mask for index. Must be a power of 2 minus 1
18         Entries [1]InterfaceSwitchCacheEntry // Mask+1 entries total
19 }
20
21 type InterfaceSwitchCacheEntry struct {
22         // type of source value (a *Type)
23         Typ uintptr
24         // case # to dispatch to
25         Case int
26         // itab to use for resulting case variable (a *runtime.itab)
27         Itab uintptr
28 }
29
30 const go122InterfaceSwitchCache = true
31
32 func UseInterfaceSwitchCache(goarch string) bool {
33         if !go122InterfaceSwitchCache {
34                 return false
35         }
36         // We need an atomic load instruction to make the cache multithreaded-safe.
37         // (AtomicLoadPtr needs to be implemented in cmd/compile/internal/ssa/_gen/ARCH.rules.)
38         switch goarch {
39         case "amd64", "arm64", "loong64", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "riscv64", "s390x":
40                 return true
41         default:
42                 return false
43         }
44 }