From e9cad49d94b0b6d94ef9164096b12dcd0bf3c7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?0hm=E2=98=98=EF=B8=8F?= Date: Fri, 26 Jun 2026 21:48:33 +0530 Subject: [PATCH] index serialized graph lookups --- lib/compat/loadSerializedHyperGraph.ts | 50 +++++++++++++------------- lib/core.ts | 14 ++++---- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/lib/compat/loadSerializedHyperGraph.ts b/lib/compat/loadSerializedHyperGraph.ts index 6dc4eba..036f50f 100644 --- a/lib/compat/loadSerializedHyperGraph.ts +++ b/lib/compat/loadSerializedHyperGraph.ts @@ -312,20 +312,6 @@ const getCentermostPortIdForRegion = ( return sortedPortIds[0] } -const getSharedPortIdsForConnection = ( - serializedHyperGraph: SerializedHyperGraph, - connection: NonNullable[number], -): string[] => - serializedHyperGraph.ports - .filter( - (port) => - (port.region1Id === connection.startRegionId && - port.region2Id === connection.endRegionId) || - (port.region2Id === connection.startRegionId && - port.region1Id === connection.endRegionId), - ) - .map((port) => port.portId) - export const loadSerializedHyperGraph = ( serializedHyperGraph: SerializedHyperGraph, ): { @@ -335,8 +321,10 @@ export const loadSerializedHyperGraph = ( } => { const filteredHyperGraph = filterObstacleRegions(serializedHyperGraph) const regionIdToIndex = new Map() + const regionById = new Map() const portIdToIndex = new Map() const portById = new Map() + const portIdsByRegionPair = new Map() const solvedRouteByConnectionId = new Map( (filteredHyperGraph.solvedRoutes ?? []).map((route) => [ route.connection.connectionId, @@ -346,11 +334,23 @@ export const loadSerializedHyperGraph = ( filteredHyperGraph.regions.forEach((region, regionIndex) => { regionIdToIndex.set(region.regionId, regionIndex) + regionById.set(region.regionId, region) }) filteredHyperGraph.ports.forEach((port, portIndex) => { portIdToIndex.set(port.portId, portIndex) portById.set(port.portId, port) + const lesserRegionId = + port.region1Id < port.region2Id ? port.region1Id : port.region2Id + const greaterRegionId = + port.region1Id < port.region2Id ? port.region2Id : port.region1Id + const regionPairKey = `${lesserRegionId}\0${greaterRegionId}` + const portIds = portIdsByRegionPair.get(regionPairKey) + if (portIds) { + portIds.push(port.portId) + } else { + portIdsByRegionPair.set(regionPairKey, [port.portId]) + } }) const regionCount = filteredHyperGraph.regions.length @@ -485,10 +485,16 @@ export const loadSerializedHyperGraph = ( const routableConnections = connections .map((connection) => { const solvedRoute = solvedRouteByConnectionId.get(connection.connectionId) - const sharedPortIds = getSharedPortIdsForConnection( - filteredHyperGraph, - connection, - ) + const lesserRegionId = + connection.startRegionId < connection.endRegionId + ? connection.startRegionId + : connection.endRegionId + const greaterRegionId = + connection.startRegionId < connection.endRegionId + ? connection.endRegionId + : connection.startRegionId + const sharedPortIds = + portIdsByRegionPair.get(`${lesserRegionId}\0${greaterRegionId}`) ?? [] return { connection, @@ -509,15 +515,11 @@ export const loadSerializedHyperGraph = ( routableConnections.forEach(({ connection, solvedRoute }, routeIndex) => { const fallbackStartPortId = getCentermostPortIdForRegion( - filteredHyperGraph.regions.find( - (region) => region.regionId === connection.startRegionId, - ), + regionById.get(connection.startRegionId), portById, ) const fallbackEndPortId = getCentermostPortIdForRegion( - filteredHyperGraph.regions.find( - (region) => region.regionId === connection.endRegionId, - ), + regionById.get(connection.endRegionId), portById, ) diff --git a/lib/core.ts b/lib/core.ts index d334dc5..216f65d 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -566,10 +566,6 @@ export class TinyHyperGraphSolver extends BaseSolver { if (neighborPortId === currentCandidate.portId) continue if (problem.portSectionMask[neighborPortId] === 0) continue - const g = this.computeG(currentCandidate, neighborPortId) - if (!Number.isFinite(g)) continue - const h = this.computeH(neighborPortId) - const nextRegionId = topology.incidentPortRegion[neighborPortId][0] === currentCandidate.nextRegionId @@ -583,6 +579,13 @@ export class TinyHyperGraphSolver extends BaseSolver { continue } + const g = this.computeG(currentCandidate, neighborPortId) + if (!Number.isFinite(g)) continue + + const candidateHopId = this.getHopId(neighborPortId, nextRegionId) + if (g >= this.getCandidateBestCost(candidateHopId)) continue + + const h = this.computeH(neighborPortId) const newCandidate = { prevRegionId: currentCandidate.nextRegionId, nextRegionId, @@ -598,9 +601,6 @@ export class TinyHyperGraphSolver extends BaseSolver { return } - const candidateHopId = this.getHopId(neighborPortId, nextRegionId) - if (g >= this.getCandidateBestCost(candidateHopId)) continue - this.setCandidateBestCost(candidateHopId, g) state.candidateQueue.queue(newCandidate) }