Skip to content

Commit

Permalink
nestedStack
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 30, 2023
1 parent d9bfa48 commit 2ad13ef
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
17 changes: 12 additions & 5 deletions interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,17 +1028,23 @@ func newInterp(ctx *Context, mainpkg *ssa.Package, globals map[string]interface{

func (i *Interp) loadType(typ types.Type) {
if _, ok := i.preloadTypes[typ]; !ok {
i.preloadTypes[typ] = i.record.ToType(typ)
rt, nested := i.record.toType(typ)
if nested {
return
}
i.preloadTypes[typ] = rt
}
}

func (i *Interp) preToType(typ types.Type) reflect.Type {
if t, ok := i.preloadTypes[typ]; ok {
return t
}
t := i.record.ToType(typ)
i.preloadTypes[typ] = t
return t
rt, nested := i.record.toType(typ)
if !nested {
i.preloadTypes[typ] = rt
}
return rt
}

func (i *Interp) toType(typ types.Type) reflect.Type {
Expand All @@ -1048,7 +1054,8 @@ func (i *Interp) toType(typ types.Type) reflect.Type {
// log.Panicf("toType %v %p\n", typ, typ)
i.typesMutex.Lock()
defer i.typesMutex.Unlock()
return i.record.ToType(typ)
rt, _ := i.record.toType(typ)
return rt
}

func (i *Interp) RunFunc(name string, args ...Value) (r Value, err error) {
Expand Down
7 changes: 6 additions & 1 deletion types_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ func hasTypeParam(t types.Type) bool {
return false
}

func (r *TypesRecord) SetFunction(fn *ssa.Function) {
type nestedStack struct {
}

func (r *TypesRecord) EnterInstance(fn *ssa.Function) {
}
func (r *TypesRecord) LeaveInstance(fn *ssa.Function) {
}

func (r *TypesRecord) extractNamed(named *types.Named, totype bool) (pkgpath string, name string, typeargs bool, nested bool) {
Expand Down
39 changes: 34 additions & 5 deletions types_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ func hasTypeParam(t types.Type) bool {
return false
}

type nestedStack struct {
targs []string
cache []*typeutil.Map
}

func (s *nestedStack) Push(targs string) *typeutil.Map {
s.targs = append(s.targs, targs)
m := &typeutil.Map{}
s.cache = append(s.cache, m)
return m
}

func (s *nestedStack) Pop() (targs string, cache *typeutil.Map) {
n := len(s.targs)
if n >= 1 {
targs = s.targs[n-1]
cache = s.cache[n-1]
}
s.targs = s.targs[:n-1]
s.cache = s.cache[:n-1]
return
}

func (r *TypesRecord) typeId(t reflect.Type, nested bool) string {
path := t.PkgPath()
if path == "" {
Expand All @@ -38,9 +61,14 @@ func (r *TypesRecord) typeId(t reflect.Type, nested bool) string {
return path + "." + t.Name()
}

func (r *TypesRecord) SetFunction(fn *ssa.Function) {
func (r *TypesRecord) EnterInstance(fn *ssa.Function) {
r.fntargs = ""
r.fntargs = r.parseFuncTypeArgs(fn)
r.ncache = &typeutil.Map{}
r.ncache = r.nstack.Push(r.fntargs)
}

func (r *TypesRecord) LeaveInstance(fn *ssa.Function) {
r.fntargs, r.ncache = r.nstack.Pop()
}

func (r *TypesRecord) parseFuncTypeArgs(fn *ssa.Function) (targs string) {
Expand All @@ -67,7 +95,7 @@ func (r *TypesRecord) extractNamed(named *types.Named, totype bool) (pkgpath str
pkgpath = pkg.Path()
}
}
if r.fntargs != "" && named.TypeParams() != nil && r.nested[named.Origin()] != 0 {
if r.fntargs != "" && r.nested[named.Origin()] != 0 {
nested = true
}
name = obj.Name()
Expand Down Expand Up @@ -97,8 +125,9 @@ func (r *TypesRecord) extractNamed(named *types.Named, totype bool) (pkgpath str
func (r *TypesRecord) LookupReflect(typ types.Type) (rt reflect.Type, ok bool, nested bool) {
rt, ok = r.loader.LookupReflect(typ)
if !ok {
if r.fntargs != "" {
if rt := r.ncache.At(typ); rt != nil {
n := len(r.nstack.cache)
for i := n; i > 0; i-- {
if rt := r.nstack.cache[i-1].At(typ); rt != nil {
return rt.(reflect.Type), true, true
}
}
Expand Down
5 changes: 4 additions & 1 deletion visit.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func (visit *visitor) function(fn *ssa.Function) {
return
}
visit.seen[fn] = true
visit.intp.record.SetFunction(fn)
fnPath := fn.String()
if f, ok := visit.intp.ctx.override[fnPath]; ok &&
visit.intp.preToType(fn.Type()) == f.Type() {
Expand All @@ -107,6 +106,10 @@ func (visit *visitor) function(fn *ssa.Function) {
}
return
}
if len(fn.TypeArgs()) != 0 {
visit.intp.record.EnterInstance(fn)
defer visit.intp.record.LeaveInstance(fn)
}
visit.intp.loadType(fn.Type())
for _, alloc := range fn.Locals {
visit.intp.loadType(alloc.Type())
Expand Down
11 changes: 3 additions & 8 deletions xtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type TypesRecord struct {
ncache *typeutil.Map //nested cache
fntargs string //reflect type arguments used to instantiate the current func
nested map[*types.Named]int
nstack nestedStack
}

func NewTypesRecord(loader Loader, finder FindMethod, nested map[*types.Named]int) *TypesRecord {
Expand All @@ -169,7 +170,6 @@ func NewTypesRecord(loader Loader, finder FindMethod, nested map[*types.Named]in
finder: finder,
rcache: make(map[reflect.Type]types.Type),
tcache: &typeutil.Map{},
ncache: &typeutil.Map{},
nested: nested,
}
}
Expand Down Expand Up @@ -197,11 +197,6 @@ func (r *TypesRecord) saveType(typ types.Type, rt reflect.Type, nested bool) {
return
}

func (r *TypesRecord) ToType(typ types.Type) (rt reflect.Type) {
rt, _ = r.toType(typ)
return
}

func (r *TypesRecord) toType(typ types.Type) (reflect.Type, bool) {
if rt, ok, nested := r.LookupReflect(typ); ok {
return rt, nested
Expand Down Expand Up @@ -457,8 +452,8 @@ func toReflectChanDir(d types.ChanDir) reflect.ChanDir {
return 0
}

func (r *TypesRecord) LoadType(typ types.Type) reflect.Type {
return r.ToType(typ)
func (r *TypesRecord) LoadType(typ types.Type) {
r.toType(typ)
}

func (r *TypesRecord) Load(pkg *ssa.Package) {
Expand Down

0 comments on commit 2ad13ef

Please sign in to comment.