Skip to content

Commit 338d640

Browse files
sjakobimergify[bot]
authored andcommitted
Slightly optimize encoding of lists of known length (#1578)
1 parent 96ae330 commit 338d640

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

dhall/src/Dhall/Binary.hs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ encodeExpressionInternal encodeEmbed = go
697697
Encoding.encodeString "Sort"
698698

699699
a@App{} ->
700-
encodeList
700+
encodeListN
701+
(2 + length arguments)
701702
( Encoding.encodeInt 0
702703
: go function
703704
: map go arguments
@@ -777,7 +778,8 @@ encodeExpressionInternal encodeEmbed = go
777778
| null xs ->
778779
encodeList2 (Encoding.encodeInt label) _T₁
779780
| otherwise ->
780-
encodeList
781+
encodeListN
782+
(2 + length xs)
781783
( Encoding.encodeInt 4
782784
: Encoding.encodeNull
783785
: map go (Data.Foldable.toList xs)
@@ -824,7 +826,8 @@ encodeExpressionInternal encodeEmbed = go
824826
(Encoding.encodeString x)
825827

826828
Project t (Left xs) ->
827-
encodeList
829+
encodeListN
830+
(2 + Dhall.Set.size xs)
828831
( Encoding.encodeInt 10
829832
: go t
830833
: map Encoding.encodeString (Dhall.Set.toList xs)
@@ -884,7 +887,8 @@ encodeExpressionInternal encodeEmbed = go
884887
(Encoding.encodeString z)
885888

886889
TextLit (Chunks xys z) ->
887-
encodeList
890+
encodeListN
891+
(2 + 2 * length xys)
888892
( Encoding.encodeInt 18
889893
: concatMap encodePair xys ++ [ Encoding.encodeString z ]
890894
)
@@ -900,7 +904,8 @@ encodeExpressionInternal encodeEmbed = go
900904
encodeEmbed x
901905

902906
Let a₀ b₀ ->
903-
encodeList
907+
encodeListN
908+
(2 + 3 * length as)
904909
( Encoding.encodeInt 25
905910
: concatMap encodeBinding (toList as) ++ [ go b₁ ]
906911
)
@@ -966,9 +971,12 @@ encodeList4 :: Encoding -> Encoding -> Encoding -> Encoding -> Encoding
966971
encodeList4 a b c d = Encoding.encodeListLen 4 <> a <> b <> c <> d
967972
{-# INLINE encodeList4 #-}
968973

974+
encodeListN :: Int -> [ Encoding ] -> Encoding
975+
encodeListN len xs = Encoding.encodeListLen (fromIntegral len) <> mconcat xs
976+
{-# INLINE encodeListN #-}
977+
969978
encodeList :: [ Encoding ] -> Encoding
970-
encodeList xs =
971-
Encoding.encodeListLen (fromIntegral (length xs)) <> mconcat xs
979+
encodeList xs = encodeListN (length xs) xs
972980
{-# INLINE encodeList #-}
973981

974982
decodeImport :: Int -> Decoder s Import

dhall/src/Dhall/Set.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Dhall.Set (
2121
, sort
2222
, isSorted
2323
, null
24+
, size
2425
) where
2526

2627
import Prelude hiding (null)
@@ -64,7 +65,7 @@ instance Foldable Set where
6465
null = Dhall.Set.null
6566
{-# INLINABLE null #-}
6667

67-
length (Set s _) = Data.Set.size s
68+
length = Dhall.Set.size
6869
{-# INLINABLE length #-}
6970

7071
-- | Convert to an unordered @"Data.Set".`Data.Set.Set`@
@@ -136,3 +137,10 @@ True
136137
-}
137138
null :: Set a -> Bool
138139
null (Set s _) = Data.Set.null s
140+
141+
{-|
142+
>>> size (fromList [1])
143+
1
144+
-}
145+
size :: Set a -> Int
146+
size (Set s _) = Data.Set.size s

0 commit comments

Comments
 (0)