packages feed

lynceus-0.0.1.0: src/Lynceus/Record.hs

{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      :  Lynceus.Record
-- Copyright   :  © 2026–present Mark Karpov
-- License     :  BSD 3 clause
--
-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- Operations on 'Record's.
module Lynceus.Record
  ( Key (..),
    Record (..),
    Entry (..),
    emptyRecord,
    parseRecord,
    renderRecord,
    readRecord,
    writeRecord,
  )
where

import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (mapMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.IO.Utf8 qualified as T
import Data.Word (Word64)
import System.Directory (doesFileExist)
import Text.Read (readMaybe)

-- | A benchmark, by group and name.
data Key = Key Text Text
  deriving (Eq, Ord, Show)

-- | The bytes each benchmark allocated and the instructions it retired.
data Record = Record
  { -- | The compiler the record was made with, which what is allocated
    -- depends on.
    recordCompiler :: Text,
    -- | The processor the instructions were counted on, which how many
    -- there are depends on, or @none@.
    recordProcessor :: Text,
    -- | One entry for every benchmark.
    recordEntries :: Map Key Entry
  }
  deriving (Eq, Show)

-- | What one benchmark cost.
data Entry = Entry
  { -- | The bytes it allocated.
    entryAllocated :: !Word64,
    -- | The instructions it retired, where they were counted.
    entryInstructions :: !(Maybe Word64)
  }
  deriving (Eq, Show)

-- | The record of no benchmarks.
emptyRecord :: Record
emptyRecord = Record "" "none" Map.empty

-- | Read a record from what 'renderRecord' wrote.
parseRecord :: Text -> Record
parseRecord text =
  Record
    { recordCompiler = field "compiler ",
      recordProcessor = field "processor ",
      recordEntries = Map.fromList (mapMaybe entry ls)
    }
  where
    ls = T.stripEnd <$> T.lines text
    field key = mconcat (mapMaybe (T.stripPrefix key) ls)
    entry line = case T.words line of
      group : allocated : instructions : name@(_ : _)
        | Just a <- readMaybe (T.unpack allocated) ->
            Just
              ( Key group (T.unwords name),
                Entry a (readMaybe (T.unpack instructions))
              )
      _ -> Nothing

-- | Render a record, sorted by group and name so that a regeneration diff
-- shows what changed rather than what moved.
renderRecord :: Record -> Text
renderRecord record =
  T.unlines $
    [ "# The benchmark record. Run with LYNCEUS_ACCEPT=1 to overwrite.",
      "#",
      "# compiler    The compiler the benchmarks were built with, which what",
      "#             they allocate depends on.",
      "# processor   The processor the instructions were counted on, which",
      "#             how many there are depends on, or none.",
      "#",
      "# Every other line is a benchmark:",
      "#",
      "# group         The group of the benchmark.",
      "# allocated     The bytes it allocates.",
      "# instructions  The instructions it retires in user space, or - where",
      "#               they were not counted.",
      "# benchmark     The name of the benchmark.",
      "",
      "compiler " <> recordCompiler record,
      "processor " <> recordProcessor record,
      "",
      row "# group" ["allocated", "instructions"] "benchmark"
    ]
      <> fmap line (Map.toAscList (recordEntries record))
  where
    line (Key group name, Entry allocated instructions) =
      row group [shown allocated, maybe "-" shown instructions] name
    row group costs name =
      T.justifyLeft width ' ' group
        <> foldMap (T.justifyRight 14 ' ') costs
        <> "  "
        <> name
    width =
      maximum
        (8 : [T.length g + 1 | Key g _ <- Map.keys (recordEntries record)])
    shown = T.pack . show

-- | Read a record, where a missing one is empty, so that benchmarks added
-- before it is written each say they are not in it.
--
-- A missing record is not read at all, since the exception that reading
-- it throws changes what the first benchmark allocates.
readRecord :: FilePath -> IO Record
readRecord path = do
  there <- doesFileExist path
  if there
    then parseRecord <$> T.readFile path
    else pure emptyRecord

-- | Write a record.
writeRecord :: FilePath -> Record -> IO ()
writeRecord path = T.writeFile path . renderRecord