Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-136025

Performance improvements on BlockPos cardinal offsets

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • Minecraft 1.12.2, Minecraft 1.13, 1.16.1, 1.16.2 Pre-release 1, 1.16.2, 1.16.4 Pre-release 2, 20w49a, 1.16.5, 21w10a, 1.17 Pre-release 1
    • None
    • Confirmed
    • Performance

      The current implementation of the BlockPos offset "helper" methods (BlockPos#up(int n), BlockPos#up(), etc.), can be improved by reducing the number of function calls. While the performance increase may seem insignificant, due to the sheer number of calls related to BlockPos checks any improvement is valuable.

      Instead of complicating the offset through the use of EnumFacing and the likes, the methods could instead directly create a new BlockPos with the offset. A full example can be found here:
      >https://github.com/cabaletta/baritone/blob/3a68009194ee46930d9609947c93863a82c7d180/src/main/java/baritone/bot/utils/pathing/BetterBlockPos.java#L29

      Benchmarking our BetterBlockPos against MC's BlockPos shifting methods, we see a clear performance increase (see the two tables below, and generated from https://github.com/cabaletta/baritone/blob/3a68009194ee46930d9609947c93863a82c7d180/src/test/java/baritone/bot/utils/pathing/BetterBlockPosTest.java#L23). This is even including the Hotspot VM optimizations, which we can see kick in after the first two rows.

      Also, to be clear, our implementation creates the exact same number of objects that the Vanilla version does, so any concerns about garbage collection performance shouldn't be considered around this specific fix.

      Benchmark for up()

      Vanilla Modified
      43 6
      15 9
      3 0
      2 1
      1 1
      3 1
      2 0
      2 0
      2 1
      1 0

      Benchmark for up(int n)

      Vanilla Modified
      28 10
      38 0
      6 0
      6 0
      4 0
      5 0
      6 0
      5 1
      6 0
      6 0

            Unassigned Unassigned
            howardstark Howard Stark
            Votes:
            16 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              CHK: