go/internal/span
Alan Donovan 9651276d64 internal/lsp/cache: optimize Snapshot.clone
This change replaces the single large map used for snapshot.goFiles
by a map of 256 stripes, each of which becomes immutable once shared.
This optimizes the common case in which the copy is nearly identical
to the original.

We still need to visit each map entry to see whether it needs to be
deleted (which is rare) and to inherit the handle in the usual case.
This is now done concurrently.

Also, share the (logically immutable) []PackageIDs slices across
old and new snapshots. This was worth 5% of CPU and 1/3 of allocations
(all small).

Benchmark on darwin/arm64 shows a 29% reduction for DidChange.

$ go test -v ./gopls/internal/regtest/bench -run=TestBenchmarkDidChange -didchange_dir=$HOME/w/kubernetes -didchange_file=pkg/util/hash/hash.go

Before:
BenchmarkStatistics	     100	  22955469 ns/op	11308095 B/op	   47412 allocs/op
BenchmarkStatistics	     100	  23454630 ns/op	11226742 B/op	   46882 allocs/op
BenchmarkStatistics	     100	  23618532 ns/op	11258619 B/op	   47068 allocs/op

After goFilesMap:
BenchmarkStatistics	     100	  16643972 ns/op	 8770787 B/op	   46238 allocs/op
BenchmarkStatistics	     100	  17805864 ns/op	 8862926 B/op	   46762 allocs/op
BenchmarkStatistics	     100	  18618255 ns/op	 9308864 B/op	   49776 allocs/op

After goFilesMap and ids sharing:
BenchmarkStatistics          100          16703623 ns/op         8772626 B/op      33812 allocs/op
BenchmarkStatistics          100          16927378 ns/op         8529491 B/op      32328 allocs/op
BenchmarkStatistics          100          16632762 ns/op         8557533 B/op      32497 allocs/op

Also:
- Add comments documenting findings of profiling.
- preallocate slice for knownSubdirs.
- remove unwanted loop over slice in Generation.Inherit

Updates golang/go#45686

Change-Id: Id953699191b8404cf36ba3a7ab9cd78b1d19c0a2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/410176
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
2022-06-10 17:57:19 +00:00
..
parse.go internal/lsp/cache: parse filenames from go list errors correctly 2021-03-02 01:39:30 +00:00
span.go internal/span: eliminate TokenConverter 2022-06-01 19:19:15 +00:00
span_test.go internal/span: eliminate TokenConverter 2022-06-01 19:19:15 +00:00
token.go internal/span: eliminate TokenConverter 2022-06-01 19:19:15 +00:00
token_test.go internal/span: eliminate TokenConverter 2022-06-01 19:19:15 +00:00
uri.go internal/lsp/cache: optimize Snapshot.clone 2022-06-10 17:57:19 +00:00
uri_test.go all: go fmt ./... 2021-02-20 03:28:52 +00:00
uri_windows_test.go all: go fmt ./... 2021-02-20 03:28:52 +00:00
utf16.go internal/lsp/mod: fix broken assumptions about file base 2022-06-01 22:04:06 +00:00
utf16_test.go internal/span: remove check for nil content 2020-09-02 00:13:12 +00:00