diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2023-08-31 13:02:58 -0700 |
|---|---|---|
| committer | Joe Tsai <joetsai@digital-static.net> | 2023-09-06 13:03:51 -0700 |
| commit | 8841fd58c95cd335c5cf531ad08e978ca9c87c76 (patch) | |
| tree | 621414b273badfcf682c85dc55b0b169bf5ada0f /util/codegen | |
| parent | ac7b4d62fdc6ff589d894cc2fbac3e47c9c674c6 (diff) | |
| download | tailscale-dsnet/viewer-jsonv2.tar.xz tailscale-dsnet/viewer-jsonv2.zip | |
cmd/viewer: support v2 JSON methodsdsnet/viewer-jsonv2
This links in github.com/go-json-experiment/json into tailscaled.
After this change, the tailscaled binary on GOOS=linux and GOARCH=amd64
increases by ~85KiB.
The v2 marshal/unmarshal methods avoids a O(n^2) behavior
with deeply nested v1 MarshalJSON and UnmarshalJSON calls,
since each call requires the encoding/json package to rescan
the entire JSON value. Our data structures are not so deep
that the O(n^2) behavior becomes notable,
but this does provide about a ~20% performance benefit.
Updates tailscale/corp#14379
Signed-off-by: Joe Tsai <joetsai@digital-static.net>
Diffstat (limited to 'util/codegen')
| -rw-r--r-- | util/codegen/codegen.go | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/util/codegen/codegen.go b/util/codegen/codegen.go index cf848b1d2..e560bfb00 100644 --- a/util/codegen/codegen.go +++ b/util/codegen/codegen.go @@ -74,12 +74,20 @@ func NewImportTracker(thisPkg *types.Package) *ImportTracker { // ImportTracker provides a mechanism to track and build import paths. type ImportTracker struct { thisPkg *types.Package - packages map[string]bool + packages map[string]string // package paths to package names; empty name to use default } func (it *ImportTracker) Import(pkg string) { - if pkg != "" && !it.packages[pkg] { - mak.Set(&it.packages, pkg, true) + _, ok := it.packages[pkg] + if pkg != "" && !ok { + mak.Set(&it.packages, pkg, "") + } +} + +func (it *ImportTracker) ImportNamed(pkg, name string) { + _, ok := it.packages[pkg] + if pkg != "" && !ok { + mak.Set(&it.packages, pkg, name) } } @@ -100,8 +108,12 @@ func (it *ImportTracker) QualifiedName(t types.Type) string { // Write prints all the tracked imports in a single import block to w. func (it *ImportTracker) Write(w io.Writer) { fmt.Fprintf(w, "import (\n") - for s := range it.packages { - fmt.Fprintf(w, "\t%q\n", s) + for pkgPath, pkgName := range it.packages { + if pkgName == "" { + fmt.Fprintf(w, "\t%q\n", pkgPath) + } else { + fmt.Fprintf(w, "\t%s %q\n", pkgName, pkgPath) + } } fmt.Fprintf(w, ")\n\n") } |
