Skip to content

Commit

Permalink
Finish the BlockData caching in Bukkit
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 28, 2018
1 parent c931095 commit fdb9d77
Showing 1 changed file with 9 additions and 1 deletion.
Expand Up @@ -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
*
Expand All @@ -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();
}

/**
Expand Down

9 comments on commit fdb9d77

@hafarooki
Copy link
Contributor

@hafarooki hafarooki commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@me4502 me4502 commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Miclebrick Not unless Bukkit becomes a decent API

@hafarooki
Copy link
Contributor

@hafarooki hafarooki commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@me4502
Copy link
Member Author

@me4502 me4502 commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@hafarooki
Copy link
Contributor

@hafarooki hafarooki commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@me4502
Copy link
Member Author

@me4502 me4502 commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@hafarooki
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@me4502
Copy link
Member Author

@me4502 me4502 commented on fdb9d77 Sep 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@hafarooki
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh 😛

Please sign in to comment.