Skip to content
LogoLogo

Bytecode size and gas

Lithe's restrictions make its programs easier to compile, as well as easier to review. Across the benchmark suite, Lithe's runtime bytecode is about 40% smaller than the best of five optimized solc builds of the equivalent Solidity. Every one of the 51 contracts comes in smaller, most by 25–55%.

Runtime bytecode

Runtime bytecode size in bytes. Smaller is better. solc is 0.8.36, and each column is one optimizer configuration. The solar columns build the same Solidity with Solar, a new Solidity compiler written in Rust, version 0.3.0 (commit e7a91fc7), optimizing for gas and for size:

contractlithesolc ir r1solc ir r200solc ir r10ksolc ir r1Msolc r200solar gassolar sizelithe vs best solclithe vs best solar
base6483713011322165516551170909826-28.5%+1.3%
book74312471251176617661585915867-40.4%-14.3%
codec18792657265230493619311728692321-29.1%-19.0%
collections61210851098138017751392758654-43.6%-6.4%
counter205331336489489408236205-38.1%+0.0%
date_time10011225127224922792186010721011-18.3%-1.0%
deployers11821916190326172917287714661303-37.9%-9.3%
dialer10911803184123152495222913161098-39.5%-0.6%
env821281282132131378280-35.9%+2.5%
erc115543606610678893159975707246353974-34.0%+9.7%
erc2018263090313544395159305623981981-40.2%-7.8%
erc20_full6157105121074713612160211022674306213-39.8%-0.9%
erc462627383683373547525352440823452342-25.7%+16.9%
erc690929144558465356586565458727652609-36.1%+11.7%
erc72125634759483771888438505531802698-46.1%-5.0%
erc721_full4588861187391186914090842162655154-45.5%-11.0%
exchange404614615822822850441385-34.2%+4.9%
executor67114391440202620261798923885-53.4%-24.2%
fixed_point28653526355342854740368330252849-18.7%+0.6%
flash_borrower52910131056144614461225687687-47.8%-23.0%
layout67410931094130816681447732714-38.3%-5.6%
lists13772357235227853175269823601817-41.5%-24.2%
logs239369371565565529246245-35.2%-2.4%
loops90313921401171720171759997792-35.1%+14.0%
math4286726728099891063434420-36.3%+1.9%
names11112743280530113708286920751655-59.5%-32.9%
numerics17153176318541954915376422522013-46.0%-14.8%
overloads4327157238491089906438423-39.6%+2.1%
ownable_roles93412851296169521151455891814-27.3%+14.7%
profile3951135116215151515866617614-54.4%-35.7%
reasons7811312131218842064180811861089-40.5%-28.3%
receiver38069173710941094885516562-45.0%-26.4%
receiver11559031611177023722372180612431098-43.9%-17.8%
records13033398345544895407412720661975-61.7%-34.0%
registry8592190224025333200169910921064-49.4%-19.3%
relay17323081314734944040280021881806-38.1%-4.1%
safe_erc20527655655955955757437416-19.5%+26.7%
selectors34162062212141214769427390-45.0%-12.6%
shapes10771790180024742684222714861387-39.8%-22.4%
solady_erc115525513429347437654365456830762811-25.6%-9.2%
solady_erc2014312083210923882808231715831507-31.3%-5.0%
solady_erc72117602717274534654039313821041987-35.2%-11.4%
staking19933256326341184838335223712141-38.8%-6.9%
strings10122354238928903254236214941454-57.0%-30.4%
tally401599641823823618509399-33.1%+0.5%
texts35566477663370928666643155564582-44.7%-22.4%
token357566571834834765355353-36.9%+1.1%
traits464647651979979905560498-28.3%-6.8%
uniswap_v2_factory56119707978712359137471012057585330-42.2%+5.3%
uniswap_v2_pair469276807713963410782830046034248-38.9%+10.5%
vault379618620951951856391390-38.7%-2.8%
total775651305261324961696441932271431229376083136-40.6%-6.7%

The builds are --via-ir with 1, 200, 10,000, and 1,000,000 optimizer runs, and the legacy pipeline with 200 runs. The high run counts trade size for gas, so they set the bar for the gas comparison below. All are compiled --no-cbor-metadata, so metadata doesn't count against Solidity. Solar appends no metadata, and its builds are -Ogas and -Osize.

Solar is the closer comparison. Lithe is about 7% smaller than Solar's size build across the suite, and smaller on 34 of the 51 contracts. It is about 17% smaller than Solar's gas build. Lithe weighs bytes against gas, so it gives up some bytes to save gas on code that runs often.

Smaller code means cheaper deployment, and more room under the EIP-170 contract size limit of 24,576 bytes. A contract that still doesn't fit can run modules from shared facets. Deployment gas falls with it: about 25% across the suite, 34% for the ERC-20, 41% for the ERC-721, and 41% for the Uniswap v2 factory. Each contract is set up as it deploys, by a deploy in Lithe and a constructor in Solidity, with the same arguments.

Gas per transaction

Storage reads and writes dominate the cost of most transactions, and both compilers touch the same slots, so what separates them is everything else a transaction runs: dispatch, argument decoding, arithmetic, loops, memory, hashing, and encoding. On that part Lithe spends about a third less gas than the best solc build of each contract, and about 6% less in total execution gas, or 2.5% leaving out the pairs the Uniswap factory deploys. Of the 1,577 transactions and calls in the suite, Lithe costs less than every solc build on 1,289, the same on 196, and more on 92, all but one of those by under 2%. Solar is about even with Lithe: its gas build spends 0.2% more in total, and its size build 0.6% more. Against the cheaper of Solar's two builds, Lithe costs less on 722 transactions, the same on 247, and more on 608, 582 of them by under 1%. The compiler weighs each choice between smaller and faster code by an estimate of how often the code runs, so a hot path can spend a few bytes to save gas on every call.

