...

Source file src/cmd/compile/internal/types2/lookup_test.go

Documentation: cmd/compile/internal/types2

     1  // Copyright 2022 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 types2_test
     6  
     7  import (
     8  	"path/filepath"
     9  	"runtime"
    10  	"testing"
    11  
    12  	. "cmd/compile/internal/types2"
    13  )
    14  
    15  // BenchmarkLookupFieldOrMethod measures types.LookupFieldOrMethod performance.
    16  // LookupFieldOrMethod is a performance hotspot for both type-checking and
    17  // external API calls.
    18  func BenchmarkLookupFieldOrMethod(b *testing.B) {
    19  	// Choose an arbitrary, large package.
    20  	path := filepath.Join(runtime.GOROOT(), "src", "net", "http")
    21  
    22  	files, err := pkgFiles(path)
    23  	if err != nil {
    24  		b.Fatal(err)
    25  	}
    26  
    27  	conf := Config{
    28  		Importer: defaultImporter(),
    29  	}
    30  
    31  	pkg, err := conf.Check("http", files, nil)
    32  	if err != nil {
    33  		b.Fatal(err)
    34  	}
    35  
    36  	scope := pkg.Scope()
    37  	names := scope.Names()
    38  
    39  	// Look up an arbitrary name for each type referenced in the package scope.
    40  	lookup := func() {
    41  		for _, name := range names {
    42  			typ := scope.Lookup(name).Type()
    43  			LookupFieldOrMethod(typ, true, pkg, "m")
    44  		}
    45  	}
    46  
    47  	// Perform a lookup once, to ensure that any lazily-evaluated state is
    48  	// complete.
    49  	lookup()
    50  
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		lookup()
    54  	}
    55  }
    56  

View as plain text