on master help sign in
Have Space.docComment produce a parse snippet

This will keep the memory region alive so that doc parsing can be avoided as much as possible. The ByteString lives in pinned memory, so this shouldn't have a negative impact on the frequency of garbage collection.

commit001084ece7d780ba1ab1b35db245395d22a72cee authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 3 Jun 2019, 10:31 (7 years ago) parentd1e6d9b
1 file changed+18−12
compiler/src/Parse/Space.hs+18−12
compiler/src/Parse/Space.hs+18−12
@@ -16,7 +16,8 @@ module Parse.Space
1616
1717
1818import Data.Word (Word8, Word16)
19−import Foreign.Ptr (Ptr, plusPtr)
19+import Foreign.Ptr (Ptr, plusPtr, minusPtr)
20+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
2021
2122import qualified AST.Source as Src
2223import Parse.Primitives (Row, Col)
@@ -39,14 +40,14 @@ type Parser x a =
3940
4041chomp :: (E.Space -> Row -> Col -> x) -> P.Parser x ()
4142chomp toError =
42− P.Parser $ \(P.State pos end indent row col) cok _ cerr _ ->
43+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr _ ->
4344 let
4445 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
4546 in
4647 case status of
4748 Good ->
4849 let
49− !newState = P.State newPos end indent newRow newCol
50+ !newState = P.State src newPos end indent newRow newCol
5051 in
5152 cok () newState
5253
@@ -60,7 +61,7 @@ chomp toError =
6061
6162checkIndent :: A.Position -> (Row -> Col -> x) -> P.Parser x ()
6263checkIndent (A.Position endRow endCol) toError =
63− P.Parser $ \state@(P.State _ _ indent _ col) _ eok _ eerr ->
64+ P.Parser $ \state@(P.State _ _ _ indent _ col) _ eok _ eerr ->
6465 if col > indent && col > 1
6566 then eok () state
6667 else eerr endRow endCol toError
@@ -68,7 +69,7 @@ checkIndent (A.Position endRow endCol) toError =
6869
6970checkAligned :: (Word16 -> Row -> Col -> x) -> P.Parser x ()
7071checkAligned toError =
71− P.Parser $ \state@(P.State _ _ indent row col) _ eok _ eerr ->
72+ P.Parser $ \state@(P.State _ _ _ indent row col) _ eok _ eerr ->
7273 if col == indent
7374 then eok () state
7475 else eerr row col (toError indent)
@@ -76,7 +77,7 @@ checkAligned toError =
7677
7778checkFreshLine :: (Row -> Col -> x) -> P.Parser x ()
7879checkFreshLine toError =
79− P.Parser $ \state@(P.State _ _ _ row col) _ eok _ eerr ->
80+ P.Parser $ \state@(P.State _ _ _ _ row col) _ eok _ eerr ->
8081 if col == 1
8182 then eok () state
8283 else eerr row col toError
@@ -88,7 +89,7 @@ checkFreshLine toError =
8889
8990chompAndCheckIndent :: (E.Space -> Row -> Col -> x) -> (Row -> Col -> x) -> P.Parser x ()
9091chompAndCheckIndent toSpaceError toIndentError =
91− P.Parser $ \(P.State pos end indent row col) cok _ cerr _ ->
92+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr _ ->
9293 let
9394 (# status, newPos, newRow, newCol #) = eatSpaces pos end row col
9495 in
@@ -98,7 +99,7 @@ chompAndCheckIndent toSpaceError toIndentError =
9899 then
99100
100101 let
101− !newState = P.State newPos end indent newRow newCol
102+ !newState = P.State src newPos end indent newRow newCol
102103 in
103104 cok () newState
104105
@@ -240,7 +241,7 @@ eatMultiCommentHelp pos end row col openComments =
240241
241242docComment :: (Row -> Col -> x) -> (E.Space -> Row -> Col -> x) -> P.Parser x Src.Comment
242243docComment toExpectation toSpaceError =
243− P.Parser $ \(P.State pos end indent row col) cok _ cerr eerr ->
244+ P.Parser $ \(P.State src pos end indent row col) cok _ cerr eerr ->
244245 let
245246 !pos3 = plusPtr pos 3
246247 in
@@ -250,14 +251,19 @@ docComment toExpectation toSpaceError =
250251 && P.unsafeIndex (plusPtr pos 2) == 0x7C {- | -}
251252 then
252253 let
254+ !col3 = col + 3
255+
253256 (# status, newPos, newRow, newCol #) =
254− eatMultiCommentHelp pos3 end row (col + 3) 1
257+ eatMultiCommentHelp pos3 end row col3 1
255258 in
256259 case status of
257260 MultiGood ->
258261 let
259− !comment = Src.Comment pos3 (plusPtr newPos (-2))
260− !newState = P.State newPos end indent newRow newCol
262+ !off = minusPtr pos3 (unsafeForeignPtrToPtr src)
263+ !len = minusPtr newPos pos3 - 2
264+ !snippet = P.Snippet src off len row col3
265+ !comment = Src.Comment snippet
266+ !newState = P.State src newPos end indent newRow newCol
261267 in
262268 cok comment newState
263269