Skip to content

Commit 27e3587

Browse files
committed
fix: avoid quadratic String traversals, resolving a fixme
1 parent 5b2927c commit 27e3587

File tree

1 file changed

+10
-9
lines changed
  • tests/integration/lib/StackTest

1 file changed

+10
-9
lines changed

tests/integration/lib/StackTest/Repl.hs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ module StackTest.Repl
1313
import Control.Exception (SomeException, catch, displayException, finally)
1414
import Control.Monad (unless, when)
1515
import Control.Monad.IO.Class (liftIO)
16+
import Control.Monad.Trans (lift)
17+
import Control.Monad.Trans.Reader
18+
import Control.Monad.Trans.State qualified as State
1619
import Data.Maybe (fromMaybe)
20+
import Data.Foldable (toList)
21+
import Data.Sequence as Seq (Seq(Empty), (|>), fromList)
1722
import GHC.Stack (HasCallStack)
1823
import System.Directory (removeFile)
1924
import System.Environment (lookupEnv)
@@ -24,10 +29,6 @@ import System.IO
2429
, openTempFile
2530
, withFile
2631
)
27-
28-
import Control.Monad.Trans (lift)
29-
import Control.Monad.Trans.Reader
30-
import Control.Monad.Trans.State qualified as State
3132
import System.Process
3233
( CreateProcess (std_err, std_in, std_out)
3334
, StdStream (CreatePipe, UseHandle)
@@ -54,15 +55,15 @@ replGetLine :: Repl String
5455
replGetLine = ask >>= liftIO . hGetLine . replStdout
5556

5657
nextPrompt :: Repl ()
57-
nextPrompt = State.evalStateT poll "" where
58+
nextPrompt = State.evalStateT poll Seq.Empty where
5859
poll = do
5960
c <- lift (asks replStdout) >>= liftIO . hGetChar
60-
State.modify (++ [c]) -- FIXME crap perf
61+
State.modify (|> c)
6162
when (c == '\n') $ do
62-
State.get >>= liftIO . putStr . ("ghci> " <>)
63-
State.put ""
63+
State.get >>= liftIO . putStr . ("ghci> " ++) . toList
64+
State.put Seq.Empty
6465
buf <- State.get
65-
unless (buf == "ghci> ")
66+
unless (buf == Seq.fromList "ghci> ")
6667
poll
6768

6869
runRepl

0 commit comments

Comments
 (0)