tailscale/cmd
Joe Tsai d145c594ad
util/deephash: improve cycle detection (#2470)
The previous algorithm used a map of all visited pointers.
The strength of this approach is that it quickly prunes any nodes
that we have ever visited before. The detriment of the approach
is that pruning is heavily dependent on the order that pointers
were visited. This is especially relevant for hashing a map
where map entries are visited in a non-deterministic manner,
which would cause the map hash to be non-deterministic
(which defeats the point of a hash).

This new algorithm uses a stack of all visited pointers,
similar to how github.com/google/go-cmp performs cycle detection.
When we visit a pointer, we push it onto the stack, and when
we leave a pointer, we pop it from the stack.
Before visiting a pointer, we first check whether the pointer exists
anywhere in the stack. If yes, then we prune the node.
The detriment of this approach is that we may hash a node more often
than before since we do not prune as aggressively.

The set of visited pointers up until any node is only the
path of nodes up to that node and not any other pointers
that may have been visited elsewhere. This provides us
deterministic hashing regardless of visit order.
We can now delete hashMapFallback and associated complexity,
which only exists because the previous approach was non-deterministic
in the presence of cycles.

This fixes a failure of the old algorithm where obviously different
values are treated as equal because the pruning was too aggresive.
See https://github.com/tailscale/tailscale/issues/2443#issuecomment-883653534

The new algorithm is slightly slower since it prunes less aggresively:
	name              old time/op    new time/op    delta
	Hash-8              66.1µs ± 1%    68.8µs ± 1%   +4.09%        (p=0.000 n=19+19)
	HashMapAcyclic-8    63.0µs ± 1%    62.5µs ± 1%   -0.76%        (p=0.000 n=18+19)
	TailcfgNode-8       9.79µs ± 2%    9.88µs ± 1%   +0.95%        (p=0.000 n=19+17)
	HashArray-8          643ns ± 1%     653ns ± 1%   +1.64%        (p=0.000 n=19+19)
However, a slower but more correct algorithm seems
more favorable than a faster but incorrect algorithm.

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2021-07-22 15:22:48 -07:00
..
addlicense cmd/addlicense: add command to add licenseheaders to generated code 2021-07-19 15:31:56 -07:00
cloner cmd/cloner: support maps with clone ptrs 2021-06-22 22:11:38 -07:00
derper cmd/derper: remove default for non-root users 2021-07-14 17:48:31 -07:00
derpprobe cmd/derpprobe: add in a delay to wait for mesh info to sync 2021-07-19 07:52:55 -07:00
hello all: adapt to opaque netaddr types 2021-05-16 14:52:00 -07:00
microproxy tsweb: replace NewMux with a more flexible DebugHandler. 2021-06-16 19:00:47 -07:00
mkpkg cmd/mkpkg: allow zero files in a package. 2021-06-07 16:22:23 -07:00
speedtest Implemented Commandline Download Speedtest (#2064) 2021-07-15 14:43:13 -04:00
tailscale cmd/tailscale/cli: allow effective GOOS to be changed for integration tests 2021-07-19 15:41:35 -07:00
tailscaled util/deephash: improve cycle detection (#2470) 2021-07-22 15:22:48 -07:00
tsshd net/interfaces: return all Tailscale addresses from Tailscale(). 2021-04-29 12:25:47 -07:00