mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-16 03:31:39 +00:00
types/views: optimize slices contains under some conditions (#11449)
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>
This commit is contained in:
parent
d2ccfa4edd
commit
7fe4cbbaf3
@ -274,8 +274,8 @@ func (v Slice[T]) IndexFunc(f func(T) bool) int {
|
|||||||
//
|
//
|
||||||
// As it runs in O(n) time, use with care.
|
// As it runs in O(n) time, use with care.
|
||||||
func (v Slice[T]) ContainsFunc(f func(T) bool) bool {
|
func (v Slice[T]) ContainsFunc(f func(T) bool) bool {
|
||||||
for i := 0; i < v.Len(); i++ {
|
for _, x := range v.ж {
|
||||||
if f(v.At(i)) {
|
if f(x) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -286,8 +286,8 @@ func (v Slice[T]) ContainsFunc(f func(T) bool) bool {
|
|||||||
//
|
//
|
||||||
// As it runs in O(n) time, use with care.
|
// As it runs in O(n) time, use with care.
|
||||||
func SliceContains[T comparable](v Slice[T], e T) bool {
|
func SliceContains[T comparable](v Slice[T], e T) bool {
|
||||||
for i := 0; i < v.Len(); i++ {
|
for _, x := range v.ж {
|
||||||
if v.At(i) == e {
|
if x == e {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,8 +296,8 @@ func SliceContains[T comparable](v Slice[T], e T) bool {
|
|||||||
|
|
||||||
// SliceContainsFunc reports whether f reports true for any element in v.
|
// SliceContainsFunc reports whether f reports true for any element in v.
|
||||||
func SliceContainsFunc[T any](v Slice[T], f func(T) bool) bool {
|
func SliceContainsFunc[T any](v Slice[T], f func(T) bool) bool {
|
||||||
for i := 0; i < v.Len(); i++ {
|
for _, x := range v.ж {
|
||||||
if f(v.At(i)) {
|
if f(x) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user