mesecons icon indicating copy to clipboard operation
mesecons copied to clipboard

Remove some unneeded memory allocation

Open TurkeyMcMac opened this issue 2 years ago • 5 comments

Overview

This PR does three things:

  • Clean up the basic info getters like mesecon.get_conductor.
  • Speed up the functions for finding node links, remove unnecessary allocations.
    • As far as I can tell there can only be one linking rule between two nodes, so the functions have been simplified.
    • The inverted function now returns the actual rule, not the inverted rule.
  • Remove unnecessary node copies.
    • Components in the modpack with rule getter functions are passed non-copied nodes. External components can get non-copied nodes as well by putting the rule_node_nocopy key in their definitions.

Benchmarks

I used the benchmarks from https://github.com/minetest-mods/mesecons/pull/578. Keep in mind that reducing garbage collection load may also increase the performance of code outside mesecons.

Long line of diodes

Before: 0.11s After: 0.10s (probably within margin of error)

Block of wires

I turned the block on and off 10 times.

Before: 1.7s After: 0.8s

256 byte memory bank

Before: 1.5s After: 1.3s

Tests

I've done manual testing and things work. The tests from https://github.com/minetest-mods/mesecons/pull/605 also pass.

TurkeyMcMac avatar May 20 '22 12:05 TurkeyMcMac

Perhaps someone with the right privilege can run a search of ContentDB for mods using rules_link_rule_all and rules_link_rule_all_inverted. I know lwwires uses them. If they are used by many external mods, I will provide new implementations of them for compatibility with these mods.

TurkeyMcMac avatar Jul 18 '22 14:07 TurkeyMcMac

https://content.minetest.net/zipgrep/43bc7f2d-403d-4bc4-84bf-3ba524da579d/

rubenwardy avatar Jul 20 '22 00:07 rubenwardy

Thanks. It looks like lwwires is the only one.

TurkeyMcMac avatar Jul 20 '22 00:07 TurkeyMcMac

I decided to reimplement the functions regardless.

TurkeyMcMac avatar Jul 23 '22 01:07 TurkeyMcMac

By the way, this code could be simplified and sped up a bit if it were assumed that the functions which return rules do not modify the node tables passed to them. This assumption is not the norm in the Minetest API, but it seems realistically true.

TurkeyMcMac avatar Oct 24 '22 20:10 TurkeyMcMac