Some transactions still cost more than their Solidity versions. A transition that defers an external call runs its storage writes first, so when the call then reverts, those writes were paid for. A Solidity contract that makes the call before writing reverts sooner.

Compile time

Milliseconds to compile each contract, start to finish, one process per contract. Smaller is better. lithe compiles into an empty build cache, and lithe cached reads the cache back:

contractlithelithe cachedsolc ir r200solc r200solar gassolar size
base648655251010
book9785371313
codec147137871922
collections107106441010
counter87231266
date_time108853799
deployers108115431212
dialer97103721314
env8716855
erc115526104111783030
erc20107235731717
erc20_full26166862223538
erc4626169273982122
erc69091276332081817
erc7211393531172323
erc721_full17105671753133
exchange97431976
executor98853199
fixed_point1410156491616
flash_borrower97673399
layout97823188
lists107141691817
logs87331766
loops10786321010
math87443077
names107167761716
numerics129187731818
overloads97492077
ownable_roles108752598
profile9793491110
reasons9772301010
receiver87512998
receiver11559786441413
records108175591314
registry97128631312
relay128172831819
safe_erc2098491788
selectors87402177
shapes107101481112
solady_erc1155158168842324
solady_erc2010891421010
solady_erc721118130571516
staking128219591616
strings107110511212
tally76291666
texts1883431693230
token98331566
traits87452477
uniswap_v2_factory2996451502323
uniswap_v2_pair22105111362323
vault98411676
total58740384283201704710

Lithe compiles the whole suite in about 0.6 seconds. The legacy solc pipeline takes about 3.2 seconds, five times as long, and --via-ir about 8.4 seconds, fourteen times as long. The four --via-ir builds are within 1% of each other, so the table shows one. Lithe is faster than every solc build on all 51 contracts.

Solar is the closer comparison here too. It takes about 0.7 seconds for the suite with either setting, a fifth longer than Lithe. Lithe is faster than both Solar builds on 31 contracts and slower than both on 17. The smallest contracts take 5 to 10 milliseconds in every compiler, most of it starting the process.

Each figure is the median of ten runs, on an Apple M4 Max. bench/run.py prints the same table from a single run.

The comparison is like for like

Most of the benchmark contracts are real programs, not microbenchmarks:

  • erc20: OpenZeppelin's wizard token, burnable, pausable, and ownable, on the upgradeable contracts deployed without a proxy
  • erc20_full: the wizard token with every common add-on: roles for minting and pausing, a cap, permits, votes with delegation and a checkpointed history, and flash loans through a borrower contract
  • erc721: OpenZeppelin's wizard NFT with the same extensions, plus safeTransferFrom against a receiver contract
  • erc721_full: the wizard NFT with every common add-on: enumeration, URI storage with a base URI, burning, pausing, auto-increment minting, and ERC-2981 royalties
  • erc1155: OpenZeppelin's wizard multi-token, burnable, pausable, and ownable, with supply tracking and an updatable URI
  • erc4626: OpenZeppelin's ERC4626 vault
  • erc6909: OpenZeppelin's ERC6909 with its token supply, metadata, and content URI extensions, built from the standard library's modules, whose storage sits at their own ERC-7201 roots, so its slots are left out like Solady's
  • staking: Synthetix's StakingRewards on OpenZeppelin's Ownable and Initializable
  • uniswap_v2_factory and uniswap_v2_pair: Uniswap v2-core, ported to Solidity 0.8, with the factory creating each pair with CREATE2. A pair lands at a different address in each build, so the comparison names it by role, and swaps both ways, liquidity, the protocol fee, and a signed permit replay on each
  • fixed_point, date_time, and base64: Solady's FixedPointMathLib, DateTimeLib, and Base64, next to Lithe's standard library
  • deployers and safe_erc20: OpenZeppelin's Clones and SafeERC20, and Solady's SSTORE2 and CREATE3
  • executor and exchange: calls whose results feed continuations
  • records and layout: packed records, fixed lists, and tuples in storage
  • solady_erc20, solady_erc721, and solady_erc1155: Solady's hand-written assembly tokens, with EIP-2612 permits and the Permit2 allowance on the ERC-20, and safe transfers against receiver contracts on the others, next to plain Lithe with the same errors, events, and order of checks

The rest each exercise one part of the language: strings, lists, records, logs, loops, ABI encoding, low-level calls, and errors.

Each Solidity version uses OpenZeppelin where the original does. The benchmark deploys every build of every contract to a local chain and replays the same transactions against each one. It then checks that statuses, return data, logs, and every storage slot agree across all eight builds. Lithe lays out storage exactly as Solidity does, so the raw slots have to match byte for byte. Solady's tokens are the exception: they put each value at a hashed slot of their own, which no Solidity layout produces, so their slots are left out and everything else is still compared.

Reproducing

The benchmark needs Foundry (anvil and cast) and solc. Set SOLC to pick a specific binary. The Solar builds run when solar is on the path, or when SOLAR names the binary. From the repository root:

cargo build --release
python3 bench/run.py            # every contract
python3 bench/run.py erc20      # one contract

The Lithe and Solidity sources are side by side in bench/: erc20.lithe next to erc20.sol, and so on. A contract answers ERC-165 only where it implements it, in both languages: erc721, erc1155, erc6909, and erc20_full list their standards in supports, and erc721_full writes its own supportsInterface to add ERC-4906.

These numbers come from the compiler version this site was built with. Rerun the benchmark for current figures.