-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
[VB] For-Loop Overflow and Underflow #22563
Comments
@AdamSpeight2008 I'm concerned that any attempt to address this design flaw in today's Wouldn't it be better to somehow make specialized "lowered code" for Byte/SByte/Short/UShort, pretty much in the style that @ericmutta already does in his work-around (using an Integer then casting to Byte)? I think the work-around might even be slightly faster, since conversion of Integer to Byte is cheap, and it's generally better to use a CPU's native-size int. (In my own testing, using VS 2015, the work-around runs about the same as the pure-Byte approach, and maybe slightly faster.) |
@rskar-git It being a |
@AdamSpeight2008 Yep, I got that. Yet However, this approach is now, and has been for quite a long time, the "devil we know." And, for good or ill, people have made use of the side-effect. Hence one of my worries about the possibility of a breaking change (the other being performance). |
What are the current workarounds?
We could enable the safety via an option |
@AdamSpeight2008 The suggested lowering form above won't work with signed types (it can overflow at fourth line, Another proposal to consider:
When possible, it preserves the legacy side effect; otherwise by design it won't suffer overflow/underflow. (I suspect that if unchecked integer arithmetic/converision was more readily available in VB, we could make a much better alternative.) I should still point out that employing a 32-bit integer type and then casting back down works well for the 8-bit and 16-bit integer types, and runs efficiently for both Debug and Release compiles. In Release, its performance is the same as a pure 8-bit/16-bit implementation. In Debug, even though it runs about 15% slower versus a pure 8-bit/16-bit implementation, the performance nonetheless beats both "suggested lowering form above" and "another proposal" by a wide margin (they are each like >70% slower). Employing a 64-bit integer type and then casting back to 32-bit works OK too. However, at this point in time, people would have already made their own specialized work-arounds for their Int32.MinValue to Int32.MaxValue use cases. I really don't think we need to make a fix for the 32-bit case. I can't imagine anyone doing Int64.MinValue to Int64.MaxValue (2^64 is mind-bogglingly hugh!), but I think they would use the same code pattern as they did for the 32-bit situation. And a super computer, like maybe a Cray. Anyway, I'm still of the opinion of leaving For example, I would suppose that if the
|
It would be nice to have compile-time verified templates so we can optimise the generated code. Template Templated_ForLoop(
_Start_ As ConstExpr,
_End_ As ConstExpr,
_Step_ As ConstExpr,
Body As Expression,
Cmp As Operator.Comparision,
deltaFunc As ...,
op As Operatior.Binary
)
Dim _Value_ = %{ _Start_ }%
While True
Body(_Value_)
If _Value_ %{Cmp}% %{_End_}% Then Exit While
Dim _delta_ = %{ deltaFunc }%
If _delta_ > Abs( %{ _Step_ }% ) Then Exit While
_Value_ = _Value_ %{ op }% %{ _Step_ }%
End While
End Template
|
Yep, compile-time verified templates do seem like a very nice thing. However, I've been experimenting on this for much of yesterday, and have come to the conclusion that today's CPU is optimized around the idiomatic The
This indicates to the compiler to use Integer for the indexing, but to then cast back to Byte into
|
Hey guys, I think it's premature to split this discussion off from the language repo. We still haven't made a decision there and having people discuss across two threads is a pain. We'll discuss execution on this repo when and if we decide to do anything. |
Issue in VB Repo
The follow will throw a
System.OverflowException
due to the underlying implementation details.We should consider changing the lowering to better handle the cases which approach the numerical type's MIn or Max values.
suggested lowering form, which should all current allowed for-loop forms.
The text was updated successfully, but these errors were encountered: