Skip to content

Count leading zeroes a few bytes at a time - #126

Merged
glbrntt merged 1 commit into
apple:mainfrom
glbrntt:count-zeroes
Sep 1, 2026
Merged

Count leading zeroes a few bytes at a time#126
glbrntt merged 1 commit into
apple:mainfrom
glbrntt:count-zeroes

Conversation

@glbrntt

@glbrntt glbrntt commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

QUICFrame.countZeroes() shows up in a few benchmarks as using ~1.5% of CPU. It can be made a little faster by loading and checking 8 bytes at a time. For long runs of zeros (1200) it's ~4.5x faster, for very short runs (< 8) it's no worse.

`QUICFrame.countZeroes()` shows up in a few benchmarks as 1.5% of CPU
usage. It can be made a little faster by loading and checking 8 bytes at
a time. For long runs of zeros (1200) it's ~4.5x faster, for very short
runs (< 8) it's no worse.
@glbrntt glbrntt added the 🔨 semver/patch No public API change. label Sep 1, 2026
@glbrntt glbrntt changed the title Count leading a few bytes at a time Count leading zeroes a few bytes at a time Sep 1, 2026

while offset < count, bytes[offset] == 0x00 {
offset &+= 1
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this for trailing bytes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes: the first while loop does chunks of 8 bytes, this does the final up-to 7 seven bytes.

@agnosticdev agnosticdev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @glbrntt !

@glbrntt
glbrntt merged commit 264e160 into apple:main Sep 1, 2026
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants