Skip to content

Commit 30d6792

Browse files
committed
Cosmetic changes/cleanup
1 parent 26ee6a2 commit 30d6792

File tree

6 files changed

+42
-38
lines changed

6 files changed

+42
-38
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public BWClientConfiguration getConfiguration() {
4545
* @return Whether the current frame should be subject to timing.
4646
*/
4747
boolean doTime() {
48-
return ! configuration.getUnlimitedFrameZero() || (client.isConnected() && client.clientData().gameData().getFrameCount() > 0);
48+
return ! configuration.getUnlimitedFrameZero() || (client.isConnected() && client.liveClientData().gameData().getFrameCount() > 0);
4949
}
5050

5151
/**
5252
* @return The number of frames between the one exposed to the bot and the most recent received by JBWAPI.
5353
* This tracks the size of the frame buffer except when the game is paused (which results in multiple frames arriving with the same count).
5454
*/
5555
public int framesBehind() {
56-
return botWrapper == null ? 0 : Math.max(0, client.clientData().gameData().getFrameCount() - getGame().getFrameCount());
56+
return botWrapper == null ? 0 : Math.max(0, client.liveClientData().gameData().getFrameCount() - getGame().getFrameCount());
5757
}
5858

5959
/**
@@ -107,7 +107,7 @@ public void startGame(BWClientConfiguration gameConfiguration) {
107107
client.reconnect();
108108

109109
do {
110-
ClientData.GameData liveGameData = client.clientData().gameData();
110+
ClientData.GameData liveGameData = client.liveClientData().gameData();
111111
while (!liveGameData.isInGame()) {
112112
if (!client.isConnected()) {
113113
return;

src/main/java/bwapi/BotWrapper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ private Thread createBotThread() {
206206
if (doUnsafeRead) {
207207
configuration.log("Bot: Reading live frame");
208208
setUnsafeReadReady(false);
209-
// TODO: Maybe we should point it at live data from here?
210209
} else {
211210
configuration.log("Bot: Peeking next frame from buffer");
212211
botGame.botClientData().setBuffer(frameBuffer.peek());

src/main/java/bwapi/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ interface MappingKernel extends Kernel32 {
6363
clientData.setBuffer(buffer);
6464
}
6565

66-
ClientData clientData() {
66+
ClientData liveClientData() {
6767
return clientData;
6868
}
6969

src/main/java/bwapi/FrameBuffer.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ private boolean tryMemcpyBuffer(ByteBuffer source, ByteBuffer destination, long
181181
void copyBuffer(ByteBuffer source, ByteBuffer destination, boolean copyEverything) {
182182
/*
183183
The speed at which we copy data into the frame buffer is a major cost of JBWAPI's asynchronous operation.
184-
Copy times observed in the wild usually range from 2.6ms - 19ms but are prone to large amounts of variance.
184+
Copy times observed in the wild for the complete buffer usually range from 2.6ms - 19ms
185+
but are prone to large amounts of variance.
185186
186187
The normal Java way to execute this copy is via ByteBuffer.put(), which has reasonably good performance characteristics.
187-
Some experiments in 64-bit JRE have shown that using a native memcpy achieves a 35% speedup.
188-
Some experiments in 32-bit JRE show no difference in performance.
188+
Experiments in 64-bit JRE have shown that using a native memcpy achieves a 35% speedup.
189+
Experiments in 32-bit JRE show no difference in performance.
189190
190191
So, speculatively, we attempt to do a native memcpy.
191192
*/
@@ -195,13 +196,17 @@ void copyBuffer(ByteBuffer source, ByteBuffer destination, boolean copyEverythin
195196
return;
196197
}
197198
} else {
198-
int STATICTILES_START = 3447004; // getGroundHeight, isWalkable, isBuildable
199-
int STATICTILES_END = 4823260;
200-
int REGION_START = 5085404; // getMapTileRegionId, ..., getRegions
201-
int REGION_END = 10586480;
202-
int STRINGSSHAPES_START = 10962632; // getStringCount, ... getShapes
203-
int STRINGSHAPES_END = 32242636;
204-
int UNITFINDER_START = 32962644;
199+
// After the buffer has been filled the first time,
200+
// we can omit copying blocks of data which are unused or which don't change after game start.
201+
// These blocks account for *most* of the 33MB shared memory,
202+
// so omitting them drastically reduces the copy duration
203+
final int STATICTILES_START = 3447004; // getGroundHeight, isWalkable, isBuildable
204+
final int STATICTILES_END = 4823260;
205+
final int REGION_START = 5085404; // getMapTileRegionId, ..., getRegions
206+
final int REGION_END = 10586480;
207+
final int STRINGSSHAPES_START = 10962632; // getStringCount, ... getShapes
208+
final int STRINGSHAPES_END = 32242636;
209+
final int UNITFINDER_START = 32962644;
205210
if (
206211
tryMemcpyBuffer(source, destination, 0, STATICTILES_START)
207212
&& tryMemcpyBuffer(source, destination, STATICTILES_END, REGION_START - STATICTILES_END)

src/test/java/bwapi/ClientDataBenchmark.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public static class FilledWithStrings {
3434

3535
@Setup(Level.Invocation)
3636
public void setup() {
37-
data = client.clientData().gameData();
37+
data = client.liveClientData().gameData();
3838
game = new Game();
3939
game.botClientData().setBuffer(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
4040
String[] strings = buildStrings();
4141
for (String s : strings) {
42-
GameDataUtils.addString(client.clientData().gameData(), s);
42+
GameDataUtils.addString(client.liveClientData().gameData(), s);
4343
}
4444
}
4545
}
@@ -69,16 +69,16 @@ public int addUnitCommand(EmptyState s) {
6969
for (int i = 0; i < GameDataUtils.MAX_COUNT; i++) {
7070
s.game.addUnitCommand(0, 1, 2, 3, 4, 5);
7171
}
72-
return s.client.clientData().gameData().getCommandCount();
72+
return s.client.liveClientData().gameData().getCommandCount();
7373
}
7474

7575
@Benchmark
7676
@OperationsPerInvocation(GameDataUtils.MAX_COUNT)
7777
public int addString(EmptyState s) {
7878
for (int i = 0; i < GameDataUtils.MAX_COUNT; i++) {
79-
GameDataUtils.addString(s.client.clientData().gameData(), s.strings[i]);
79+
GameDataUtils.addString(s.client.liveClientData().gameData(), s.strings[i]);
8080
}
81-
return s.client.clientData().gameData().getStringCount();
81+
return s.client.liveClientData().gameData().getStringCount();
8282
}
8383

8484
@Benchmark

src/test/java/bwapi/SynchronizationEnvironment.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class SynchronizationEnvironment {
3333
onFrames = new HashMap<>();
3434

3535
when(client.mapFile()).thenReturn(GameBuilder.binToBufferUnchecked(GameBuilder.DEFAULT_BUFFER_PATH));
36-
when(client.clientData()).thenReturn(new ClientData());
37-
client.clientData().setBuffer(client.mapFile());
38-
client.clientData().gameData().setFrameCount(-1);
39-
client.clientData().gameData().setIsInGame(false);
36+
when(client.liveClientData()).thenReturn(new ClientData());
37+
client.liveClientData().setBuffer(client.mapFile());
38+
client.liveClientData().gameData().setFrameCount(-1);
39+
client.liveClientData().gameData().setIsInGame(false);
4040

4141
when(client.isConnected()).thenReturn(true);
4242
doAnswer(answer -> {
@@ -63,7 +63,7 @@ class SynchronizationEnvironment {
6363
}
6464

6565
ClientData.GameData liveGameData() {
66-
return client.clientData().gameData();
66+
return client.liveClientData().gameData();
6767
}
6868

6969
PerformanceMetrics metrics() {
@@ -102,28 +102,28 @@ void runGame(int onEndFrame) {
102102
}
103103

104104
private int liveFrame() {
105-
return client.clientData().gameData().getFrameCount();
105+
return client.liveClientData().gameData().getFrameCount();
106106
}
107107

108108
private void clientUpdate() throws InterruptedException{
109109
Thread.sleep(bwapiDelayMs);
110-
client.clientData().gameData().setFrameCount(liveFrame() + 1);
110+
client.liveClientData().gameData().setFrameCount(liveFrame() + 1);
111111
configuration.log("Test: clientUpdate() to liveFrame #" + liveFrame());
112112
if (liveFrame() == 0) {
113-
client.clientData().gameData().setIsInGame(true);
114-
client.clientData().gameData().setEventCount(2);
115-
client.clientData().gameData().getEvents(0).setType(EventType.MatchStart);
116-
client.clientData().gameData().getEvents(1).setType(EventType.MatchFrame);
113+
client.liveClientData().gameData().setIsInGame(true);
114+
client.liveClientData().gameData().setEventCount(2);
115+
client.liveClientData().gameData().getEvents(0).setType(EventType.MatchStart);
116+
client.liveClientData().gameData().getEvents(1).setType(EventType.MatchFrame);
117117
} else if (liveFrame() < onEndFrame) {
118-
client.clientData().gameData().setIsInGame(true);
119-
client.clientData().gameData().setEventCount(1);
120-
client.clientData().gameData().getEvents(0).setType(EventType.MatchFrame);
118+
client.liveClientData().gameData().setIsInGame(true);
119+
client.liveClientData().gameData().setEventCount(1);
120+
client.liveClientData().gameData().getEvents(0).setType(EventType.MatchFrame);
121121
} else if (liveFrame() == onEndFrame) {
122-
client.clientData().gameData().setIsInGame(true);
123-
client.clientData().gameData().getEvents(0).setType(EventType.MatchEnd);
122+
client.liveClientData().gameData().setIsInGame(true);
123+
client.liveClientData().gameData().getEvents(0).setType(EventType.MatchEnd);
124124
} else {
125-
client.clientData().gameData().setIsInGame(false);
126-
client.clientData().gameData().setEventCount(0);
125+
client.liveClientData().gameData().setIsInGame(false);
126+
client.liveClientData().gameData().setEventCount(0);
127127
}
128128
}
129129
}

0 commit comments

Comments
 (0)