Replace get_objects_inside_radius calls with get_connected_players() where possible #6325

Open
opened 2024-02-21 08:34:09 +00:00 by whosit · 21 comments
Member
		for _, obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do
			if obj:is_player() then
                --stuff
            end
        end

vs

        local players = minetest.get_connected_players()
        for i = 1, #players do
            local player = players[i]
            if player:get_pos():distance(pos) <= radius then
                --stuff
            end
        end
```lua for _, obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do if obj:is_player() then --stuff end end ``` vs ```lua local players = minetest.get_connected_players() for i = 1, #players do local player = players[i] if player:get_pos():distance(pos) <= radius then --stuff end end ```
AliasAlreadyTaken added the
1. kind/enhancement
2. prio/elevated
1. kind/breaking
labels 2024-02-21 20:26:48 +00:00
Member

here's a set of files which contain both a get_objects_in_area/get_objects_inside_radius can and also an is_player call:

  • 3d_armor/3d_armor_stand/init.lua
  • aerotest/behavior_and_helpers.lua
  • aerotest/chatcommand.lua
  • airutils/lib_planes/utilities.lua
  • bees/init.lua
  • cannons/functions.lua
  • cottages/modules/water/well.lua
  • detect_bad_entities/init.lua
  • digilines/lcd.lua
  • fireworks_redo/init.lua
  • leads/api.lua
  • leads/util.lua
  • mesecons/mesecons_mvps/init.lua
  • mesecons/mesecons_pressureplates/init.lua
  • meseportals/node_behaviors.lua
  • mob_core/api.lua
  • mobkit/init.lua
  • mobs_balrog/api/whip_actions.lua
  • mobs_balrog/compat/invisibility.lua
  • mobs_banshee/core/spawning.lua
  • mobs_mime/core/procedures.lua
  • mobs_monster/mese_monster.lua
  • mobs_redo/api.lua
  • mobs_redo/spawner.lua
  • nether/portal_api.lua
  • petz/kitz/api/api_clear_mobs.lua
  • petz/petz/misc/items.lua
  • petz/petz/misc/nodes.lua
  • ropes/functions.lua
  • scorpion/boss.lua
  • scorpion/pet.lua
  • spawnit/spawn_mobs.lua
  • spears/functions.lua
  • tnt/init.lua
  • water_life/api.lua
  • water_life/tools/buoy.lua
  • waypoint_announce/init.lua
  • worldedit/worldedit/manipulations.lua
  • yl_arena/functions.lua
  • yl_arena/game_gladiators.lua
  • yl_church/functions.lua
  • yl_commons/bugfixes/fix_bad_objects_in_area.lua
  • yl_commons/chatcommands/thankyou.lua
  • yl_commons/features/log_fall_death_details.lua
  • yl_commons/features/ocean_builder.lua
  • yl_events/quest_dhirrim.lua
  • yl_events/temp.lua
  • yl_events/tribe_miocene.lua
  • yl_monsterrace/functions.lua
  • yl_nether/api.lua
  • yl_nether_mobs/villager_evoker.lua
  • yl_nether_mobs/wither.lua
  • yl_xdecor/handlers/animations.lua
  • yl_xdecor/src/cooking.lua
  • yl_xdecor/src/mechanisms.lua
