From 26bf10f31bc7620e21c0cb19f53dffd07b1d5890 Mon Sep 17 00:00:00 2001 From: Sung-Shik Jongmans Date: Wed, 5 Aug 2026 13:02:41 +0200 Subject: [PATCH] Update `resolvePaths` by separating initialization and updating of `pathsByPathRole`, and by making the initialization part faster --- src/analysis/typepal/Solver.rsc | 49 ++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/analysis/typepal/Solver.rsc b/src/analysis/typepal/Solver.rsc index 2a118e9..8f91615 100644 --- a/src/analysis/typepal/Solver.rsc +++ b/src/analysis/typepal/Solver.rsc @@ -1024,18 +1024,47 @@ Solver newSolver(map[str,Tree] namedTrees, TModel tm){ } newPaths = { tup | tup: <- newPaths, u != d }; tm.referPaths = referPaths; - newPathFound = !isEmpty(newPaths); - if( newPathFound // we found new paths - || (!isEmpty(tm.paths) && isEmpty(pathsByPathRole)) // pathsByPathRole not yet initialized - ){ - tm.paths += newPaths; - pathsByPathRole = (); - for( <- tm.paths){ - pathsByPathRole[r] ? {} += {}; - } + tm.paths += newPaths; + + initPathsByPathRole(); + updatePathsByPathRole(newPaths); + return !isEmpty(newPaths); + } + + void initPathsByPathRole() { + if (!isEmpty(pathsByPathRole)) { // Already initialized + return; } - return newPathFound; + paths = tm.paths; + if (isEmpty(paths)) { // Nothing to initialize + return; + } + + pathsByPathRole = (r: {} | <_, PathRole r, _> <- paths); + for (PathRole r <- pathsByPathRole) { + pathsByPathRole[r] = { | <- paths}; + } + // That is, first compute the keys, and second compute the total + // values. It seems to be significantly faster than computing keys + // and total values together: + // ``` + // pathsByPathRole = (r: { | <- paths} | <_, PathRole r, _> <- paths); + // ``` + // It also seems to be significantly faster than computing keys + // and partial values iteratively: + // ``` + // pathsByPathRole = (); + // for ( <- tm.paths) { + // pathsByPathRole[r] ? {} += {}; + // } + // ``` + } + + void updatePathsByPathRole(Paths newPaths) { + for( <- newPaths){ + pathsByPathRole[r] ? {} += {}; + } } // ---- "equal" and "requireEqual" ----------------------------------------