You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Performance-critical Julia code usually performs bounds checks and input validation once before running (presumably) safe code. Macros such as @simd, @inbounds, and @turbo have been added to Julia to boost performance for safe code. However, the way that @inbounds propogation works allows for differing syntax with for loops:
@inboundsfor i in1:20
x[i] =rand(1:i)
end
and
for i in1:20@inbounds x[i] =rand(1:i)
end
have the same llvm representation. Another subtlety of propogation is that @inboundsdisables bound checks for entire lines;
@inbounds x[i] + y[j]
disables bounds check for both x and y, although the author of the code may have intended for the macro to only affect x. Another issue with propogation is that the syntax
@inbounds@simdfor i in1:20
x[i] +=rand(1:i)
end
does not actually disable bounds checks for x (as specified in the docs of @simd).
To avoid confusion and ambiguity with the use of @inbounds (and other macros), I propose that we add a section on macro calls, specifying that the code meant to be modified by the macro should be enclosed in parentheses. Examples of proper usage
for i in1:20
x[i] +=@inbounds(x[end-i])
end
@inbounds(x[i] +=sum(y[A]))
Examples of improper usage
@inbounds x[3]
@inboundsfor i in1:20print(x[i])
end
Assignments are special in the case of @inbounds, as the macro can affect both the left- and right-hand sides or only the right-hand side. The only method of dropping bounds check for only the right-hand side is via the syntax
=(@inbounds(x[3]), x[4])
which should be avoided completely.
The text was updated successfully, but these errors were encountered:
Performance-critical Julia code usually performs bounds checks and input validation once before running (presumably) safe code. Macros such as
@simd
,@inbounds
, and@turbo
have been added to Julia to boost performance for safe code. However, the way that@inbounds
propogation works allows for differing syntax with for loops:and
have the same llvm representation. Another subtlety of propogation is that
@inbounds
disables bound checks for entire lines;disables bounds check for both
x
andy
, although the author of the code may have intended for the macro to only affectx
. Another issue with propogation is that the syntaxdoes not actually disable bounds checks for
x
(as specified in the docs of@simd
).To avoid confusion and ambiguity with the use of
@inbounds
(and other macros), I propose that we add a section on macro calls, specifying that the code meant to be modified by the macro should be enclosed in parentheses. Examples of proper usageExamples of improper usage
Assignments are special in the case of
@inbounds
, as the macro can affect both the left- and right-hand sides or only the right-hand side. The only method of dropping bounds check for only the right-hand side is via the syntaxwhich should be avoided completely.
The text was updated successfully, but these errors were encountered: