Permalink
Browse files

Finish the BlockData caching in Bukkit

  • Loading branch information...
me4502 committed Aug 28, 2018
1 parent c931095 commit fdb9d77710379e955c2fbbe0386efdc816f5ccd1
Showing with 9 additions and 1 deletion.
  1. +9 −1 worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java
@@ -345,6 +345,8 @@ public BlockState apply(@Nullable String input) {
});
}
private static Map<String, BlockData> blockDataCache = new HashMap<>();
/**
* Create a Bukkit BlockData from a WorldEdit BlockStateHolder
*
@@ -353,7 +355,13 @@ public BlockState apply(@Nullable String input) {
*/
public static BlockData adapt(BlockStateHolder block) {
checkNotNull(block);
return Bukkit.createBlockData(block.getAsString());
return blockDataCache.computeIfAbsent(block.getAsString(), new Function<String, BlockData>() {
@Nullable
@Override
public BlockData apply(@Nullable String input) {
return Bukkit.createBlockData(block.getAsString());
}
}).clone();
}
/**

9 comments on commit fdb9d77

@Miclebrick

This comment has been minimized.

Show comment
Hide comment
@Miclebrick

Miclebrick Sep 1, 2018

Is there a more efficient way to do it than converting it to a string? (or is that less expensive than one might immediately expect)

Miclebrick replied Sep 1, 2018

Is there a more efficient way to do it than converting it to a string? (or is that less expensive than one might immediately expect)

@me4502

This comment has been minimized.

Show comment
Hide comment
@me4502

me4502 Sep 1, 2018

Collaborator

@Miclebrick Not unless Bukkit becomes a decent API

Collaborator

me4502 replied Sep 1, 2018

@Miclebrick Not unless Bukkit becomes a decent API

@Miclebrick

This comment has been minimized.

Show comment
Hide comment
@Miclebrick

Miclebrick Sep 1, 2018

Does worldedit have constants for block state internal IDs? If so you could map it by internal ID instead of string

Miclebrick replied Sep 1, 2018

Does worldedit have constants for block state internal IDs? If so you could map it by internal ID instead of string

@me4502

This comment has been minimized.

Show comment
Hide comment
@me4502

me4502 Sep 1, 2018

Collaborator

Worldedit doesn’t, no. Theoretically it could, I guess. I’ll see how slow this is, seeing as string hash lookups are ridiculously fast.

Collaborator

me4502 replied Sep 1, 2018

Worldedit doesn’t, no. Theoretically it could, I guess. I’ll see how slow this is, seeing as string hash lookups are ridiculously fast.

@Miclebrick

This comment has been minimized.

Show comment
Hide comment
@Miclebrick

Miclebrick Sep 1, 2018

It's more a question of how expensive block.getAsString() is I guess

Miclebrick replied Sep 1, 2018

It's more a question of how expensive block.getAsString() is I guess

@me4502

This comment has been minimized.

Show comment
Hide comment
@me4502

me4502 Sep 1, 2018

Collaborator

That’s not a Bukkit method, it should be fairly fast - worst case I can easily cache that as it can never change as it’s immutable

Collaborator

me4502 replied Sep 1, 2018

That’s not a Bukkit method, it should be fairly fast - worst case I can easily cache that as it can never change as it’s immutable

@Miclebrick

This comment has been minimized.

Show comment
Hide comment
@Miclebrick

Miclebrick Sep 1, 2018

Yeah just not sure how fast it is, it might be worth checking

Miclebrick replied Sep 1, 2018

Yeah just not sure how fast it is, it might be worth checking

@me4502

This comment has been minimized.

Show comment
Hide comment
@me4502

me4502 Sep 1, 2018

Collaborator

It’s literally some basic string concatenation so it should be very fast

Collaborator

me4502 replied Sep 1, 2018

It’s literally some basic string concatenation so it should be very fast

@Miclebrick

This comment has been minimized.

Show comment
Hide comment
@Miclebrick

Miclebrick replied Sep 1, 2018

oh 😛

Please sign in to comment.