Optimizing memory use in markdown parser 0 ▲ Krzysztof Kowalczyk blog 1 day ago · 15 min read3073 words · Tech · hide · 0 comments I’m porting gpui-component (a Rust UI component library built on GPUI) to C++ as gpui-cpp. By which I mean: my friend Claude does the porting, I’m just directing. It uses markdown-rs (a CommonMark + GFM parser) markdown parser so I ported it too. Then I optimized it. This post describes what I did with the intention of teaching other how to optimize C++ code. The starting point There are 2 kinds of markdown parser: those that stream nodes as they parse those that build an AST in memory markdown-rs builds an AST. The game is about minimizing the size of AST node. In Rust there are various kinds of nodes, the largest being 152 bytes. Claude generated a single Node struct of 232 bytes. I got it down to 16 bytes. Here’s the initial Node struct, before optimizations: Node, 232 bytes k children 24 position 24 8 string fields — 128 bytes align 24 nums 16 grey = padding and small fields · blue = growable vector · yellow = pointer+length strings Node, 16 bytes (same scale) lastKid · sibling ·… No comments yet. Log in to reply on the Fediverse. Comments will appear here.