-
Notifications
You must be signed in to change notification settings - Fork 333
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
Add array:indexOf
#2939
Add array:indexOf
#2939
Conversation
Add array:indexOf
return i | ||
end | ||
end | ||
self.prf = self.prf + #arr |
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.
I think this should just be changed to a single #arr
variable and loop through that with a manual number loop to avoid the double length check. Also need to make this not just add 0 prf for zero length arrays
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.
Also need to make this not just add 0 prf for zero length arrays
It still adds ops from the base cost of the function.
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.
Also, I don't understand what you mean. Do you mean using for i = 1, #arr
? I would think that would be less optimal, as ipairs
is usually better than a normal for loop.
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.
Where'd you get that idea from? Afaik ipairs
only jits on 64 bit. And either way, it's wasted effort for the non-contained case when you loop through the entire array and then have to loop through it again, but in C with the length operator.
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.
Huh, I guess I just misremembered, my bad. I wasn't aware about the specifics of the length operator, I thought Lua just had a member to its tables that contained sequential length.
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.
I'm partially wrong, luajit apparently does cache the length operator? But PUC lua doesn't. It's weird cause my json library got a pretty significant speedup from passing it.
So it's probably not specifically that the length is stored but that it caches any pure operations inside a scope into variables.
Either way it's just better to play it safe.
array:indexOf
returns the index of the element of the array, or 0 if it doesn't exist. It is likearray:exists
but works on values instead of keys.Dynamic op cost of 1 op per element of the array.
Also cleans up some legacy
registerFunction
s and C syntax.