Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BestFirstSearch: switch to Long as State key #3942

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ private class BestFirstSearch private (
val visits = new Array[Int](tokens.length)
var keepSlowStates = !pruneSlowStates

type StateHash = Long

/** Returns true if it's OK to skip over state.
*/
def shouldEnterState(curr: State): Boolean = keepSlowStates ||
Expand All @@ -70,21 +68,17 @@ private class BestFirstSearch private (
}
} else None

def stateColumnKey(state: State): StateHash = state.column << 8 |
state.indentation

val memo = mutable.Map.empty[(Int, StateHash), State]
private val memo = mutable.Map.empty[Long, State]

def shortestPathMemo(
start: State,
stop: Token,
depth: Int,
maxCost: Int,
): Option[State] = {
val key = (start.depth, stateColumnKey(start))
val cachedState = memo.get(key)
if (cachedState.nonEmpty) cachedState
else {
val key = (start.indentation & 0xffL) | (start.column & 0xffffffL) << 8 |
(start.depth & 0xffffffffL) << 32
memo.get(key).orElse {
// Only update state if it reached stop.
val nextState = shortestPath(start, stop, depth, maxCost)
if (null == nextState) None
Expand Down
Loading