Skip to content

perf(maps): fill a LongArrayDistanceMatrix without a location look-up per distance - #2877

Open
larsbeck wants to merge 4 commits into
TimefoldAI:mainfrom
larsbeck:maps/bulk-matrix-write
Open

larsbeck wants to merge 4 commits into
TimefoldAI:mainfrom
larsbeck:maps/bulk-matrix-write

Conversation

@larsbeck

@larsbeck larsbeck commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

What

LongArrayDistanceMatrix.bulkWriter(origins, destinations) returns a writer that puts a distance by the position of its two locations in the given lists. It looks up each location once per call, not once per distance.

Why

put(from, to, distance) looks up both locations in a HashMap and adds both ids to the origin and destination sets for each distance. For a full matrix of 10,000 locations, that is 100 million calls, and these look-ups take most of the time to fill the matrix. A caller that fills a full matrix, or a block of one, can use the writer instead.

Measured on a laptop: filling a travel-time matrix and a distance matrix for 10,000 locations from a routing response took 8.7 s with put() and 1.2 s with the writer (median of 5 runs).

Changes

  • New bulkWriter(List<Location>, List<Location>) and the nested BulkWriter class. The locations count as origins and destinations from the call on, and the matrix grows if it needs more space.
  • The call fails with an IllegalArgumentException if the matrix would have more than the maximum number of locations.
  • get(), put() and the writer use one method that maps a location pair to its array slot.

Compatibility

New public method and class in the impl package. revapi passes.

Tests

./mvnw -pl service/maps/api clean install: 51 tests pass (6 new for the writer).

🤖 Generated with Claude Code

https://claude.ai/code/session_01UUczJSzTQ9zXe1wLE2raD9

…istance

LongArrayDistanceMatrix.bulkWriter(origins, destinations) looks up each
location once and returns a writer that puts a distance by the position
of its locations in the two lists. put() looks up both locations and
adds both ids to the origin and destination sets for each distance,
which dominates the time to fill a large matrix.

The rule that maps a location pair to its array slot is now in one
method, which get(), put() and the writer use.
Without the check, the capacity arithmetic overflows for 46,340 locations, so the writer skips the resize and fails at a later put.
@larsbeck
larsbeck requested a review from a team December 4, 2026 13:15
Copilot AI balanced review requested due to automatic review settings December 4, 2026 13:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The bulk writer mutates matrix state before rejecting oversized input, leaving it unusable for later writes, and indexed LinkedList access makes registration quadratic.

2 open issues
What changed in this PR

Adds a position-based bulk writer to speed up distance-matrix loading without repeated location lookups for each distance.

Changes:

  • Adds bulk location registration and indexed distance writes.
  • Shares array-slot calculation across reads and writes.
  • Adds six tests covering bulk writes, resizing, existing locations, and input validation.
File Description
service/​maps/​api/​src/​test/​java/​ai/​timefold/​solver/​service/​maps/​impl/​LongArrayDistanceMatrixTest.java Tests bulk-writer behavior and validation.
service/​maps/​api/​src/​main/​java/​ai/​timefold/​solver/​service/​maps/​impl/​LongArrayDistanceMatrix.java Implements the bulk writer and shared slot calculation.

Review effort: Balanced

The size check now counts the new locations of both lists before
registration starts. Before, a rejected call left the extra locations
in the matrix, so every later bulkWriter call failed.

Registration iterates the lists instead of get(i), so it stays linear
for lists without random access, such as a LinkedList.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the distance-matrix bulk-write changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@larsbeck
larsbeck requested review from jennedebleser and a balanced review from Copilot December 4, 2026 13:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The position-based bulk writer change has no active review issues.

No open issues 🎉

Review effort: Balanced

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