Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(YouTube): Filter litho components using prefix tree #447

Merged

Conversation

LisoUseInAIKyrios
Copy link
Contributor

@LisoUseInAIKyrios LisoUseInAIKyrios commented Jul 24, 2023

Since all litho filters are all searching against the same target path and protobuffer, a prefix tree (trie) can be used to speed this up quite a bit.

This PR adds a prefix tree search class (TrieSearch), which works for both strings and byte arrays.

This default ReVanced patch installation uses 79+ filter strings, and a trie effectively searches for all of them in 1 pass. This implementation does not prune away any non-enabled filters, so the runtime is basically a constant time regardless of how many filters are added or enabled.

Performance using default patches, default settings.
Average method runtime of each call to LithoFilterPatch#filter to run all enabled filters:
Before this PR, average method call runtime of 235,139 ns
With this PR, average method call runtime of 55,437 ns

Which gives a 425% speed up. Of note, that if more 'hide' features are enabled beyond the default settings then the speedup would be even greater (as the old linear searching would be adding more work, while the trie would continue to take the same time). During development a 10x speedup of the worse case (slowest use cases) was seen.

The litho filter has one fundamental change, instead of each filter always being called with "Check if you match this path/protobuffer, then confirm if you want to hide the litho element". With this PR the litho patch searches the trie once for matches, and then any matched filters are then confirmed with "I found a match of one of your filters, should I hide the litho component?"

Of note, the existing filter group classes still work the same as before, where they do a linear search for each pattern. These existing filter groups can still be used to fine tune the filtering, just as ShortsFilter and a few other filters do.

@oSumAtrIX oSumAtrIX changed the title feat(youtube): Litho filter prefix tree pattern matching perf(YouTube): patterm match litho filters using tree structure Jul 24, 2023
@oSumAtrIX oSumAtrIX changed the title perf(YouTube): patterm match litho filters using tree structure perf(YouTube): filter litho components using prefix tree Jul 24, 2023
@LisoUseInAIKyrios
Copy link
Contributor Author

LisoUseInAIKyrios commented Jul 25, 2023

Since no protobuffer filters exist just yet, I tried merging #416 and this all works correctly as expected.

When the flyout menu is opened:
The litho resolving speed of that PR: 468,262 ns
That PR merged with this PR: 80,924 ns

@oSumAtrIX
Copy link
Member

Cheers, in this case we can merge #416. It was put on hold due to performance issues

@LisoUseInAIKyrios
Copy link
Contributor Author

I was able to speed up the flyout menu filtering even more by checking the protobuffer only if the flyout is present (since currently the flyout filter is the only filter that uses protobuffer filtering)

If #446 is merged first, then I'll add this change to this PR:
LisoUseInAIKyrios@6d94dc1

@LisoUseInAIKyrios LisoUseInAIKyrios marked this pull request as ready for review July 30, 2023 16:56
@oSumAtrIX oSumAtrIX changed the title perf(YouTube): filter litho components using prefix tree perf(YouTube): Filter litho components using prefix tree Aug 1, 2023
@LisoUseInAIKyrios LisoUseInAIKyrios merged commit 18f2900 into ReVanced:dev Aug 1, 2023
2 checks passed
@LisoUseInAIKyrios LisoUseInAIKyrios deleted the prefix_tree_pattern_matching branch August 1, 2023 17:05
revanced-bot pushed a commit that referenced this pull request Aug 1, 2023
# [0.115.0-dev.5](v0.115.0-dev.4...v0.115.0-dev.5) (2023-08-01)

### Performance Improvements

* **YouTube:** Filter litho components using prefix tree ([#447](#447)) ([18f2900](18f2900))
revanced-bot pushed a commit that referenced this pull request Aug 2, 2023
# [0.115.0](v0.114.0...v0.115.0) (2023-08-02)

### Bug Fixes

* **YouTube - SponsorBlock:** Fix skip highlight button showing when set to 'show in seekbar' ([ea7bc57](ea7bc57))
* **YouTube - Spoof App Version:** Remove 17.30.35 target ([e906c6d](e906c6d))
* **YouTube:** fix potential litho filter thread race ([d3f523f](d3f523f))

### Features

* **Tiktok - Feed filter:** Add more filters ([#445](#445)) ([c16c038](c16c038))
* **YouTube - Hide layout components:** Hide `chips shelf` ([#448](#448)) ([c7d2a9b](c7d2a9b))
* **YouTube:** add `Player Flyout Menu` patch ([#416](#416)) ([b2085ae](b2085ae))

### Performance Improvements

* **YouTube:** Filter litho components using prefix tree ([#447](#447)) ([18f2900](18f2900))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants