Skip to content

Commit b843cae

Browse files
authored
Improve encoding/decoding speed (#1500)
... by not going through a `Term` intermediate This gives a ~28% performance in decoding improvement, which means that cache looks are not faster. Here are the new decoding benchmarks before and after this change: Before: ``` benchmarked Issue #108/Binary time 266.5 μs (265.7 μs .. 267.4 μs) 1.000 R² (1.000 R² .. 1.000 R²) mean 266.3 μs (265.6 μs .. 267.1 μs) std dev 2.418 μs (1.891 μs .. 3.436 μs) benchmarking Kubernetes/Binary ... took 36.94 s, total 56 iterations benchmarked Kubernetes/Binary time 641.3 ms (623.0 ms .. 655.4 ms) 0.999 R² (0.997 R² .. 1.000 R²) mean 679.7 ms (665.5 ms .. 702.6 ms) std dev 29.48 ms (14.15 ms .. 39.05 ms) ``` After: ``` benchmarked Issue #108/Binary time 282.2 μs (279.6 μs .. 284.7 μs) 1.000 R² (0.999 R² .. 1.000 R²) mean 281.9 μs (280.7 μs .. 287.7 μs) std dev 7.089 μs (2.550 μs .. 15.44 μs) variance introduced by outliers: 11% (moderately inflated) benchmarking Kubernetes/Binary ... took 27.57 s, total 56 iterations benchmarked Kubernetes/Binary time 499.1 ms (488.1 ms .. 506.6 ms) 0.999 R² (0.998 R² .. 1.000 R²) mean 498.9 ms (494.4 ms .. 503.9 ms) std dev 8.539 ms (6.236 ms .. 12.56 ms) ``` There's a slight performance regression for the decoding microbenchmark, but in practice my testing on real examples matches performance improvements seen in the larger benchmark based on an example cache product from `dhall-kubernetes`. Note that is a breaking change because: * There is no longer a `FromTerm` nor `ToTerm` class. Now we use the `Serialise` class and `{encode,decode}Expression` now work on `ByteString`s instead of `Term`s * I further narrowed the types of several encoding/decoding utilites to expect a `Void` for the first type parameter of `Expr` * This is a regression with respect to stripping 55799 CBOR tags, mainly because properly handling the tags at every possible point in the syntax tree would considerably complicate the code
1 parent 5ceb8d9 commit b843cae

File tree

13 files changed

+1174
-934
lines changed

13 files changed

+1174
-934
lines changed

dhall-lsp-server/src/Dhall/LSP/Backend/Dhall.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,5 @@ normalize (WellTyped expr) = Normal $ Dhall.normalize expr
165165
-- Dhall's hash annotations (prefixed by "sha256:" and base-64 encoded).
166166
hashNormalToCode :: Normal -> Text
167167
hashNormalToCode (Normal expr) =
168-
Dhall.hashExpressionToCode alphaNormal
168+
Dhall.hashExpressionToCode (Dhall.denote alphaNormal)
169169
where alphaNormal = Dhall.alphaNormalize expr
4.84 MB
Binary file not shown.

dhall/benchmark/parser/Main.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Main where
66
import Control.Monad (forM)
77
import Data.Map (Map, foldrWithKey, singleton, unions)
88
import Data.Monoid ((<>))
9-
import Gauge (defaultMain, bgroup, bench, whnf, nfIO)
9+
import Data.Void (Void)
10+
import Gauge (defaultMain, bgroup, bench, nf, whnf, nfIO)
1011

1112
import System.Directory
1213

@@ -67,27 +68,25 @@ benchExprFromText name expr =
6768

6869
benchExprFromBytes
6970
:: String -> Data.ByteString.Lazy.ByteString -> Gauge.Benchmark
70-
benchExprFromBytes name bytes = bench name (whnf f bytes)
71+
benchExprFromBytes name bytes = bench name (nf f bytes)
7172
where
7273
f bytes = do
73-
term <- case Codec.Serialise.deserialiseOrFail bytes of
74-
Left _ -> Nothing
75-
Right term -> return term
76-
case Dhall.Binary.decodeExpression term
77-
:: Either Dhall.Binary.DecodingFailure (Dhall.Expr () Dhall.Import) of
78-
Left _ -> Nothing
79-
Right expression -> return expression
74+
case Dhall.Binary.decodeExpression bytes of
75+
Left exception -> error (show exception)
76+
Right expression -> expression :: Dhall.Expr Void Dhall.Import
8077

8178
main :: IO ()
8279
main = do
8380
prelude <- loadPreludeFiles
8481
issue108Text <- TIO.readFile "benchmark/examples/issue108.dhall"
8582
issue108Bytes <- Data.ByteString.Lazy.readFile "benchmark/examples/issue108.dhall.bin"
83+
kubernetesExample <- Data.ByteString.Lazy.readFile "benchmark/examples/kubernetes.dhall.bin"
8684
defaultMain
8785
[ bgroup "Issue #108"
8886
[ benchExprFromText "Text" issue108Text
8987
, benchExprFromBytes "Binary" issue108Bytes
9088
]
89+
, benchExprFromBytes "Kubernetes/Binary" kubernetesExample
9190
, benchExprFromText "Long variable names" (T.replicate 1000000 "x")
9291
, benchExprFromText "Large number of function arguments" (T.replicate 10000 "x ")
9392
, benchExprFromText "Long double-quoted strings" ("\"" <> T.replicate 1000000 "x" <> "\"")

0 commit comments

Comments
 (0)