You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailscale/util
Joe Tsai 9d0c86b6ec
util/deephash: remove unnecessary formatting for structs and slices (#2571)
The index for every struct field or slice element and
the number of fields for the struct is unncessary.

The hashing of Go values is unambiguous because every type (except maps)
encodes in a parsable manner. So long as we know the type information,
we could theoretically decode every value (except for maps).

At a high level:
* numbers are encoded as fixed-width records according to precision.
* strings (and AppendTo output) are encoded with a fixed-width length,
followed by the contents of the buffer.
* slices are prefixed by a fixed-width length, followed by the encoding
of each value. So long as we know the type of each element, we could
theoretically decode each element.
* arrays are encoded just like slices, but elide the length
since it is determined from the Go type.
* maps are encoded first with a byte indicating whether it is a cycle.
If a cycle, it is followed by a fixed-width index for the pointer,
otherwise followed by the SHA-256 hash of its contents. The encoding of maps
is not decodeable, but a SHA-256 hash is sufficient to avoid ambiguities.
* interfaces are encoded first with a byte indicating whether it is nil.
If not nil, it is followed by a fixed-width index for the type,
and then the encoding for the underlying value. Having the type be encoded
first ensures that the value could theoretically be decoded next.
* pointers are encoded first with a byte indicating whether it is
1) nil, 2) a cycle, or 3) newly seen. If a cycle, it is followed by
a fixed-width index for the pointer. If newly seen, it is followed by
the encoding for the pointed-at value.

Removing unnecessary details speeds up hashing:

	name              old time/op    new time/op    delta
	Hash-8              76.0µs ± 1%    55.8µs ± 2%  -26.62%        (p=0.000 n=10+10)
	HashMapAcyclic-8    61.9µs ± 0%    62.0µs ± 0%     ~             (p=0.666 n=9+9)
	TailcfgNode-8       10.2µs ± 1%     7.5µs ± 1%  -26.90%         (p=0.000 n=10+9)
	HashArray-8         1.07µs ± 1%    0.70µs ± 1%  -34.67%         (p=0.000 n=10+9)

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
3 years ago
..
cibuild wgengine/magicsock: increase legacy ping timeout again 3 years ago
cmpver util/cmpver: move into OSS from corp repo. 3 years ago
deephash util/deephash: remove unnecessary formatting for structs and slices (#2571) 3 years ago
dnsname util/dnsname: make ToFQDN take exactly 0 or 1 allocs for everything. 3 years ago
endian util/endian: add Native variable to get the platform's native binary.ByteOrder 4 years ago
groupmember cmd/tailscale/web: add support for QNAP 3 years ago
jsonutil util/jsonutil: new package 4 years ago
lineread util/lineread: add docs to Reader 4 years ago
osshare ipn/ipnlocal: add file sharing to windows shell 3 years ago
pidowner util/pidowner: add missing copyright header 4 years ago
racebuild util/racebuild: add package to export a race-is-enabled const 4 years ago
systemd util/systemd: explicitly check for os.ErrNotExist from sdnotify 3 years ago
uniq util/uniq: add new package 4 years ago
winutil net/dns: do not run wsl.exe as LocalSystem 3 years ago