summaryrefslogtreecommitdiffhomepage
path: root/types/views
AgeCommit message (Collapse)AuthorFilesLines
2025-08-19types/views: add min/max helpers to views.SliceAdrian Dewhurst1-0/+29
This has come up in a few situations recently and adding these helpers is much better than copying the slice (calling AsSlice()) in order to use slices.Max and friends. Updates #cleanup Change-Id: Ib289a07d23c3687220c72c4ce341b9695cd875bf Signed-off-by: Adrian Dewhurst <adrian@tailscale.com>
2025-08-14cmd/viewer, types/views: implement support for json/v2 (#16852)Joe Tsai2-62/+196
This adds support for having every viewer type implement jsonv2.MarshalerTo and jsonv2.UnmarshalerFrom. This provides a significant boost in performance as the json package no longer needs to validate the entirety of the JSON value outputted by MarshalJSON, nor does it need to identify the boundaries of a JSON value in order to call UnmarshalJSON. For deeply nested and recursive MarshalJSON or UnmarshalJSON calls, this can improve runtime from O(N²) to O(N). This still references "github.com/go-json-experiment/json" instead of the experimental "encoding/json/v2" package now available in Go 1.25 under goexperiment.jsonv2 so that code still builds without the experiment tag. Of note, the "github.com/go-json-experiment/json" package aliases the standard library under the right build conditions. Updates tailscale/corp#791 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2025-01-30types/views: make SliceEqualAnyOrder also do short slice optimizationBrad Fitzpatrick2-59/+162
SliceEqualAnyOrderFunc had an optimization missing from SliceEqualAnyOrder. Now they share the same code and both have the optimization. Updates #14593 Change-Id: I550726e0964fc4006e77bb44addc67be989c131c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2025-01-28types/views: fix SliceEqualAnyOrderFunc short optimizationAndrew Dunham2-6/+64
This was flagged by @tkhattra on the merge commit; thanks! Updates tailscale/corp#25479 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: Ia8045640f02bd4dcc0fe7433249fd72ac6b9cf52
2025-01-14cmd/viewer,types/views,various: avoid allocations in pointer field getters ↵Nick Khyl1-0/+80
whenever possible In this PR, we add a generic views.ValuePointer type that can be used as a view for pointers to basic types and struct types that do not require deep cloning and do not have corresponding view types. Its Get/GetOk methods return stack-allocated shallow copies of the underlying value. We then update the cmd/viewer codegen to produce getters that return either concrete views when available or ValuePointer views when not, for pointer fields in generated view types. This allows us to avoid unnecessary allocations compared to returning pointers to newly allocated shallow copies. Updates #14570 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2025-01-09types/views: optimize SliceEqualAnyOrderFunc for small slicesAndrew Dunham2-0/+32
If the total number of differences is less than a small amount, just do the dumb quadratic thing and compare every single object instead of allocating a map. Updates tailscale/corp#25479 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I8931b4355a2da4ec0f19739927311cf88711a840
2025-01-09types/views: add SliceEqualAnyOrderFuncAndrew Dunham2-0/+72
Extracted from some code written in the other repo. Updates tailscale/corp#25479 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I6df062fdffa1705524caa44ac3b6f2788cf64595
2025-01-08types/views: add MapViewsEqual and MapViewsEqualFuncAndrew Dunham2-0/+126
Extracted from some code written in the other repo. Updates tailscale/corp#25479 Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I92c97a63a8f35cace6e89a730938ea587dcefd9b
2025-01-04types/views: remove various Map Range funcs; use iterators everywhereBrad Fitzpatrick1-30/+0
The remaining range funcs in the tree are RangeOverTCPs and RangeOverWebs in ServeConfig; those will be cleaned up separately. Updates #12912 Change-Id: Ieeae4864ab088877263c36b805f77aa8e6be938d Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-10-09types/views: add iterators to the three Map view typesBrad Fitzpatrick2-0/+77
Their callers using Range are all kinda clunky feeling. Iterators should make them more readable. Updates #12912 Change-Id: I93461eba8e735276fda4a8558a4ae4bfd6c04922 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-09-20types/views: add SliceView.All iterator (#13536)Joe Tsai2-0/+43
And convert a all relevant usages. Updates #12912 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2024-08-22types/views: add Slice.All iteratorBrad Fitzpatrick2-0/+26
And convert a few callers as an example, but nowhere near all. Updates #12912 Change-Id: I5eaa12a29a6cd03b58d6f1072bd27bc0467852f2 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-07-19cmd/viewer, types/views, util/codegen: add viewer support for custom ↵Nick Khyl2-0/+274
container types This adds support for container-like types such as Container[T] that don't explicitly specify a view type for T. Instead, a package implementing a container type should also implement and export a ContainerView[T, V] type and a ContainerViewOf(*Container[T]) ContainerView[T, V] function, which returns a view for the specified container, inferring the element view type V from the element type T. Updates #12736 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-06-15cmd/viewer,type/views: add MapSlice for maps of slicesMaisem Ali1-5/+98
This abstraction provides a nicer way to work with maps of slices without having to write out three long type params. This also allows it to provide an AsMap implementation which copies the map and the slices at least. Updates tailscale/corp#20910 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-05-11types/views: move AsMap to Map from *MapMaisem Ali1-2/+2
This was a typo in 2e19790f611cd15d603eac35bae46cb3c707ef85. It should have been on `Map` and not on `*Map` as otherwise it doesn't allow for chaining like `someView.SomeMap().AsMap()` and requires first assigning it to a variable. Updates #typo Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-05-03types/views: add AppendStrings util funcMaisem Ali1-0/+9
Updates tailscale/corp#19623 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-05-03types/views: remove duplicate SliceContainsFuncMaisem Ali2-13/+1
We already have `(Slice[T]).ContainsFunc`. Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-04-30types/views: use slices.Contains{,Func}Brad Fitzpatrick1-12/+2
Updates #8419 Change-Id: Ib1a9cb3fb425284b7e02684072a4e7a35975f35c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-04-16all: use Go 1.22 range-over-intBrad Fitzpatrick2-4/+4
Updates #11058 Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-03-18types/views: optimize slices contains under some conditions (#11449)James Tucker1-6/+6
In control there are conditions where the leaf functions are not being optimized away (i.e. At is not inlined), resulting in undesirable time spent copying during SliceContains. This optimization is likely irrelevant to simpler code or smaller structures. Updates #optimization Signed-off-by: James Tucker <james@tailscale.com>
2024-02-25all: remove LenIter, use Go 1.22 range-over-int insteadBrad Fitzpatrick2-36/+0
Updates #11058 Updates golang/go#65685 Change-Id: Ibb216b346e511d486271ab3d84e4546c521e4e22 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-02-12types/views: add test that LenIter doesn't allocateBrad Fitzpatrick1-0/+9
For a second we thought this was allocating but we were looking at a CPU profile (which showed calls to mallocgc view makeslice) instead of the alloc profile. Updates golang/go#65685 (which if fixed wouldn't have confused us) Change-Id: Ic0132310d52d8a65758a516142525339aa23b1ed Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-02-06types/views: add SliceMapKey[T]Maisem Ali2-0/+70
views.Slice are meant to be immutable, and if used as such it is at times desirable to use them as a key in a map. For non-viewed slices it was kinda doable by creating a custom key struct but views.Slice didn't allow for the same so add a method to create that struct here. Updates tailscale/corp#17122 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-09-18types/views: add SliceContainsFunc like slices.ContainsFuncBrad Fitzpatrick2-0/+12
Needed for a future change. Updates #cleanup Change-Id: I6d89ee8a048b3bb1eb9cfb2e5a53c93aed30b021 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-08-27types/views: add SliceEqual, like std slices.EqualBrad Fitzpatrick2-0/+20
Updates tailscale/corp#6198 Change-Id: I38614a4552c9fa933036aa493c7cdb57c7ffe2d2 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-08-20types/views,cmd/viewer: add ByteSlice[T] to replace mem.ROMaisem Ali1-2/+83
Add a new views.ByteSlice[T ~[]byte] to provide a better API to use with views. Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-18cmd/viewer, types/views, all: un-special case slice of netip.PrefixBrad Fitzpatrick2-86/+4
Make it just a views.Slice[netip.Prefix] instead of its own named type. Having the special case led to circular dependencies in another WIP PR of mine. Updates #8948 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-08-18types/views: add LenIter method to slice view typesBrad Fitzpatrick2-0/+27
This is basically https://github.com/bradfitz/iter which was a joke but now that Go's adding range over int soonish, might as well. It simplies our code elsewher that uses slice views. Updates #8948 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-08-17all: use Go 1.21 slices, maps instead of x/exp/{slices,maps}Brad Fitzpatrick1-2/+2
Updates #8419 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-06-29types/views: add JSON marshal/unmarshal and AsMap to MapMaisem Ali1-3/+28
This allows cloning a Map as well as marshaling the Map as JSON. Updates tailscale/corp#12754 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-06-20types/views: add Slice methods on Slice viewsBrad Fitzpatrick2-0/+27
Updates #cleanup for change elsewhere. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-01-27all: update copyright and license headersWill Norris2-6/+4
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
2023-01-11types/views: add SliceEqualAnyOrder funcWill Norris2-0/+35
This is based on the tagsEqual func from corp/control/control.go, moved here so that it can be reused in other places. Signed-off-by: Will Norris <will@tailscale.com>
2022-12-05net/tsaddr: remove ContainsFunc helpers (they're now in x/exp/slices)Brad Fitzpatrick1-1/+2
x/exp/slices now has ContainsFunc (golang/go#53983) so we can delete our versions. Change-Id: I5157a403bfc1b30e243bf31c8b611da25e995078 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-10-24ipn/prefs: add viewsMaisem Ali1-0/+5
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-10-12types/view: add ContainsNonExitSubnetRoutes funcSonia Appasamy1-0/+11
Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
2022-07-26types/views: add BenchmarkSliceIterationMaisem Ali1-8/+48
``` goos: darwin goarch: arm64 pkg: tailscale.com/types/views BenchmarkSliceIteration/Len-10 340093 3212 ns/op 0 B/op 0 allocs/op BenchmarkSliceIteration/Cached-Len-10 366727 3211 ns/op 0 B/op 0 allocs/op BenchmarkSliceIteration/direct-10 361561 3290 ns/op 0 B/op 0 allocs/op PASS ok tailscale.com/types/views 3.662s ``` Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-07-25all: convert more code to use net/netip directlyBrad Fitzpatrick2-13/+12
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.) perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. ) goimports -w . Then delete some stuff from the net/netaddr shim package which is no longer neeed. Updates #5162 Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25all: use various net/netip parse funcs directlyBrad Fitzpatrick1-1/+2
Mechanical change with perl+goimports. Changed {Must,}Parse{IP,IPPrefix,IPPort} to their netip variants, then goimports -d . Finally, removed the net/netaddr wrappers, to prevent future use. Updates #5162 Change-Id: I59c0e38b5fbca5a935d701645789cddf3d7863ad Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25net/netaddr: start migrating to net/netip via new netaddr adapter packageBrad Fitzpatrick2-2/+2
Updates #5162 Change-Id: Id7bdec303b25471f69d542f8ce43805328d56c12 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-21types/views: add SliceContains, View.ContainsFunc, View.IndexFuncBrad Fitzpatrick2-0/+50
We were starting to write these elsewhere as little unexported copies in misc places. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-05-10types/views: remove alloc in hot pathMaisem Ali2-85/+4
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-05-09types/views: make SliceOf/MapOf panic if they see a pointerMaisem Ali2-9/+104
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-05-09cmd/viewer,types/views: add support for views of mapsMaisem Ali1-0/+103
Updates #4635 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-05-06cmd/viewer: add codegen tool for ViewsMaisem Ali1-10/+73
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-03-17types/views: rename Generic to UnwrapMaisem Ali1-2/+2
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-03-16types/views: add generic Slice[T] and remove StringSliceMaisem Ali2-45/+41
Also make IPPrefixSliceOf use Slice[netaddr.IPPrefix] as it also provides additional functions besides the standard ones provided by Slice[T]. Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-03-04types/views: add ContainsExitRoutes to IPPrefixSliceMaisem Ali1-0/+5
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-02-22ipn/ipnlocal: use views for Peer.PrimaryRoutes and Peer.TagsMaisem Ali2-0/+120
RELNOTE=`tailscale status --json` now shows Tags and PrimaryRoutes Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-02-16tailcfg: introduce HostinfoViewMaisem Ali1-0/+74
Signed-off-by: Maisem Ali <maisem@tailscale.com>