Skip to content

[v17] Reduce resource consumption of ListUnifiedResources#53302

Merged
rosstimothy merged 1 commit intobranch/v17from
tross/backport-53213/v17
Mar 21, 2025
Merged

[v17] Reduce resource consumption of ListUnifiedResources#53302
rosstimothy merged 1 commit intobranch/v17from
tross/backport-53213/v17

Conversation

@rosstimothy
Copy link
Copy Markdown
Contributor

Backport #53213 to branch/v17

Changelog: Improve resource consumption when retrieving resources via the Web UI or tsh ls.

The changes here were a result of examining CPU and memory profiles
of BenchmarkListUnifiedResources. The biggest inefficiencies stemmed
from the authorizer and `(types.Server) MatchSearch`.

When the authorizer computes lock targets it will call `(types.LockTarget) Equals`,
which was relying on proto.Equals to determine if nine strings were
equivalent. The fields are now individually checked and a test
was added to ensure that if any new fields are added they must
be added to the check for the test to pass.

ListUnifiedResources attempts to be smart and evaluate whether the
user has read/list access to a particular resource kind once. However,
it was always checking if users had access to _all_ supported resources
instead of just the resources that the user was requesting. While most
of the checks are not very expensive, checking all resources is still
worse than only checking the required resources.

Lastly, the `(types.Server) MatchSearch` implementation is no longer
allocating a slice of search fields and using `types.MatchSearch`.
Instead a variant of the loop from `types.MatchSearch` is inlined.
While this was only applied to `types.Server` it should likely
be extended to other resource types in the future - or `types.MatchSearch`
should be refactored in a way that does not require callers to allocate.

```bash
$ benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: github.com/gravitational/teleport/lib/auth
cpu: Apple M2 Pro
                                              │   old.txt   │               new.txt               │
                                              │   sec/op    │   sec/op     vs base                │
ListUnifiedResourcesFilter/labels-12            195.0m ± 6%   173.3m ± 2%  -11.12% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_path-12    230.6m ± 3%   210.8m ± 4%   -8.57% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_index-12   229.8m ± 5%   209.0m ± 7%   -9.08% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_lower-12      600.6m ± 3%   364.3m ± 1%  -39.35% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_upper-12      687.0m ± 2%   423.4m ± 1%  -38.37% (p=0.000 n=10)
geomean                                         335.7m        259.5m       -22.69%

                                              │    old.txt     │               new.txt                │
                                              │      B/op      │     B/op      vs base                │
ListUnifiedResourcesFilter/labels-12              868.8Ki ± 0%   414.1Ki ± 0%  -52.34% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_path-12      7.731Mi ± 0%   7.287Mi ± 0%   -5.74% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_index-12     7.731Mi ± 0%   7.288Mi ± 0%   -5.74% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_lower-12      22106.9Ki ± 0%   486.8Ki ± 0%  -97.80% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_upper-12      22107.0Ki ± 0%   486.3Ki ± 0%  -97.80% (p=0.000 n=10)
geomean                                           7.494Mi        1.371Mi       -81.70%

                                              │    old.txt    │               new.txt               │
                                              │   allocs/op   │  allocs/op   vs base                │
ListUnifiedResourcesFilter/labels-12             11.401k ± 0%   4.865k ± 0%  -57.32% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_path-12      161.5k ± 0%   155.0k ± 0%   -4.04% (p=0.000 n=10)
ListUnifiedResourcesFilter/predicate_index-12     161.5k ± 0%   155.0k ± 0%   -4.04% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_lower-12      162.212k ± 0%   5.277k ± 0%  -96.75% (p=0.000 n=10)
ListUnifiedResourcesFilter/search_upper-12      162.224k ± 0%   5.275k ± 0%  -96.75% (p=0.000 n=10)
geomean                                           95.22k        20.07k       -78.93%

```
@rosstimothy rosstimothy marked this pull request as ready for review March 21, 2025 20:16
@github-actions github-actions Bot added size/sm tsh tsh - Teleport's command line tool for logging into nodes running Teleport. labels Mar 21, 2025
@rosstimothy rosstimothy added this pull request to the merge queue Mar 21, 2025
Merged via the queue into branch/v17 with commit c5feaad Mar 21, 2025
47 checks passed
@rosstimothy rosstimothy deleted the tross/backport-53213/v17 branch March 21, 2025 21:25
@doggydogworld doggydogworld mentioned this pull request Mar 25, 2025
@camscale camscale mentioned this pull request Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport size/sm tsh tsh - Teleport's command line tool for logging into nodes running Teleport.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants