| 1 | // Copyright 2024 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 objectpath_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "go/types" |
| 11 | "testing" |
| 12 | |
| 13 | "golang.org/x/tools/go/types/objectpath" |
| 14 | "golang.org/x/tools/internal/testfiles" |
| 15 | "golang.org/x/tools/txtar" |
| 16 | ) |
| 17 | |
| 18 | // BenchmarkMonster tests that objectpath operations on monstrously |
| 19 | // large packages complete in linear time and space. |
| 20 | func BenchmarkMonster(b *testing.B) { |
| 21 | // Construct a monster package. |
| 22 | const ( |
| 23 | numTypes = 5000 |
| 24 | numFields = 10 |
| 25 | numMethods = 10 |
| 26 | ) |
| 27 | var buf bytes.Buffer |
| 28 | buf.WriteString("-- go.mod --\nmodule x.io\n\n-- a/a.go --\npackage a\n") |
| 29 | for i := range numTypes { |
| 30 | fmt.Fprintf(&buf, "type T%d struct {\n", i) |
| 31 | for j := range numFields { |
| 32 | fmt.Fprintf(&buf, "\tF%d int\n", j) |
| 33 | } |
| 34 | fmt.Fprintf(&buf, "}\n") |
| 35 | for j := range numMethods { |
| 36 | fmt.Fprintf(&buf, "func (T%d) M%d() {}\n", i, j) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Load it. |
| 41 | ar := txtar.Parse(buf.Bytes()) |
| 42 | pkgs := testfiles.LoadPackages(b, ar, "./a") |
| 43 | if len(pkgs) != 1 { |
| 44 | b.Fatalf("expected 1 package, got %d", len(pkgs)) |
| 45 | } |
| 46 | pkg := pkgs[0].Types |
| 47 | scope := pkg.Scope() |
| 48 | |
| 49 | // Collect all methods and fields of named structs. |
| 50 | // All these symbols engage the "search" algorithm. |
| 51 | var all []types.Object |
| 52 | for _, name := range scope.Names() { |
| 53 | obj := scope.Lookup(name) |
| 54 | all = append(all, obj) |
| 55 | if named, ok := obj.Type().(*types.Named); ok { |
| 56 | for method := range named.Methods() { |
| 57 | all = append(all, method) |
| 58 | } |
| 59 | if s, ok := named.Underlying().(*types.Struct); ok { |
| 60 | for field := range s.Fields() { |
| 61 | all = append(all, field) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Single measures the cost of requesting paths for a single symbol, |
| 68 | // as happens whenever we use [objectpath.For] without an Encoder. |
| 69 | // This case must not allocate an index. |
| 70 | b.Run("Single", func(b *testing.B) { |
| 71 | // Pick a deep object (last field of the last type). |
| 72 | obj := all[len(all)-1] |
| 73 | for b.Loop() { |
| 74 | enc := new(objectpath.Encoder) |
| 75 | _, err := enc.For(obj) |
| 76 | if err != nil { |
| 77 | b.Fatal(err) |
| 78 | } |
| 79 | } |
| 80 | }) |
| 81 | |
| 82 | // Many measures the cost of requesting many symbols from the same encoder. |
| 83 | b.Run("Many", func(b *testing.B) { |
| 84 | const count = 100 |
| 85 | // Pick a set of objects including a mix of types, fields, and methods. |
| 86 | var targets []types.Object |
| 87 | for j := range count { |
| 88 | // Offset of 17 (prime) avoids locking stride |
| 89 | // with periodicity in monster package. |
| 90 | idx := (j*len(all)/count + 17) % len(all) |
| 91 | targets = append(targets, all[idx]) |
| 92 | } |
| 93 | |
| 94 | for b.Loop() { |
| 95 | enc := new(objectpath.Encoder) |
| 96 | for _, obj := range targets { |
| 97 | _, err := enc.For(obj) |
| 98 | if err != nil { |
| 99 | b.Fatal(err) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | }) |
| 104 | |
| 105 | // All measures performance when a large fraction (or all) |
| 106 | // objects in the API are requested. |
| 107 | // |
| 108 | // This happens when analyzers (e.g. checklocks) emit facts |
| 109 | // for a large fraction of symbols, and historically led to |
| 110 | // O(n²) running time; see https://go.dev/issue/78893. |
| 111 | b.Run("All", func(b *testing.B) { |
| 112 | for b.Loop() { |
| 113 | enc := new(objectpath.Encoder) |
| 114 | for _, obj := range all { |
| 115 | _, err := enc.For(obj) |
| 116 | if err != nil { |
| 117 | b.Fatal(err) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | }) |
| 122 | } |