Skip to content

Commit 769c431

Browse files
committed
Attempting to speed walkExpandedAtom up...
by getting macros directly instead of calling isNamespacedRecBound first to check if they are bound.
1 parent a3b4068 commit 769c431

File tree

1 file changed

+41
-55
lines changed

1 file changed

+41
-55
lines changed

hs-src/Language/Scheme/Macro.hs

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -636,52 +636,42 @@ walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv dim _ isQuoted (List r
636636
walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv dim False isQuoted (List $ result ++ [DottedList ls l]) (List ts) apply
637637

638638
walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv dim startOfList inputIsQuoted (List result) (List (Atom aa : ts)) apply = do
639-
640639
Atom a <- expandAtom renameEnv (Atom aa)
641-
640+
maybeMacro <- findBoundMacro defEnv useEnv a
642641
-- If a macro is quoted, keep track of it and do not invoke rules below for
643642
-- procedure abstraction or macro calls
644643
let isQuoted = inputIsQuoted || (a == "quote")
645644

646-
isDefinedAsMacro <- liftIO $ isNamespacedRecBound useEnv macroNamespace a
647-
isDefDefinedAsMacro <- liftIO $ isNamespacedRecBound defEnv macroNamespace a
648-
649-
-- (currently) unused conditional variables for below test
650-
--isDiverted <- liftIO $ isRecBound divertEnv a
651-
--isMacroBound <- liftIO $ isRecBound renameEnv a
652-
--isLocalRename <- liftIO $ isNamespacedRecBound renameEnv 'r' {-"renamed"-} a
653-
654-
-- Determine if we should recursively rename an atom
655-
-- This code is a bit of a hack/mess at the moment
656-
if isDefinedAsMacro || isDefDefinedAsMacro
657-
-- if (trace ("walkExp " ++ a ++ " " ++ aa ++ " " ++ (show isDefinedAsMacro) ++ " " ++ (show isDefDefinedAsMacro) ++ " " ++ (show startOfList) ++ " " ++ (show inputIsQuoted) ++ " " ++ (show isQuoted) ++ " ") isDefinedAsMacro || isDefDefinedAsMacro)
658-
-- || isDiverted
659-
-- || (isMacroBound && not isLocalRename)
660-
-- || not startOfList
661-
|| a == aa -- Prevent an infinite loop
662-
-- Preserve keywords encountered in the macro
663-
-- as each of these is really a special form, and renaming them
664-
-- would not work because there is nothing to divert back...
665-
|| a == "if"
666-
|| a == "let-syntax"
667-
|| a == "letrec-syntax"
668-
|| a == "define-syntax"
669-
|| a == "define"
670-
|| a == "set!"
671-
|| a == "lambda"
672-
|| a == "quote"
673-
|| a == "expand"
674-
|| a == "string-set!"
675-
|| a == "set-car!"
676-
|| a == "set-cdr!"
677-
|| a == "vector-set!"
678-
|| a == "hash-table-set!"
679-
|| a == "hash-table-delete!"
680-
then walkExpandedAtom defEnv useEnv divertEnv renameEnv cleanupEnv
681-
dim startOfList inputIsQuoted (List result) a ts isQuoted (isDefinedAsMacro || isDefDefinedAsMacro) apply
682-
else walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv
683-
dim startOfList inputIsQuoted (List result) (List (Atom a : ts)) apply
684-
645+
case maybeMacro of
646+
Just _ -> walkExpandedAtom defEnv useEnv divertEnv renameEnv cleanupEnv
647+
dim startOfList inputIsQuoted (List result)
648+
a ts isQuoted maybeMacro apply
649+
_ -> do
650+
-- Determine if we should recursively rename an atom
651+
-- This code is a bit of a hack/mess at the moment
652+
if a == aa -- Prevent an infinite loop
653+
-- Preserve keywords encountered in the macro
654+
-- as each of these is really a special form, and renaming them
655+
-- would not work because there is nothing to divert back...
656+
|| a == "if"
657+
|| a == "let-syntax"
658+
|| a == "letrec-syntax"
659+
|| a == "define-syntax"
660+
|| a == "define"
661+
|| a == "set!"
662+
|| a == "lambda"
663+
|| a == "quote"
664+
|| a == "expand"
665+
|| a == "string-set!"
666+
|| a == "set-car!"
667+
|| a == "set-cdr!"
668+
|| a == "vector-set!"
669+
|| a == "hash-table-set!"
670+
|| a == "hash-table-delete!"
671+
then walkExpandedAtom defEnv useEnv divertEnv renameEnv cleanupEnv
672+
dim startOfList inputIsQuoted (List result) a ts isQuoted maybeMacro apply
673+
else walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv
674+
dim startOfList inputIsQuoted (List result) (List (Atom a : ts)) apply
685675

686676
-- Transform anything else as itself...
687677
walkExpanded defEnv useEnv divertEnv renameEnv cleanupEnv dim _ isQuoted (List result) (List (t : ts)) apply = do
@@ -709,7 +699,7 @@ walkExpandedAtom :: Env
709699
-> String
710700
-> [LispVal]
711701
-> Bool -- is Quoted
712-
-> Bool -- is defined as macro
702+
-> Maybe LispVal -- is defined as macro
713703
-> (LispVal -> LispVal -> [LispVal] -> IOThrowsError LispVal) -- ^Apply func
714704
-> IOThrowsError LispVal
715705

@@ -858,19 +848,8 @@ walkExpandedAtom defEnv useEnv divertEnv renameEnv cleanupEnv dim True _ (List r
858848
walkExpandedAtom defEnv useEnv divertEnv renameEnv cleanupEnv dim True _ (List result)
859849
a
860850
ts
861-
False True apply = do
862-
synUse <- getNamespacedVar' useEnv macroNamespace a
863-
case synUse of
864-
Just syn -> expandSyntax syn
865-
_ -> do
866-
synDef <- getNamespacedVar' defEnv macroNamespace a
867-
case synDef of
868-
Just syn -> expandSyntax syn
869-
_ -> throwError $ Default
870-
"Unexpected error processing a symbol in walkExpandedAtom"
871-
where
872-
expandSyntax syn = do
873-
case syn of
851+
False (Just syn) apply = do
852+
case syn of
874853
--
875854
-- Note:
876855
--
@@ -1553,3 +1532,10 @@ isLexicallyDefined outerEnv renameEnv a = do
15531532
o <- liftIO $ isBound outerEnv a
15541533
r <- liftIO $ isBound renameEnv a
15551534
return $ o || r
1535+
1536+
findBoundMacro :: Env -> Env -> String -> IOThrowsError (Maybe LispVal)
1537+
findBoundMacro defEnv useEnv a = do
1538+
synUse <- getNamespacedVar' useEnv macroNamespace a
1539+
case synUse of
1540+
Just syn -> return $ Just syn
1541+
_ -> getNamespacedVar' defEnv macroNamespace a

0 commit comments

Comments
 (0)