WiIIiam278 / HuskTowns

A simple and elegant proxy-compatible Towny-style protection plugin
https://william278.net/project/husktowns
Apache License 2.0
76 stars 43 forks source link

performance improvements #437

Closed matipoirierg closed 7 months ago

matipoirierg commented 7 months ago

Hello, I would like to know if it's possible to improve the performance of the plugin, as I'm experiencing significant increases in MSPT. It's not urgent, since despite everything, my server remains stable at 20-19 TPS, but I believe there is room for improvement, especially if we think about scalability. I'm using latest build 3.0.2

https://spark.lucko.me/6cCNCHKC7y

ProdPreva1l commented 7 months ago

Not sure if there is any way to make it any faster, (atleast for this specific case) just at a quick glance

whats causing it is this function here

   public Optional<TownClaim> getClaimAt(@NotNull Chunk chunk, @NotNull HuskTowns plugin) {
        return claims.entrySet().stream()
                .filter(entry -> entry.getValue().stream().anyMatch(claim -> claim.getChunk().equals(chunk)))
                .findFirst()
                .flatMap(entry -> entry.getValue().stream()
                        .filter(claim -> claim.getChunk().equals(chunk))
                        .findFirst()
                        .flatMap(claim -> plugin.findTown(entry.getKey())
                                .map(town1 -> new TownClaim(town1, claim))))
                .or(() -> adminClaims.stream()
                        .filter(claim -> claim.getChunk().equals(chunk))
                        .findFirst()
                        .map(claim -> new TownClaim(plugin.getAdminTown(), claim)));
    }

and stream filtering is always going to be slow but, there might be a better way of doing it, someone smarter than me will know more than me though

matipoirierg commented 7 months ago

Not sure if there is any way to make it any faster, (atleast for this specific case) just at a quick glance

whats causing it is this function here

  public Optional<TownClaim> getClaimAt(@NotNull Chunk chunk, @NotNull HuskTowns plugin) {
       return claims.entrySet().stream()
               .filter(entry -> entry.getValue().stream().anyMatch(claim -> claim.getChunk().equals(chunk)))
               .findFirst()
               .flatMap(entry -> entry.getValue().stream()
                       .filter(claim -> claim.getChunk().equals(chunk))
                       .findFirst()
                       .flatMap(claim -> plugin.findTown(entry.getKey())
                               .map(town1 -> new TownClaim(town1, claim))))
               .or(() -> adminClaims.stream()
                       .filter(claim -> claim.getChunk().equals(chunk))
                       .findFirst()
                       .map(claim -> new TownClaim(plugin.getAdminTown(), claim)));
   }

and stream filtering is always going to be slow but, there might be a better way of doing it, someone smarter than me will know more than me though

Catch another spike that actually dropped my tps to 17, but I think it's a different function this time https://spark.lucko.me/ls3a0fXsj9

ProdPreva1l commented 7 months ago

Different listener but same function, its getting the claim object (if it exists) and checking the flag for that event, it might be better to cache all claims at startup but that could cause higher ram, again not too sure the best way to go about this

matipoirierg commented 7 months ago

Different listener but same function, its getting the claim object (if it exists) and checking the flag for that event, it might be better to cache all claims at startup but that could cause higher ram, again not too sure the best way to go about this

thanks for the clarification

MSCMDD commented 7 months ago

Here is also a performance issue

425

image

matipoirierg commented 7 months ago

This situation is worse than I anticipated. My TPS is dropping to 14-15, especially when there are high player counts.

WiIIiam278 commented 7 months ago

Seems like we need to take a look at some stream logic & make some optimisations :)

WiIIiam278 commented 7 months ago

Going to close this as a duplicate of #425 :)