Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict plutus-cbor #213

Closed
wants to merge 7 commits into from
Closed

Strict plutus-cbor #213

wants to merge 7 commits into from

Conversation

ch1bo
Copy link
Member

@ch1bo ch1bo commented Feb 9, 2022

Introducing more strictness to plutus-cbor

Could shave off about 30% on computation time on the expensive 100 asset case, but still not linear in input data! That means, that benchmarking in Haskell is still vastly different than evaluating & estimating execution costs in plutus.

Before:
image

After:
image

ch1bo added 7 commits February 9, 2022 14:46
No performance improvement, but was a potential space leak?
No noticable performance improvement, but same potential space leak as
in encodeMap
This should have subtractions be performed right away. Small improvement
on benchmark
This did not achieve a noticable improvement, but made all functions
consistent in their strictness behavior with internal functions
@ch1bo ch1bo requested a review from KtorZ February 9, 2022 14:20
@ch1bo ch1bo changed the title Ch1bo/strict plutus cbor Strict plutus-cbor Feb 9, 2022
@ch1bo
Copy link
Member Author

ch1bo commented Feb 9, 2022

I just realized that this is breaking the normal plutus compilation of our validators using these functions 😓

@@ -86,7 +87,7 @@ import PlutusTx.Builtins (subtractInteger)
newtype Encoding = Encoding (BuiltinByteString -> BuiltinByteString)

instance Semigroup Encoding where
(Encoding a) <> (Encoding b) = Encoding (a . b)
(Encoding f) <> (Encoding g) = Encoding (\(!x) -> f $ g x)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't want that one... Being too strict is also not good as it prevents some compiler optimizations. Here, I'd heavily favor the composition over strictness.. I don't think it buys us much.

@@ -115,7 +116,7 @@ encodeBool = \case
--
-- Note (2): This can only encode numbers up to @2^64 - 1@ and down to @-2^63@
encodeInteger :: Integer -> Encoding
encodeInteger n
encodeInteger !n
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed as well.

@@ -124,14 +125,15 @@ encodeInteger n

-- | Encode a 'BuiltinByteString' as a CBOR type-02 major type.
encodeByteString :: BuiltinByteString -> Encoding
encodeByteString bytes =
encodeByteString !bytes =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same 😅

@@ -161,7 +163,7 @@ encodeBreak = Encoding (consByteString 0xFF)
-- <> encodeInteger 42
-- @
encodeListLen :: Integer -> Encoding
encodeListLen = Encoding . encodeUnsigned 4
encodeListLen !n = Encoding (encodeUnsigned 4 n)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also favor Lazyness and composition here.

@@ -188,7 +190,7 @@ encodeList :: (a -> Encoding) -> [a] -> Encoding
encodeList encodeElem =
step 0 mempty
where
step n bs = \case
step !n !bs = \case
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is definitely needed!

@KtorZ
Copy link
Collaborator

KtorZ commented Feb 11, 2022

I guess we can close that one?

@KtorZ KtorZ closed this Feb 11, 2022
@ch1bo ch1bo deleted the ch1bo/strict-plutus-cbor branch August 5, 2022 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants