Skip to content

Commit d26be3e

Browse files
authored
Flat: encode UPLC arrays as lists (#7296)
* Use list encoding for flat serialisation of arrays * Add changelog entry
1 parent 7c192ce commit d26be3e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Changed
2+
3+
- The Flat serialisation format for UPLC built-in arrays was changed to use the encoder for lists instead of the encoder for the lazy `Vector` type.
4+

plutus-core/plutus-core/src/Data/Vector/Orphans.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import Flat.Instances.Vector ()
1010
instance (Hashable a) => Hashable (Strict.Vector a) where
1111
hashWithSalt = Strict.foldl' hashWithSalt
1212

13+
{- The `flat` library does not provide a `Flat` instance for
14+
`Vector.Strict.Vector`. We could encode and decode strict vectors by converting
15+
them to and from lazy vectors (for which there is a `Flat` instance), but
16+
experiments show that deserialisation is actually faster by about 5-10% if we
17+
encode vectors as lists. This incurs a slight size penalty (lists require one
18+
bit of overhead per entry whereas vectors can be encoded with an overhead of one
19+
byte per 255 elements), but this is offset by the decoding speedup. Encoding
20+
vectors as lists also simplifies maintenance and specification. -}
1321
instance (Flat a) => Flat (Strict.Vector a) where
14-
size = size . Strict.toLazy -- Strict to Lazy is O(1)
15-
encode = encode . Strict.toLazy
16-
decode = Strict.fromLazy <$> decode -- Strict from Lazy is O(1)
22+
size = size . Strict.toList
23+
encode = encode . Strict.toList
24+
decode = Strict.fromList <$> decode

0 commit comments

Comments
 (0)