summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInari Listenmaa <inari.listenmaa@gmail.com>2022-10-10 12:00:23 +0200
committerGitHub <noreply@github.com>2022-10-10 12:00:23 +0200
commit6edd449d68a7dcf68eb3fff31370018dc6508a70 (patch)
tree8f0549380ca8d4193666668eb59d5f36e8e4793f
parent3122590e351f769ca6e60dfd4eeaafba1c5c22e8 (diff)
parenta58c6d49d4b5d5cf750c36c9071279fccd9c468f (diff)
Merge pull request #147 from anka-213/extend-performance-issue
Improve performance with long extend-lists
-rw-r--r--src/compiler/GF/Grammar/Grammar.hs19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/compiler/GF/Grammar/Grammar.hs b/src/compiler/GF/Grammar/Grammar.hs
index 587b09a9f..448fa1657 100644
--- a/src/compiler/GF/Grammar/Grammar.hs
+++ b/src/compiler/GF/Grammar/Grammar.hs
@@ -78,6 +78,7 @@ import PGF.Internal (FId, FunId, SeqId, LIndex, Sequence, BindType(..))
import Data.Array.IArray(Array)
import Data.Array.Unboxed(UArray)
import qualified Data.Map as Map
+import qualified Data.Set as Set
import GF.Text.Pretty
@@ -125,10 +126,20 @@ extends :: ModuleInfo -> [ModuleName]
extends = map fst . mextend
isInherited :: MInclude -> Ident -> Bool
-isInherited c i = case c of
- MIAll -> True
- MIOnly is -> elem i is
- MIExcept is -> notElem i is
+isInherited c =
+ case c of
+ MIAll -> const True
+ MIOnly is -> elemOrd is
+ MIExcept is -> not . elemOrd is
+
+-- | Faster version of `elem`, using a `Set`.
+-- Make sure you give this the first argument _outside_ of the inner loop
+--
+-- Example:
+-- > myIntersection xs ys = filter (elemOrd xs) ys
+elemOrd :: Ord a => [a] -> a -> Bool
+elemOrd list = (`Set.member` set)
+ where set = Set.fromList list
inheritAll :: ModuleName -> (ModuleName,MInclude)
inheritAll i = (i,MIAll)