Repository navigation
Optimize jbwapi - #110
Draft
JasperGeurtz wants to merge 7 commits into
Draft
Optimize jbwapi#110JasperGeurtz wants to merge 7 commits into
JasperGeurtz wants to merge 7 commits into
Conversation
added 7 commits
November 8, 2026 12:26
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR reduces per-frame CPU overhead, shared-memory
ByteBufferreads, and heap/GC allocation pressureLazy Per-Frame
UnitListCache(Game,Player):Game.getMinerals(),Game.getGeysers(),Game.getNeutralUnits(),Player.getUnits(), and internalselfPylons(Game.hasPower) with lazy per-frameUnitListCacheinstances that compute at most once per frame on demand and return the cached unmodifiable list on subsequent calls.Lazy Write-Only Latency Compensation (
UnitSelf,PlayerSelf):Unit.self(37Cacheobjects per unit) andPlayer.self(520Cacheobjects per player) are now allocated lazily only whenCommandTempmutates latency-compensated state, rather than on the first getter read or unconditionally on construction.Allocation-Free
Unit.getDistance& StationaryUnit.updatePosition(Point,Unit):Unit.getDistance(Position)andUnit.getDistance(Unit)now readgetType()andpositiononce per unit (instead of 4–8 times viagetLeft()/getTop()/getRight()/getBottom()) and call package-privatePoint.getApproxDistance(0, 0, xDist, yDist)directly instead of allocating a temporaryPosition.Unit.updatePosition(int)only allocates a newPositionwhen((position.x ^ newX) | (position.y ^ newY)) != 0, avoiding heap allocations and Old-Gen → Young-Gen card-table dirtying for stationary units (minerals, geysers, buildings, sieged/holding units).Single-Pass
getUnitsInRectangle&canBuildHere(Game,Unit):getType()calls per unit inGame.getUnitsInRectanglewith a direct loop usingUnit.overlapsRectangle.Position/TilePositionallocations and fullgetUnitsInRectanglelist materialization inGame.canBuildHerewith primitive coordinate math and an early-exit ground unit overlap check.Cached Immutable Return Wrappers (
UnitType,TechType,UpgradeType):tileSize(),whatBuilds(),requiredUnits(),abilities(),upgrades(),buildsWhat(),researchesWhat(),upgradesWhat(),maxUnitWidth(), andmaxUnitHeight()onUnitType/UnitTypeContainer, as well aswhatUses()onTechTypeandUpgradeType.getTrainingQueue().size()andgetTrainingQueue().get(i)calls with allocation-freegetTrainingQueueCount()andgetTrainingQueueAt(i).BitSetforGame.visibleUnits(Game):HashSet<Integer>andvisibleUnits.stream()inGame.onFramewith aBitSetandnextSetBititeration into a pre-sizedArrayList.JMH Benchmark Results
Measured with
bwapi.PerformanceBenchmarkon JDK 1.8.0_452 ((4)Andromeda.scx, 356 visible units: 236 stationary resources/buildings + 120 mobile combat units):bwapi.PerformanceBenchmark)master)perf-optimizations)unitGetDistanceToPosition27.58 ± 3.61 µs15.52 ± 2.78 µsunitGetDistanceToUnit103.29 ± 26.84 µs64.01 ± 4.98 µscanBuildHere336.88 ± 44.05 µs232.74 ± 43.41 µs44 → 5GCs (-89%)getUnitsInRectangle184.84 ± 45.07 µs131.03 ± 3.93 µs38 → 28GCs (-26%)unitUpdatePositionPerFrame3.21 ± 0.45 µs2.65 ± 0.22 µs140 → 54GCs (-61%)visibleUnitsWorstCaseOnFrameonFrame16.09 ± 1.75 µs13.30 ± 0.48 µs41 → 12GCs (-71%)