lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

> * What does you implementation do with limits outside the range of
> integers, such as (for i = 1, 2^70 do ...)? (What should it do?)
> 

> * And for floating point arithmetic, like the following?
> 
>  for i = 2^53 - 10, 2^53 + 10 do print(i) end

These generate infinite loops due to the step size being or becoming an
insufficient magnitude to alter the loop index. A simple check could be
added to the OP_FORPREP instruction to determine this case, and if detected
an error could be thrown.

I think the check and error would probably work best, at least that way the
coder will know their loop won't work as-is. I would guess that people who
work with such extremely large numbers would know about the magnitude issues
with using very large/small floating values. I know I have learned more
about these numeric extremes than I knew before I started this patch! ;)


> * Did you test the performance cost of the extra check?

I ran the following tests 10 times (on a Q6600 CPU @2.4gHz) and averaged the
execution times in both vanilla Lua and patched Lua:

local reps = 0
for i=math.mininteger,math.maxinteger,2^35 do reps = reps + 1 end


Patched Lua did 536,870,913 iterations in ~24.28 seconds
Vanilla Lua did 536,870,913 iterations in ~23.62 seconds

So, the patch runs ~0.66 seconds slower than vanilla... for a slowdown of
0.00000000122935s per iteration. I don't think this slowdown would be
noticeable in standard Lua code, but the benefit is loops without any
edge cases to come along and bite you in the butt. Not counting the cases
where the step size is of an insufficient magnitude that is!

I also have an idea that may outright remove this small impact... I will
go play with the code and see what I can come up with. I will also add
the step size magnitude check at the same time.

Thank you again for your questions, Roberto, they are appreciated!

~Paige