here's a set of files which contain both a `get_objects_in_area/get_objects_inside_radius` can and also an `is_player` call: * [x] 3d_armor/3d_armor_stand/init.lua * [x] aerotest/behavior_and_helpers.lua * [x] aerotest/chatcommand.lua * [x] airutils/lib_planes/utilities.lua * [x] bees/init.lua * [x] cannons/functions.lua * [x] cottages/modules/water/well.lua * [x] detect_bad_entities/init.lua * [x] digilines/lcd.lua * [x] fireworks_redo/init.lua * [x] leads/api.lua * [x] leads/util.lua * [x] mesecons/mesecons_mvps/init.lua * [x] mesecons/mesecons_pressureplates/init.lua * [x] meseportals/node_behaviors.lua * [x] mob_core/api.lua * [x] mobkit/init.lua * [x] mobs_balrog/api/whip_actions.lua * [x] mobs_balrog/compat/invisibility.lua * [x] mobs_banshee/core/spawning.lua * [x] mobs_mime/core/procedures.lua * [x] mobs_monster/mese_monster.lua * [x] mobs_redo/api.lua * [x] mobs_redo/spawner.lua * [x] nether/portal_api.lua * [x] petz/kitz/api/api_clear_mobs.lua * [x] petz/petz/misc/items.lua * [x] petz/petz/misc/nodes.lua * [x] ropes/functions.lua * [x] scorpion/boss.lua * [x] scorpion/pet.lua * [x] spawnit/spawn_mobs.lua * [x] spears/functions.lua * [x] tnt/init.lua * [x] water_life/api.lua * [x] water_life/tools/buoy.lua * [x] waypoint_announce/init.lua * [x] worldedit/worldedit/manipulations.lua * [x] yl_arena/functions.lua * [x] yl_arena/game_gladiators.lua * [x] yl_church/functions.lua * [x] yl_commons/bugfixes/fix_bad_objects_in_area.lua * [x] yl_commons/chatcommands/thankyou.lua * [x] yl_commons/features/log_fall_death_details.lua * [x] yl_commons/features/ocean_builder.lua * [x] yl_events/quest_dhirrim.lua * [x] yl_events/temp.lua * [x] yl_events/tribe_miocene.lua * [x] yl_monsterrace/functions.lua * [x] yl_nether/api.lua * [x] yl_nether_mobs/villager_evoker.lua * [x] yl_nether_mobs/wither.lua * [x] yl_xdecor/handlers/animations.lua * [x] yl_xdecor/src/cooking.lua * [x] yl_xdecor/src/mechanisms.lua
Member
  • bees/init.lua - bad pattern is present, but only in code that's executed if lucky blocks are present.
  • mob_core/api.lua - in commented out code pertaining to collision detection with players in beds. we're also getting rid of mob_core, right?
  • mobkit/init.lua - bad pattern is present in spawn logic, which we're not using anymore.
  • mobs_balrog/compat/invisibility.lua - bad pattern is present in invisibility check, i can fix that.
  • mobs_banshee/core/spawning.lua - bad pattern is present in check whether players are too close, i can fix that
  • mobs_monster/mese_monster.lua - bad pattern is present on spawn, to check whether a player placed the monster from an egg, and determine its color based on the direction they are looking (?!)
  • mobs_redo/api.lua - bad pattern is present in collision detection code; also when checking whether to "look" at a nearby player; also when the life timer expires; also in spawn logic (which we're not using) to prevent spawns too close to a player;
  • mobs_redo/spawner.lua - bad pattern present when determining if spawner is active
  • nether/portal_api.lua - bad pattern is present when determining if players should be teleported
  • petz/petz/misc/nodes.lua - bad pattern is present when determining whether to spawn warrior ants near an ant hole
  • ropes/functions.lua - bad pattern is present when pushing players down while the rope is descending
  • scorpion/boss.lua - bad pattern is present when possibly spawning pet scorpion
  • scorpion/pet.lua - bad pattern is present when pet tries to decide who owns it
  • spawnit/spawn_mobs.lua - bad pattern when deciding if any player is too close/far for spawn to happen (i will fix this)
  • waypoint_announce/init.lua - bad pattern when determining whether to show HUD to nearby players
  • yl_church/functions.lua - bad pattern when teleporting players through death portal
  • yl_commons/chatcommands/thankyou.lua - bad pattern is present
  • yl_events/quest_dhirrim.lua - bad pattern is present for two quests
  • yl_nether/api.lua - bad pattern present when checking whether to show portal particles
  • yl_nether_mobs/villager_evoker.lua - bad pattern when checking for invisible players to unmask

EDIT: removed reference to yl_xdecor

* [ ] bees/init.lua - bad pattern is present, but only in code that's executed if lucky blocks are present. * [ ] mob_core/api.lua - in commented out code pertaining to collision detection with players in beds. we're also getting rid of mob_core, right? * [ ] mobkit/init.lua - bad pattern is present in spawn logic, which we're not using anymore. * [x] mobs_balrog/compat/invisibility.lua - bad pattern is present in invisibility check, i can fix that. * [x] mobs_banshee/core/spawning.lua - bad pattern is present in check whether players are too close, i can fix that * [ ] mobs_monster/mese_monster.lua - bad pattern is present on spawn, to check whether a player placed the monster from an egg, and determine its color based on the direction they are looking (?!) * [ ] mobs_redo/api.lua - bad pattern is present in collision detection code; also when checking whether to "look" at a nearby player; also when the life timer expires; also in spawn logic (which we're not using) to prevent spawns too close to a player; * [ ] mobs_redo/spawner.lua - bad pattern present when determining if spawner is active * [ ] nether/portal_api.lua - bad pattern is present when determining if players should be teleported * [ ] petz/petz/misc/nodes.lua - bad pattern is present when determining whether to spawn warrior ants near an ant hole * [ ] ropes/functions.lua - bad pattern is present when pushing players down while the rope is descending * [x] scorpion/boss.lua - bad pattern is present when possibly spawning pet scorpion * [x] scorpion/pet.lua - bad pattern is present when pet tries to decide who owns it * [x] spawnit/spawn_mobs.lua - bad pattern when deciding if any player is too close/far for spawn to happen (i will fix this) * [x] waypoint_announce/init.lua - bad pattern when determining whether to show HUD to nearby players * [x] yl_church/functions.lua - bad pattern when teleporting players through death portal * [x] yl_commons/chatcommands/thankyou.lua - bad pattern is present * [x] yl_events/quest_dhirrim.lua - bad pattern is present for two quests * [x] yl_nether/api.lua - bad pattern present when checking whether to show portal particles * [x] yl_nether_mobs/villager_evoker.lua - bad pattern when checking for invisible players to unmask EDIT: removed reference to yl_xdecor
Member

