[Scarpet] `player('all')` may be slower than `entity_selector('@a')`
altrisi opened this issue ยท 5 comments
I did some testing on my machine, and found that entity_selector('@a')
was consistently much faster than player('all')
, however, they're both so fast that I'm not sure it really matters anyways
/script run for(range(1000000), _) // around 100ms
/script run for(range(1000000), entity_selector('@a')) // around 300ms
/script run for(range(1000000), player('all')) // around 450ms
I'd be curious to see what your results are, or if you think this is even a fair test
Originally posted by @Xendergo in gnembon/scarpet#177 (comment)
I've just tested and entity_selector('@a')
was consistently faster than player('all')
.
Test using /script run profile_expr(entity_selector('@a'))
and /script run profile_expr(player('all'))
I haven't personally tested it, but IMO the native scarpet solution should be faster by skipping computation the selector has to do, there's no point having it/using it if it's slower.
Under what conditions did you test this on?
- Did you try testing with just one player (likely the basis for the original results)?
- Did you test with multiple players (e.g. 3 or 30)?
- Did you test with multiple players in different dimensions (e.g. 1 in each dim or 10 in each dim)?
I gotta agree with James on this one, cos looking at player()
function code, it uses (imo) the fastest possible method:
ListValue.wrap(((CarpetContext)c).s.getServer().getPlayerManager().getPlayerList().stream().map(EntityValue::new).collect(Collectors.toList()));
This code could ig be sped up by using a for loop to avoid iterating over the list 2 times, but aside from that there isn't rly much you can do to optimize it. But in that case, someone should check how much the following code changes:
List<Value> valueList = new ArrayList<>();
for(ServerPlayerEntity spe : ((CarpetContext)c).s.getServer().getPlayerManager().getPlayerList()){
valueList.add(new EntityValue(spe));
}
This code is essentially the same tho, which is what makes me think there is something funny going on behind the scenes.
Another thing we should look into is how exactly the entity selector works, and whether it gets the entities from a different place.
Under what conditions did you test this on?
* Did you try testing with just one player (likely the basis for the original results)? * Did you test with multiple players (e.g. 3 or 30)? * Did you test with multiple players in different dimensions (e.g. 1 in each dim or 10 in each dim)?
Tested 10 times for each expression, then took an average.
All players were 1k blocks apart.
- 10 players in the same dimension.
/script run profile_expr(entity_selector('@a'))
- 270k average
/script run profile_expr(player('all'))
- 180k average - 20 players, 10 in overworld, 10 in the nether.
/script run profile_expr(entity_selector('@a'))
- 15k average
/script run profile_expr(player('all'))
- 10k average
Something funky is 100% going on