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
Move isInArray to Lua #2095
Move isInArray to Lua #2095
Conversation
I think isInArray has been in C++ this whole time probably due to performance concerns, so I don't think it would be a good idea moving it to Lua. Also as we can see in your code, implementing it its pretty straightforward, so |
Yeah you might be right @marksamman |
I ran a quick test of 10 million iterations through the table questDoors (defined in global.lua) looking for itemId 3551, and the C++ implementation were roughly one second slower in this scenario. It should be noticeable slower with strings as well. Code local itemId = 3551
for i = 1, 10000000 do
if isInArray(questDoors, itemId) then
--
end
end C++ 2.22s
2.39s
2.22s
2.30s
2.23s
avg: 2.272s Lua 1.24s
1.31s
1.25s
1.25s
1.24s
avg: 1.258s |
As I said previously, any time you switch between the interpreter and C++ code, it has a performance impact, so that's probably where you're getting the hit. Another possibility is that LuaJIT (if you're using) is noticing the table is not being modified in the inner loop and hoisting the condition outside e.g. JITed code is:
That said, performance is not a concern here, as long as complexity maintains and the code is pretty simple, only basic operations and Now for a change I'd prefer a |
We have always used isInArray so I didn't think about using table.contains (which a lot of people use in other apps). IMO we should keep isInArray, 99% of the people know about it and have no problems using it. |
Just add it to table metatable as ranisalt suggested ( not related to this PR |
Sure ill do that @raymondtfr What is the error you are getting? |
Lua Script Error: [Action Interface]
data/actions/scripts/other/spellbook.lua:onUse
data/actions/scripts/other/spellbook.lua:7: attempt to index local 'spell' (a boolean value)
stack traceback:
[C]: in function '__index'
data/actions/scripts/other/spellbook.lua:7: in function <data/actions/scripts/other/spellbook.lua:1> |
Are you sure you update your files? |
I have just unpacked clean datakpack files (downloaded now) to a different folder and downloaded nightlies Btw, it did not happen to a god character, but did to a normal character (player). |
Humm, im using forgottenserver-03483aacbddec173517601f5482f64d15946df14-b950-Win32-Release |
The heck 😕 . I am using the item spellbook (id 2175) btw, anyway I'm gonna take a nap, it must be me brains lol. |
Yeah thats pretty strange, let's see if someone else reports it :P |
Here is okay, I think: Tested on latest TFS (pulled it right now and recompiled). ** EDIT: ** |
@brunominervino The code we are talking about has already been fixed in a PR from @ranisalt , it was not related to this PR. |
@WibbenZ Sorry, I haven't understood. :) Ignore my comment hehe |
@@ -33,6 +33,19 @@ function getFormattedWorldTime() | |||
return hours .. ':' .. minutes | |||
end | |||
|
|||
function table.contains(array, value) |
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.
table.contains = function(tbl, value)
just like string.split
below.
function table.contains(array, value) | ||
if type(array) ~= "table" then | ||
return false | ||
end |
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.
You don't have to do this check. Assume it's only possible to call as tbl:contains(value)
and, if someone passes another object like table.contains(position, value)
it's the scripter fault.
Should be even faster now without the type check :) |
Thanks! |
No description provided.