<commit_before>{-# LANGUAGE BangPatterns #-}

module Main where

import Control.Exception
import Criterion.Main
import Network.HPACK
import qualified Network.HPACK2 as N
import Data.ByteString (ByteString)
import Foreign.Marshal.Alloc

main :: IO ()
main = do
    hdrs <- read <$> readFile "bench-hpack/headers.hs"
    hpacks <- prepare hdrs
    _ <- evaluate hpacks
    let bufsiz = 4096
    buf <- mallocBytes bufsiz
    defaultMain [
        bgroup "HPACK encoding" [
              bench "Pure" $ nfIO (enc hdrs)
            , bench "New"  $ nfIO (enc2 hdrs)
            ]
      , bgroup "HPACK decoding" [
              bench "Pure" $ nfIO (dec hpacks)
            , bench "New"  $ nfIO (dec2 hpacks buf bufsiz)
            ]
      ]

----------------------------------------------------------------
prepare :: [HeaderList] -> IO [ByteString]
prepare hdrs = do
    tbl <- newDynamicTableForEncoding defaultDynamicTableSize
    go tbl hdrs id
  where
    go _    []     b = return (b [])
    go !tbl (h:hs) b = do
        (tbl', frag) <- encodeHeader defaultEncodeStrategy tbl h
        go tbl' hs (b . (frag :))

enc :: [HeaderList] -> IO ()
enc hdrs = do
    tbl <- newDynamicTableForEncoding defaultDynamicTableSize
    go tbl hdrs
  where
    go _    []     = return ()
    go !tbl (h:hs) = do
        (tbl', !_) <- encodeHeader defaultEncodeStrategy tbl h
        go tbl' hs

dec :: [ByteString] -> IO ()
dec hpacks = do
    tbl <- newDynamicTableForDecoding defaultDynamicTableSize
    go tbl hpacks
  where
    go _    []     = return ()
    go !tbl (f:fs) = do
        (tbl', !_) <- decodeHeader tbl f
        go tbl' fs

enc2 :: [HeaderList] -> IO ()
enc2 hdrs = do
    tbl <- N.newDynamicTableForEncoding N.defaultDynamicTableSize
    go tbl hdrs
  where
    go _    []     = return ()
    go !tbl (h:hs) = do
        !_ <- N.encodeHeader N.defaultEncodeStrategy tbl h
        go tbl hs

dec2 :: [ByteString] -> N.Buffer -> N.BufferSize -> IO ()
dec2 hpacks buf siz = do
    tbl <- N.newDynamicTableForDecoding N.defaultDynamicTableSize
    go tbl hpacks
  where
    go _    []     = return ()
    go !tbl (f:fs) = do
        !_ <- N.decodeHeader tbl f buf siz
        go tbl fs<commit_msg>updating bench.<commit_after>{-# LANGUAGE BangPatterns #-}

module Main where

import Control.Exception
import Criterion.Main
import Network.HPACK
import Data.ByteString (ByteString)

main :: IO ()
main = do
    hdrs <- read <$> readFile "bench-hpack/headers.hs"
    hpacks <- prepare hdrs
    _ <- evaluate hpacks
    defaultMain [
        bgroup "HPACK encoding" [
              bench "New"  $ nfIO (enc hdrs)
            ]
      , bgroup "HPACK decoding" [
              bench "New"  $ nfIO (dec hpacks)
            ]
      ]

----------------------------------------------------------------
prepare :: [HeaderList] -> IO [ByteString]
prepare hdrs = do
    tbl <- newDynamicTableForEncoding defaultDynamicTableSize
    go tbl hdrs id
  where
    go _    []     b = return (b [])
    go !tbl (h:hs) b = do
        !frag <- encodeHeader defaultEncodeStrategy tbl h
        go tbl hs (b . (frag :))

enc :: [HeaderList] -> IO ()
enc hdrs = do
    tbl <- newDynamicTableForEncoding defaultDynamicTableSize
    go tbl hdrs
  where
    go _    []     = return ()
    go !tbl (h:hs) = do
        !_ <- encodeHeader defaultEncodeStrategy { compressionAlgo = Linear, useHuffman = True }  tbl h
        go tbl hs

dec :: [ByteString] -> IO ()
dec hpacks = do
    tbl <- newDynamicTableForDecoding defaultDynamicTableSize
    go tbl hpacks
  where
    go _    []     = return ()
    go !tbl (f:fs) = do
        !_ <- decodeHeader tbl f
        go tbl fs
{'fraction_non_alphanumeric': 0.13623903508771928, 'fraction_numerical': 0.0030153508771929823, 'mean_word_length': 1.9332797427652733, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857802', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1147, 'n_words': 349}