Skip to content

Commit

Permalink
[#220] Avoid resource leaking in scanners
Browse files Browse the repository at this point in the history
Problem: Currently we use lazy `readFile`, and xrefcheck opens all
the files immediately for reading, while file descriptors are
resource that can be finite.

Solution: Replaced lazy `readFile` with strict version.
  • Loading branch information
YuriRomanowski committed Dec 11, 2022
1 parent 997c438 commit f7b8d9c
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import CMarkGFM
import Control.Lens (_Just, makeLenses, makeLensesFor, (.=))
import Control.Monad.Trans.Writer.CPS (Writer, runWriter, tell)
import Data.Aeson (FromJSON (..), genericParseJSON)
import Data.ByteString.Lazy qualified as BSL
import Data.ByteString qualified as BS
import Data.DList qualified as DList
import Data.Default (def)
import Data.Text qualified as T
import Data.Text.Lazy qualified as LT
import Fmt (Buildable (..), nameF)
import Text.HTML.TagSoup
import Text.Interpolation.Nyan
Expand Down Expand Up @@ -406,16 +405,15 @@ textToMode ("ignore" : [x])
| otherwise = InvalidMode x
textToMode _ = NotAnAnnotation

parseFileInfo :: MarkdownConfig -> FilePath -> LT.Text -> (FileInfo, [ScanError])
parseFileInfo :: MarkdownConfig -> FilePath -> T.Text -> (FileInfo, [ScanError])
parseFileInfo config fp input
= runWriter
$ flip runReaderT config
$ nodeExtractInfo fp
$ commonmarkToNode [optFootnotes] [extAutolink]
$ toStrict input
$ commonmarkToNode [optFootnotes] [extAutolink] input

markdownScanner :: MarkdownConfig -> ScanAction
markdownScanner config path = parseFileInfo config path . decodeUtf8 <$> BSL.readFile path
markdownScanner config path = parseFileInfo config path . decodeUtf8 <$> BS.readFile path

markdownSupport :: MarkdownConfig -> ([Extension], ScanAction)
markdownSupport config = ([".md"], markdownScanner config)

0 comments on commit f7b8d9c

Please sign in to comment.