Bound check optimization with "computed" index

104 views
Skip to first unread message

Leonard Mittmann

unread,
Feb 6, 2024, 10:39:56 AM2/6/24
to golang-nuts
I am trying to optimize the loop performance by reducing the number of bound checks. Inside the loop I compute a slice index and access two slice indexes like this:

j := i / 2
s[j+1] = i
s[j] = i // <- I want to get rid of this bound check

I assumed that I can get rid of the second bound check, as the compiler could already know that j+1 < len(s) and j >= 0. Am I missing something? Any suggestions how to solve this?

The full boiled down code to reproduce my issue is here: https://go.godbolt.org/z/hsjPh3WT7

Keith Randall

unread,
Feb 7, 2024, 1:01:09 PM2/7/24
to golang-nuts
I'm not sure, but I suspect that the prove pass does not propagate known ranges through divides.
Reply all
Reply to author
Forward
0 new messages