mobs_balrog fix f67f0c0868

mobs_balrog fix https://github.com/fluxionary/minetest-mobs_balrog/commit/f67f0c0868187c268c6b498d1666f8322319bee4
Member

spawnit fix 48b0181383

spawnit fix https://github.com/fluxionary/minetest-spawnit/commit/48b01813839c5a4e48c9d3f6657a65fdb7892acd
Author
Member

yl_commons /thankyou_nearby fixed 868ee8674c

yl_commons /thankyou_nearby fixed https://gitea.your-land.de/your-land/yl_commons/commit/868ee8674c6d0b9e3d80e1d5c44c340b9ebea941
Author
Member

waypoint_announce fixed "upstream" c4665b2eb6

waypoint_announce fixed "upstream" https://gitea.your-land.de/whosit/waypoint_announce/commit/c4665b2eb66213a29e6f623a0f658f8fdd5f93f1
Author
Member
PR to mobs_redo: https://codeberg.org/tenplus1/mobs_redo/pulls/15
Member

mobs_banshee 0586750ab6

mobs_banshee https://gitea.your-land.de/your-land/mobs_banshee/commit/0586750ab639d776b92658aec912270520152e1a
Member

nether: i could PR the upstream, but we can't use upstream because we don't want the new biome(s)?

nether: i could PR the upstream, but we can't use upstream because we don't want the new biome(s)?
Member

scorpion: 4d7b881c75

scorpion: https://gitea.your-land.de/your-land/Scorpion/commit/4d7b881c751f66fba0904567f48aff65841ec918
Member

yl_church 4bc05001b0

yl_church https://gitea.your-land.de/your-land/yl_church/commit/4bc05001b0957b36c44390f2418093941ee34551
Member

yl_events 527a2c4f1b

yl_events https://gitea.your-land.de/your-land/yl_events/commit/527a2c4f1b3fa6677e9c85c48da4d6728359519e
Member

yl_nether 75451aa15b

yl_nether https://gitea.your-land.de/your-land/yl_nether/commit/75451aa15bb3e0a855edd29bc346ee97fd0e9dff
Member

yl_nether_mobs 7748115fdc

yl_nether_mobs https://gitea.your-land.de/your-land/yl_nether_mobs/commit/7748115fdc438e3b2fd7849bfa90fe3d186c441a

Wouldn't it make sense to cache people's position per step in lua and then have LuaJit optimize it?

Also, we should probably gather and document antipatterns, for other modmakers to go through theirs and avoid or remove them.

Wouldn't it make sense to cache people's position per step in lua and then have LuaJit optimize it? Also, we should probably gather and document antipatterns, for other modmakers to go through theirs and avoid or remove them.
Member

Wouldn't it make sense to cache people's position per step in lua and then have LuaJit optimize it?

no, getting the list of active players through the lua API is cheap. copying the relevant players (most of them) to a lua data structure probably is cheaper than copying a lua data structure.

also, players can be disconnected in the middle of one mod processing. every mod that disconnects players would have to invalidate the cache or risk problems.

