Context
I'm creating scripts with hardcoded parameters in my smartcontract and I would like to reduce footprint of them to reduce transaction fees. Right now i'm doing so using parameterized smartcontracts. I can't provide source but I read or heard somewhere that using parameterized smartcontracts use up a lot of ExMemory. When building plutus scripts i'm using evaluateScriptCounting
which after compilation shows me ExCPU and ExMemory of my script. So far I have found the only way to hardcode parameters without using parameterized smartcontracts with BuiltinByteString
s is doing something like this:
{-# INLINABLE flattenBuiltinByteString #-}
flattenBuiltinByteString :: [BuiltinByteString] -> BuiltinByteString
flattenBuiltinByteString [] = emptyByteString
flattenBuiltinByteString (x:xs) = appendByteString x (flattenBuiltinByteString xs)
{-# INLINABLE getPkh #-}
getPkh :: PubKeyHash
getPkh = PubKeyHash { getPubKeyHash = flattenBuiltinByteString [ consByteString x emptyByteString |x <- [128,164,244,91,86,184,141,17,57,218,35,188,76,60,117,236,109,50,148,60,8,127,37,11,134,25,60,167]]}
I was expecting that this way I would get lower ExCPU and ExMemory units. But I did some tests and to my surprise my smartcontract without parameterized parameters was bigger!
Tests
- ExCPU -> 8366313, ExMemory -> 28200. Plain always succeeds.
- ExCPU -> 8366313, ExMemory -> 28200. BuiltinByteString, no checks, no datum.
- ExCPU -> 9080865, ExMemory -> 30600. Parameterized no checks, no datum.
- ExCPU -> 9438141, ExMemory -> 31800. Parameterized no checks, added typed datum with 4 fields.
- ExCPU -> 10152693, ExMemory -> 34200. Parameterized 4 checks against typed datum.
- ExCPU -> 11135202, ExMemory -> 37500. BuiltinByteString 4 checks against typed datum.
Questions
- My
flattenBuiltinByteString
is doing the flatteningfoldr
style which is ineffective and I suppose is why i'm having such big jump in ExMemory units. I was looking forfoldl'
in Plutus library but couldn't find anything. Does Plutus have something simillar toseq
so that I could make my ownfoldl'
implementation? - Are there other more effective ways to flatten
BuiltinByteString
list on-chain? - What over tricks I could do besides using
BuiltinData
types instead of my own custom typed data types to optimize my smartcontracts? - Should I even be bothering with optimizations like this, whats the upper ExCPU and ExMemory limit?