Sundae v3 Performance Comparisons

Sundae Labs
4 min readFeb 19, 2024

Recently, we released a detailed white paper describing all of the planned features for the upcoming Sundae v3 protocol. One of the main advancements of these contracts is dramatic performance improvements over the Sundae v1 protocol, and we thought it would be worthwhile to explore those differences, and where they come from.

What?

The SundaeSwap v1 protocol consists of a set of contracts, written in PlutusTx, utilizing Plutus v1 semantics, and released in late January of 2022. At absolute peak performance with the current protocol parameters, they can process 8 orders in a batch.

However, there is a small quirk in the Cardano ledger rules that went unnoticed at the time of our launch: the ledger will sort your transaction inputs before running your smart contract. This means that, in order to preserve the “first come, first serve” nature of the contracts, typical throughput is actually 2–4 orders per batch. This means that, under ideal conditions, SundaeSwap v1 might do around 51,840 orders per day (average of 3 orders per batch, 4 batches per block, 3 blocks per minute, and 1440 minutes per day). In practice, because we share the Cardano network with other protocols, it is much lower. Cardano’s peak swap throughput across all current DEX’s has been measured at between 20,000 and 25,000 orders per day.

By comparison, SundaeSwap v3 can fit between 25 and 35 orders per batch under typical working conditions, and 6 batches within a block. Using the same “ideal condition” vanity metric as above, SundaeSwap v3 can do around 518,400 orders per day, a literal 10x increase. Typical expected throughput when sharing the network with other protocols is yet to be measured, though would likely be similarly impressive.

You can see a visualization we put together outlining this difference here. Note that at the time of this visualization, our contracts were capped at 25 orders per batch, so the difference would be even more drastic now.

During our recent testnet launch, we did a soft load test (a so called “soak” test). We did this to measure the performance of the system under continual sustained load, while also accomodating real user traffic if anyone wanted to try out the protocol.

Even with the caps in place for this test, we were able to execute over 100,000 orders in just over 2 days. You can see an example of one of these large batches with 25 orders here, and one with 34 orders here.

How?

So how did we achieve this amazing performance increase? Let’s dive into it, but beware, what follows is very heavy on the technical details.

The first improvement comes from utilizing Plutus v2 reference scripts and inline datums. This saves both on block space, a dominant factor in transaction fees, and allows the Sundae protocol to be a better neighbor with other protocols. Some early tests with a Plutus v2 capable version of the Sundae v1 protocol showed that this was enough to bring us from a maximum of 8 orders to a maximum of 10 orders.

The second improvement comes from switching to Aiken. PlutusTx implements an optimal “scott-encoding” for working with data internally, while Aiken just works with the data directly. This means PlutusTx pays an upfront cost to perform this translation, but in theory performs asymptotically better.

However, Aiken brings the realization that, at the execution unit sizes we have on Cardano, we will essentially never reach transactions large enough where the savings are big enough to offset the initial cost of PlutusTx. Therefore, while in theory PlutusTx is more efficient, Aiken achieves huge real world savings. Our first draft of the Sundae v3 protocol written in Aiken, before any optimizations, was able to process 14 orders in a batch. What’s more, that was 14 orders processed in the correct order, so that’s more appropriately compared to the 2–4 order sizes we mentioned earlier.

Next, we realized that the majority of time was spent in re-ordering the transaction inputs to account for this ledger issue. This is because UPLC has no “array indexing”, and so to reach the ith element of the list, you have to start over from the beginning of the list of inputs each time.

Our whitepaper outlines some very clever optimizations we came up with to save as much work as possible on these. We didn’t benchmark these improvements in isolation, but they likely account for a 5–10 increase in the order sizes we can process.

After that, it’s just a series of clever micro-optimizations, shaving off as many memory units as possible. In particular, it’s worth understanding that a structure is just a list of values, so accessing a field multiple times wastes a lot of execution units. Similarly, smart contracts are almost always bottlenecked on memory, as opposed to CPU, so reducing the number of times you instantiate a new variable (by hoisting things out of a recursion, doing less list traversals, etc.) can result in big savings.

What Else?

Performance improvements aren’t all that the new contracts bring. Our white paper outlines many new features targeted specifically towards the real world pain points and unsatisfied niches we see, but here are a few highlights:

  • Protocol Owned Swaps: Orders on SundaeSwap can be “owned”, not just by a public key, but also by a multi-signature scheme, or even a script. This means that another protocol can place a swap order from treasury funds, and enforce that the results (or the principal, if the order is cancelled) are paid back into the protocol treasury, while never trusting a human operator.
  • Amortized Fees: Fees are dynamic, and partially shared among all orders in a batch. During periods of high volume, users receive a portion of their fees back as a rebate.
  • Strategies: implement highly reactive trading and liquidity strategies, in the programming language of your choice!

Conclusion

We poured an incredible amount of effort into optimizing these contracts to prepare Cardano for whatever the future market demands may bring, and we think we’ve certainly pushed the envelope in that regard. Very soon, these contracts will be open source, meaning the broader Cardano community can learn from and benefit from some of the clever ideas we came up with!

--

--

Sundae Labs

Building sweeter blockchain infrastructure. One scoop at a time. Website: https://sundae.fi/