> Wouldn't it make sense to cache people's position per step in lua and then have LuaJit optimize it? no, getting the list of active players through the lua API is cheap. copying the relevant players (most of them) to a lua data structure probably is cheaper than copying a lua data structure. also, players can be disconnected in the middle of one mod processing. every mod that disconnects players would *have* to invalidate the cache or risk problems.

In the loop, should it be pairs() or ipairs() when iterating over get_connected_players?

In the loop, should it be pairs() or ipairs() when iterating over get_connected_players?
Author
Member

In the loop, should it be pairs() or ipairs() when iterating over get_connected_players?

Hmm... For some reason I assumed that pairs() is faster, but there are luajit benchmarks that say ipairs() is faster or even just indexing...

For this case it should not matter too much though...

I actually noticed now that flux is doing it the indexing way :p

UPD: changed original post to use flux's version X)

> In the loop, should it be pairs() or ipairs() when iterating over get_connected_players? Hmm... For some reason I assumed that pairs() is faster, but there are luajit benchmarks that say ipairs() is faster or even just indexing... For this case it should not matter too much though... I actually noticed now that flux is doing it the indexing way :p UPD: changed original post to use flux's version X)
Author
Member

mobs_redo fixed upstream: 822e78fd32

mobs_redo fixed upstream: https://codeberg.org/tenplus1/mobs_redo/commit/822e78fd32a2c4985c27ae0ab65ae01da0597422
Member

In the loop, should it be pairs() or ipairs() when iterating over get_connected_players?

Hmm... For some reason I assumed that pairs() is faster, but there are luajit benchmarks that say ipairs() is faster or even just indexing...

For this case it should not matter too much though...

I actually noticed now that flux is doing it the indexing way :p

UPD: changed original post to use flux's version X)

ipairs is faster than pairs, but pairs works on "hash-like" tables, while ipairs does not. using explicit indices is faster than ipairs, and with luajit/lua 5.1, their behaviors are essentially identical. however, the performance difference between the two is negligible unless you've got tight loops iterating over huge tables. i used to prefer ipairs over explicit indexing because i thought it was more legible, but i've switched to preferring explicit indexing for no particular reason.

> > In the loop, should it be pairs() or ipairs() when iterating over get_connected_players? > > Hmm... For some reason I assumed that pairs() is faster, but there are luajit benchmarks that say ipairs() is faster or even just indexing... > > For this case it should not matter too much though... > > I actually noticed now that flux is doing it the indexing way :p > > UPD: changed original post to use flux's version X) ipairs is faster than pairs, but pairs works on "hash-like" tables, while ipairs does not. using explicit indices is faster than ipairs, and with luajit/lua 5.1, their behaviors are essentially identical. however, the performance difference between the two is negligible unless you've got tight loops iterating over huge tables. i used to prefer ipairs over explicit indexing because i thought it was more legible, but i've switched to preferring explicit indexing for no particular reason.
Member
local clock = os.clock
local t = {}
for i = 1, 1e8 do
 t[i] = i
end

local sum = 0
local start = clock()
for i = 1, #t do
 sum = sum + i
end
local elapsed = clock() - start
print("index", elapsed, sum)

sum = 0
start = clock()
for _, i in ipairs(t) do
 sum = sum + i
end
elapsed = clock() - start
print("ipairs", elapsed, sum)

sum = 0
start = clock()
for _, i in pairs(t) do
 sum = sum + i
end
elapsed = clock() - start
print("pairs", elapsed, sum)

results:

luajit test.lua 
index	0.089173	5.00000005e+15
ipairs	0.119828	5.00000005e+15
pairs	0.269134	5.00000005e+15
```lua local clock = os.clock local t = {} for i = 1, 1e8 do t[i] = i end local sum = 0 local start = clock() for i = 1, #t do sum = sum + i end local elapsed = clock() - start print("index", elapsed, sum) sum = 0 start = clock() for _, i in ipairs(t) do sum = sum + i end elapsed = clock() - start print("ipairs", elapsed, sum) sum = 0 start = clock() for _, i in pairs(t) do sum = sum + i end elapsed = clock() - start print("pairs", elapsed, sum) ``` results: ``` luajit test.lua index 0.089173 5.00000005e+15 ipairs 0.119828 5.00000005e+15 pairs 0.269134 5.00000005e+15 ```
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: your-land/bugtracker#6325
No description provided.