Skip to content
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

Merged
merged 4 commits into from
Jan 16, 2024
Merged

Add array:indexOf #2939

merged 4 commits into from
Jan 16, 2024

Conversation

Denneisk
Copy link
Member

@Denneisk Denneisk commented Dec 12, 2023

array:indexOf returns the index of the element of the array, or 0 if it doesn't exist. It is like array:exists but works on values instead of keys.

Dynamic op cost of 1 op per element of the array.

Also cleans up some legacy registerFunctions and C syntax.

return i
end
end
self.prf = self.prf + #arr
Copy link
Contributor

@Vurv78 Vurv78 Dec 18, 2023

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

Copy link
Member Author

@Denneisk Denneisk Dec 18, 2023

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

@Denneisk Denneisk Dec 18, 2023

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.

Copy link
Contributor

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.

@thegrb93 thegrb93 requested a review from Vurv78 January 4, 2024 21:49
@Denneisk Denneisk merged commit 93bb7cc into wiremod:master Jan 16, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants