Skip to content

Optimization principles

Finn Bear edited this page Oct 8, 2024 · 30 revisions

Introduction

This page is intended for bitcode users (version 0.6 and above), and does not concern how bitcode itself is internally optimized.

Tuple vs. Array

If you have multiple values of the same type, and plan to compress the encoded data, decide how semantically similar the values are. If they are similar, use an array. For example, to store two prices, use prices: [u32; 2]. This reduces overhead and improves compression in case of duplicates. If they are different, use a tuple or struct, to allow them to be compressed separately. For example, to store a price and a quantity, use (u32, u32) or struct Item { price: u32, quantity: u32 }.

u128 and i128

Be aware that u128 and i128 generally result in poor codegen, so avoid converting things that happen to be 16 bytes into them for encoding purposes. You can use [u8; 16] instead.

Overhead

bitcode has relatively high CPU overhead compared to other formats. If you only encode a single struct, you may be disappointed by the performance. However, as the number of things being encoded grows, bitcode may become the faster option.

Clone this wiki locally