Skip to content

Commit 30e8304

Browse files
authored
Optimise list decoder (#7358)
1 parent 0afc97f commit 30e8304

File tree

1 file changed

+28
-8
lines changed
  • plutus-core/flat/src/PlutusCore/Flat/Decoder

1 file changed

+28
-8
lines changed

plutus-core/flat/src/PlutusCore/Flat/Decoder/Strict.hs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,35 @@ import Numeric.Natural (Natural)
5555
import PlutusCore.Flat.Data.ZigZag
5656
#include "MachDeps.h"
5757

58-
{-# INLINE decodeListWith #-}
5958
decodeListWith :: Get a -> Get [a]
60-
decodeListWith dec = go
61-
where
62-
go = do
63-
b <- dBool
64-
if b
65-
then (:) <$> dec <*> go
66-
else return []
59+
decodeListWith dec = do
60+
b <- dBool
61+
if b
62+
then Get $
63+
\end s -> do
64+
GetResult s' h <- runGet dec end s
65+
if s' /= s
66+
then
67+
let goNormal = do
68+
b <- dBool
69+
if b
70+
then (:) <$> dec <*> goNormal
71+
else pure []
72+
{-# INLINE goNormal #-}
73+
in runGet ((h:) <$> goNormal) end s'
74+
else
75+
let goZero x =
76+
let goZero'= do
77+
b <- dBool
78+
if b
79+
then (x:) <$> goZero'
80+
else pure []
81+
{-# INLINE goZero' #-}
82+
in goZero'
83+
{-# INLINE goZero #-}
84+
in runGet ((h:) <$> goZero h) end s'
85+
else pure []
86+
{-# INLINE decodeListWith #-}
6787

6888
decodeArrayWith :: Get a -> Get [a]
6989
decodeArrayWith dec = DL.toList <$> getAsL_ dec

0 commit comments

Comments
 (0)