[commit: ghc] master: Improve X86CodeGen's pprASCII. (2987b04)
git at git.haskell.org
git at git.haskell.org
Tue Feb 6 19:22:04 UTC 2018
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/2987b041a3811b25bcee402ce6fdab80827dc90e/ghc
>---------------------------------------------------------------
commit 2987b041a3811b25bcee402ce6fdab80827dc90e
Author: HE, Tao <sighingnow at gmail.com>
Date: Tue Feb 6 13:29:40 2018 -0500
Improve X86CodeGen's pprASCII.
The original implementation generates a list of SDoc then concatenates
them using `hcat`. For memory optimization, we can transform the given
literal string into escaped string the construct SDoc directly.
This optimization will decreate the memory allocation when there's big
literal strings in haskell code, see Trac #14741.
Signed-off-by: HE, Tao <sighingnow at gmail.com>
Reviewers: bgamari, mpickering, simonpj
Reviewed By: simonpj
Subscribers: simonpj, rwbarton, thomie, carter
GHC Trac Issues: #14741
Differential Revision: https://phabricator.haskell.org/D4384
>---------------------------------------------------------------
2987b041a3811b25bcee402ce6fdab80827dc90e
compiler/nativeGen/X86/Ppr.hs | 47 +++++++++++++++++++++++++++++++------------
1 file changed, 34 insertions(+), 13 deletions(-)
diff --git a/compiler/nativeGen/X86/Ppr.hs b/compiler/nativeGen/X86/Ppr.hs
index f5011b2..a295a47 100644
--- a/compiler/nativeGen/X86/Ppr.hs
+++ b/compiler/nativeGen/X86/Ppr.hs
@@ -175,23 +175,44 @@ pprLabel lbl = pprGloblDecl lbl
$$ pprTypeAndSizeDecl lbl
$$ (ppr lbl <> char ':')
+{-
+Note [Pretty print ASCII when AsmCodeGen]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Previously, when generating assembly code, we created SDoc with
+`(ptext . sLit)` for every bytes in literal bytestring, then
+combine them using `hcat`.
+
+When handling literal bytestrings with millions of bytes,
+millions of SDoc would be created and to combine, leading to
+high memory usage.
+
+Now we escape the given bytestring to string directly and construct
+SDoc only once. This improvement could dramatically decrease the
+memory allocation from 4.7GB to 1.3GB when embedding a 3MB literal
+string in source code. See Trac #14741 for profiling results.
+-}
pprASCII :: [Word8] -> SDoc
pprASCII str
- = hcat (map (do1 . fromIntegral) str)
+ -- Transform this given literal bytestring to escaped string and construct
+ -- the literal SDoc directly.
+ -- See Trac #14741
+ -- and Note [Pretty print ASCII when AsmCodeGen]
+ = ptext $ sLit $ foldr (\w s -> (do1 . fromIntegral) w ++ s) "" str
where
- do1 :: Int -> SDoc
- do1 w | '\t' <- chr w = ptext (sLit "\\t")
- do1 w | '\n' <- chr w = ptext (sLit "\\n")
- do1 w | '"' <- chr w = ptext (sLit "\\\"")
- do1 w | '\\' <- chr w = ptext (sLit "\\\\")
- do1 w | isPrint (chr w) = char (chr w)
- do1 w | otherwise = char '\\' <> octal w
-
- octal :: Int -> SDoc
- octal w = int ((w `div` 64) `mod` 8)
- <> int ((w `div` 8) `mod` 8)
- <> int (w `mod` 8)
+ do1 :: Int -> String
+ do1 w | '\t' <- chr w = "\\t"
+ | '\n' <- chr w = "\\n"
+ | '"' <- chr w = "\\\""
+ | '\\' <- chr w = "\\\\"
+ | isPrint (chr w) = [chr w]
+ | otherwise = '\\' : octal w
+
+ octal :: Int -> String
+ octal w = [ chr (ord '0' + (w `div` 64) `mod` 8)
+ , chr (ord '0' + (w `div` 8) `mod` 8)
+ , chr (ord '0' + w `mod` 8)
+ ]
pprAlign :: Int -> SDoc
pprAlign bytes
More information about the ghc-commits
mailing list