-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Make length/1 yielding #2053
Make length/1 yielding #2053
Conversation
erts/emulator/beam/ops.tab
Outdated
gc_bif1 Fail=j Live u$bif:erlang:length/1 Src Dst => \ | ||
i_length_setup Live Src | i_length Fail Live Dst | ||
|
||
i_length_setup t x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would using i_length_setup t xyc
work in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, given i_length_setup
is so tiny - would it make sense to also define it for r
? It shouldn't grow the main loop significantly, but might save some space in the bytecode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would using i_length_setup t xyc work in here?
Yes, it would. Fixed.
Additionally, given i_length_setup is so tiny - would it make sense to also define it for r? It shouldn't grow the main loop significantly, but might save some space in the bytecode.
That would not save any BEAM code space, as the t
and x
operands are packed into the same 32-bit word. It could possibly save a tiny, tiny amount of execution time, but not enough to make it worthwhile.
While looking at the generated code, I realized that I had forgotten to implement a minor optimization that I had thought of earlier.
The guard BIF `length/1` would calculate the length of the list in one go without yielding, even if the list was were long. To make it even worse, the call to `length/1` would only cost a single reduction. This commit reimplements `length/1` so that it eats a number of reductions proportional to the length of the list, and yields if the available reductions run out.
f1e74d3
to
1ea57e5
Compare
The guard BIF
length/1
would calculate the length of the list in onego without yielding, even if the list was were long. To make it even
worse, the call to
length/1
would only cost a single reduction.This commit reimplements
length/1
so that it eats a number ofreductions proportional to the length of the list, and yields if the
available reductions run out.