Skip to content

Make JBWAPI ready to connect as fast as possible - #97

Open
dgant wants to merge 8 commits into
JavaBWAPI:developfrom
dgant:develop
Open

dgant wants to merge 8 commits into
JavaBWAPI:developfrom
dgant:develop

Conversation

@dgant

@dgant dgant commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Most software stacks that run client bots (SC-Docker, Tournament Manager, SCHNAIL, for example) don't bother checking whether the client is connected before starting a game. There's not even an obviously good way to do this.

Instead they just launch the client bot, wait a few seconds, then launch StarCraft and pray the client is ready. This usually works, but when it doesn't, the bot fails to connect. The game is ruined.


This pull request aims to prevent failed connections by reducing the time required for client bots to be ready for connection:

  • Don't allocate the asynchronous frame buffer until after connection.
  • Don't bind the whole Kernel32 interface, just the functions we need: OpenFileMapping and MapViewOfFile.

These changes were vibecoded but have undergone several rounds of iteration and testing.

client.reconnect();

// Allocate game bookkeeping and async frame buffers only after the BWAPI handshake.
this.performanceMetrics = new PerformanceMetrics(configuration);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My naive assumption is that change is actually the most important for the startup boost (e.g. the Client starting to listen ASAP). Do you have individual timing on how much each of the 3 changes actually brings individually?

@dgant dgant Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  • Using pre-extracted JNA DLL: -45%
  • Defer buffer allocation: -26%
  • Narrow kernel32 bindings: -7%

The deferred buffer allocation is certainly the easiest win though :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok interesting, thanks for the numbers. I would prefer putting the "Using pre-extracted JNA DLL" (most complicated) in another PR which I can look at in more detail another time, the deferral and narrow bindings look like free wins, those LGTM for this CL, thanks a bunch!

Comment thread src/main/java/bwapi/Client.java Outdated

Client(BWClient bwClient) {
this.bwClient = bwClient;
JnaNativeLoader.initialize();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: as we are checking the windowsOs one line below and this optimization is for windows anyways, perhaps we can transform this in a simple if else instead?

if (System.getProperty("os.name").toLowerCase().contains("win")) {
   JnaNativeLoader.initialize();
   clientConnector = new ClientConnectionW32();
} else {
   clientConnector = new ClientConnectionPosix();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

From my other comment, maybe let's move these changes to another PR, so I can look at those separately from the two other speedups.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done #103

dgant added 2 commits November 2, 2026 11:58
Defer frame buffers and metrics until after the BWAPI handshake, and use a focused Windows shared-memory mapping interface.

Automatically load an architecture-named JNA DLL beside the bot JAR after verifying it against the embedded binary. Preserve explicit JNA settings and ordinary loading when no matching bundle is available, without a custom native cache.

Package portable Windows DLLs beside the JAR and cover native loading, startup ordering, mapping behavior, and Windows Java 8 x86 in tests and CI.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants