# Parsing Protobuf at 2+GB/s: How I Learned To Love Tail Calls in C

DevFeed: [Parsing Protobuf at 2+GB/s: How I Learned To Love Tail Calls in C](<https://devfeed.tech/articles/parsing-protobuf-at-2-gb-s-how-i-learned-to-love-tail-calls-in-c-21135.md>)

Original publisher: [Read original article](<https://blog.reverberate.org/2021/04/21/musttail-efficient-interpreters.html>)

Author: Haberman

Published: 2021-04-21T00:00:00Z

Content type: article

Language: en

Sources: [Josh Haberman](<https://devfeed.tech/sources/josh-haberman.md>)

Topics: [Parser](<https://devfeed.tech/topics/parser.md>), [C](<https://devfeed.tech/topics/c.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [performance](<https://devfeed.tech/tags/performance.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

The article explains how guaranteed tail calls through Clang's musttail extension can improve performance in C, C++, and Objective-C. It describes applying the technique to protobuf parsing, which demonstrated throughput above 2GB/s, while noting that multiple techniques contributed to the result and that portability is the main drawback.

## Source excerpt

[Note: there have been several developments in this space since this article was published. See A Tail Calling Interpreter For Python (And Other Updates) for the latest information about this technique.] I just landed an exciting feature in the main branch of the Clang compiler. Using the [[clang::musttail]] or __attribute__((musttail)) statement attributes, you can now get guaranteed tail calls in C, C++, and Objective-C. While tail calls are usually associated with a functional programming style, I am interested in them purely for performance reasons. It turns out that in some cases we can use tail calls to get better code out of the compiler than would otherwise be possible--at least given current compiler technology--without dropping to assembly. Applying this technique to protobuf parsing has yielded amazing results: we have managed to demonstrate protobuf parsing at over 2GB/s, more than double the previous state of the art. There are multiple techniques that contributed to this speedup, so "tail calls == 2x speedup" is the wrong message to take away. But tail calls are a key part of what made that speedup possible. In this blog entry I will describe why tail calls are such a powerful technique, how we applied them to protobuf parsing, and how this technique generalizes to interpreters. I think it's likely that all of the major language interpreters written in C (Python, Ruby, PHP, Lua, etc.) could get significant performance benefits by adopting this technique. The main downside is portability: currently musttail is a nonstandard compiler extension, and while I hope it catches on it will be a while before it spreads widely enough that your system's C compiler is likely to support it. That said, at build time you can compromise some efficiency for portability if you detect that musttail is not available. Tail Call Basics A tail call is any function call that is in tail position, the final action to be performed before a function returns. When tail call optimizat