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

Content ID caching in Lua #12444

Merged
merged 13 commits into from
Sep 18, 2022
Merged

Content ID caching in Lua #12444

merged 13 commits into from
Sep 18, 2022

Conversation

TurkeyMcMac
Copy link
Contributor

I suggested this in #12438, where you can find some context/rationale. An alternative to this PR is to incorporate its changes into #12438.

To do

This PR is Ready for Review.

How to test

There's a unit test. You can also try the change with other mods that access raw content IDs.

@TurkeyMcMac TurkeyMcMac changed the title Cidcache Content ID caching in Lua Jun 14, 2022
@TurkeyMcMac
Copy link
Contributor Author

Closing since equivalent functionality is being implemented in #12438.

@TurkeyMcMac
Copy link
Contributor Author

I'll reopen this as it seems the other PR isn't very active anyway.

Copy link
Member

@Desour Desour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 For concept.
Code looks fine.
Haven't tested.
You could do a small benchmark to verify that it's faster.

builtin/game/item_s.lua Show resolved Hide resolved

local content2name = setmetatable({}, {
__index = function(self, id)
return old_get_name_from_content_id(id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@TurkeyMcMac
Copy link
Contributor Author

Should I add a setting to disable this, in case people want to save memory? At the most, it could use on the order of one megabyte per thread. Might that be significant on Raspberry Pi servers?

@sfan5
Copy link
Member

sfan5 commented Aug 4, 2022

No. There are much more impactful things that could be tweaked to save RAM.

@rubenwardy
Copy link
Member

This needs to be benchmarked to ensure the performance increase is worth the added code

@TurkeyMcMac
Copy link
Contributor Author

TurkeyMcMac commented Aug 4, 2022

I added benchmark commands. Here are the results on my computer:

LuaJIT 2.1.0-beta3PUC Lua 5.1.5
get_content_idget_name_from_content_idget_content_idget_name_from_content_id
Without cache27ms18ms37ms24ms
With cache10ms7ms22ms17ms

@SmallJoker
Copy link
Member

What's the benefit of an additional content ID cache? When used heavily, such as in Lua mapgens, the content IDs are already cached by the mod where needed to speed up generation time. This does get rid of the function call entirely inside the 3D (or more) loop to iterate through the data entries.

I very much believe in the "keep it simple, stupid" principle and I don't think this PR fits into that scheme. No optimization helps if mods are programmed inefficiently.

@TurkeyMcMac
Copy link
Contributor Author

TurkeyMcMac commented Aug 5, 2022

The original use case was a faster implementation of get_node: #12438. The cache is also used in #12609 and #12611. The same cache might as well be used for other parts of the API.

Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(code review only this time)

games/devtest/mods/experimental/commands.lua Outdated Show resolved Hide resolved
games/devtest/mods/experimental/commands.lua Show resolved Hide resolved
games/devtest/mods/unittests/content_ids.lua Outdated Show resolved Hide resolved
@TurkeyMcMac TurkeyMcMac requested a review from sfan5 August 7, 2022 12:28
Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before:

Time: 7.07 ms
Time: 18.43 ms

after:

Time: 1.11 ms
Time: 2.55 ms

@sfan5 sfan5 added the Rebase needed The PR needs to be rebased by its author. label Sep 18, 2022
@sfan5 sfan5 removed the Rebase needed The PR needs to be rebased by its author. label Sep 18, 2022
@nerzhul nerzhul merged commit 310b12b into minetest:master Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants