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 "len(buf)-1" to access final slice index to eliminate a few more boundary checks #160

Merged
merged 1 commit into from
Sep 29, 2024

Conversation

NyaaaWhatsUpDoc
Copy link
Contributor

@NyaaaWhatsUpDoc NyaaaWhatsUpDoc commented Sep 29, 2024

Also learnt in the process that when it comes to boundary checking Go doesn't seem to trust its own allocated slice length (though I'm assuming this wasn't a choice). So any slice allocated with a variable length, even if you know it's at least literal x because the length is x+$uintVariable, it still performs boundary checks on all the slice indices up to x-1 unless you put a manual check directly after the slice allocation. There were a few cases of these that could be added here, but given it's an almost pointlessly small optimization that frankly has a weird "code smell" (given you're checking the length of a slice directly after allocating it), I didn't bother including them. Though I might see if there's an issue open for it on the Go project.

@ncruces
Copy link
Owner

ncruces commented Sep 29, 2024

Yeah, I tried a couple of things on this file, couldn't remove the bulk of the checks, and just gave up.

But nice to see you could remove a couple.

@ncruces ncruces merged commit e59e2ed into ncruces:main Sep 29, 2024
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