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

Use Map for rule search and add benchmark #334

Merged
merged 6 commits into from
Nov 28, 2024
Merged

Use Map for rule search and add benchmark #334

merged 6 commits into from
Nov 28, 2024

Conversation

lupomontero
Copy link
Owner

@lupomontero lupomontero commented Nov 25, 2024

This PR includes the changes proposed by @gugu's in #302 as well as on other minor tweaks (like only iterating over the rules once instead of twice on module load).

This increases performance (ops/sec) by a factor of over 100!!

The main change is using a Map object and indexing it by punySuffix. This allows searching by punySuffix without having to iterate over the thousands of rules.

npm run benchmark

> [email protected] benchmark
> node --experimental-vm-modules --no-warnings benchmark/suite.js

┌────────────────────────────────────┬─────────┬──────┬──────────────┐
│ (index)                            │ ops/sec │ ± %  │ runs sampled │
├────────────────────────────────────┼─────────┼──────┼──────────────┤
│ [source] psl#isValid               │ 367385  │ 3.76 │ 85           │
│ [source] psl#parse                 │ 396952  │ 1.22 │ 93           │
│ [source] psl#parse invalid domain  │ 429242  │ 1.25 │ 92           │
│ [v1.13.0] psl#isValid              │ 3306    │ 1.62 │ 87           │
│ [v1.13.0] psl#parse                │ 3402    │ 2.07 │ 89           │
│ [v1.13.0] psl#parse invalid domain │ 3230    │ 1.53 │ 87           │
└────────────────────────────────────┴─────────┴──────┴──────────────┘
┌──────────────────────────┬────────┬─────────┬──────────────┐
│ (index)                  │ source │ v1.13.0 │ diff         │
├──────────────────────────┼────────┼─────────┼──────────────┤
│ psl#isValid              │ 367385 │ 3306    │ '111.13x up' │
│ psl#parse                │ 396952 │ 3402    │ '116.68x up' │
│ psl#parse invalid domain │ 429242 │ 3230    │ '132.89x up' │
└──────────────────────────┴────────┴─────────┴──────────────┘

gugu and others added 6 commits November 25, 2024 15:54
The benchmark script now runs the suite both for the local version of
the source code and v1.13.0, which is fetched from a CDN.
This avoids having to iterate over all the rules to parse them first and
then again to build the map.

It also simplifies implementation by not expecting more than one rule
for a given punySuffix.
@lupomontero lupomontero requested a review from mfdebian November 25, 2024 15:49
@lupomontero lupomontero self-assigned this Nov 25, 2024
@lupomontero lupomontero linked an issue Nov 25, 2024 that may be closed by this pull request
@lupomontero lupomontero marked this pull request as ready for review November 25, 2024 16:03
@lupomontero lupomontero mentioned this pull request Nov 25, 2024
Copy link
Collaborator

@mfdebian mfdebian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks Amazing To Me ⭐

I guess I don't really have much to add, sadly, but I ran it, tried some edge cases and it works like a charm ✨

Literally a HUGE improvement in terms of complexity 👏

index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
benchmark/suite.js Show resolved Hide resolved
benchmark/suite.js Show resolved Hide resolved
@lupomontero
Copy link
Owner Author

Thanks for the review @mfdebian 💛

@lupomontero lupomontero merged commit 1fd3665 into main Nov 28, 2024
9 checks passed
@lupomontero lupomontero deleted the Short-io-main branch November 28, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance issues - iterating over 9k rules for every domain
3 participants