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

Refactor LinkParser #4576

Merged
merged 6 commits into from
Apr 23, 2023
Merged

Refactor LinkParser #4576

merged 6 commits into from
Apr 23, 2023

Conversation

Nerixyz
Copy link
Contributor

@Nerixyz Nerixyz commented Apr 23, 2023

Description

This PR is the continuation of #4507 and #4436.

These are the main changes:

  • Use custom parsing for IPv4 addresses over a regex.
  • Drop IPv6 support (was disabled by default).
  • Use QStringView over QStringRef
  • Removed usage of goto.

In my tests (chatterino-benchmark --benchmark_filter=LinkParsing --benchmark_repetitions=100), this version is about 6x faster:

Before

Benchmark Time CPU Iterations
BM_LinkParsing 33159 ns 33692 ns 21333
...
BM_LinkParsing_mean 33569 ns 33523 ns 100
BM_LinkParsing_median 33178 ns 32959 ns 100
BM_LinkParsing_stddev 1520 ns 1457 ns 100
BM_LinkParsing_cv 4.53 % 4.35 % 100

flamegraph

After

Benchmark Time CPU Iterations
BM_LinkParsing 5137 ns 5022 ns 112000
...
BM_LinkParsing_mean 5369 ns 5352 ns 100
BM_LinkParsing_median 5324 ns 5301 ns 100
BM_LinkParsing_stddev 273 ns 254 ns 100
BM_LinkParsing_cv 5.08 % 4.75 % 100

flamegraph

As you can see, I didn't change the isValidTld it's now the bottleneck here. The main reason is that a QString needs to be created and case-converted. Ideally, it could use a QStringView and have case-insensitive search functions.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Outdated Show resolved Hide resolved

return exp.match(host).hasMatch();
if (current > u'9' || current < u'0')
Copy link
Member

Choose a reason for hiding this comment

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

QChar::isDigit implementation seems to handle unicode stuff, your solution seems good to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, isDigit also checks if the character is the "Nd" unicode category.

src/common/LinkParser.cpp Outdated Show resolved Hide resolved
@pajlada
Copy link
Member

pajlada commented Apr 23, 2023

Code looks good, some small nitpicks that you can change if you feel like it's reasonable

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

src/common/LinkParser.cpp Show resolved Hide resolved
src/common/LinkParser.cpp Show resolved Hide resolved
@pajlada pajlada enabled auto-merge (squash) April 23, 2023 14:34
@pajlada pajlada merged commit 9c9fa86 into Chatterino:master Apr 23, 2023
@Nerixyz Nerixyz deleted the perf/link-parser branch April 23, 2023 14:58
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.

2 participants