Skip to content
0:00 / 0:00

#std::string#fbstring#folly

CppCon 2016: Nicholas Ormrod “The strange details of std::string at Facebook"

CppCon

189K subscribers • 103 videos on Hallway • Conference channel

YouTube
  • Recommended by 4 people: 4 on Hacker News.
  • A classic. People kept recommending it from 2020 to 2026.
  • Presented by Nicholas Ormrod, Software Engineer at Facebook
  • Linked in the descriptioncppcon/cppcon2016

Advanced. Deep dive for people who know the field.

122K views10 years ago4 recommendations

Systems programmingProgramming languagesC++Performance and optimizationMemory management

Nicholas Ormrod explains how Facebook optimizes std::string with its open-source replacement fbstring, covering storage layout and memory allocation.

CppCon.org — Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/cppcon/cppcon2016 — Standard strings are slowing you down. Strings are everywhere. Changing the performance of std::string has a measurable impact on the speed of real-world C++ programs. But how can you make strings better? In this talk, we'll explore how Facebook optimizes strings, especially with our open-source std::string replacement, fbstring. We'll dive into implementation tradeoffs, especially the storage of data in the struct; examine which standard rules can and cannot be flouted, such as copy-on-write semantics; and share some of the things we've learned along the way, like how hard it is to abolish the null-terminator. War stories will be provided. — Nicholas Ormrod Software Engineer, Facebook Nicholas is a developer efficiency engineer at Facebook. If he talks too much, disable him with a well-placed nerd snipe. — Videos Filmed & Edited by Bash Films: BashFilms.com Work at Hudson River Trading (HRT): tinyurl.com/safxfctf

Why it's here

4 people recommend this

What people say. Highly recommended for its deep dive into C++ string optimization, memory layout tradeoffs, and real-world performance engineering at scale.

pjscotton Hacker News • Recommends

On “Libc++'s Implementation of std::string”

I think this is the talk you're referring to. If not, it's still definitely worth a watch: link to this video

jiggawattson Hacker News • Recommends

On “Algorithms for Modern Hardware”

The FAANGS certainly have people doing this kind of work. For example, Andrei Alexandrescu has a bunch of keynote talks at CPPCon where he talks about 1-4% speed optimisations he makes to the std library at Facebook. link I also like the talk by Nicholas Ormrod “The strange details of std::string at Facebook": link to this video

D4ckardon Hacker News • Recommends

On “Fixing C Strings”

Yes, there are really cool packing techniques. See this talk for example: link to this video I don't include the null-terminator because I use this type in my own environment where I never use null-terminated strings so there is no need for it.

def-pri-pubon Hacker News • Recommends

On “Faster Asin() Was Hiding in Plain Sight”

You'd be surprised how it actually is worth the effort, even just a 1% improvement. If you have the time, this is a great talk to listen to: link to this video For a little toy ray tracer, it is pretty measly. But for a larger corporation (with a professional project) a 4% speed improvement can mean MASSIVE cost savings. Some of these tiny improvements can also have a cascading effect. Imagining finding a +4%, a +2% somewhere else, +3% in neighboring code, and a bunch of +1%s here and there. Eventually you'll have built up something that is 15-20% faster. Down the road you'll come across those optimizations which can yield the big results too (e.g. the +25%).

u/skeetoin r/C_Programming • Mentions

On “Why is there no C conference?”

Every year you can think of a subset of CppCon as being like a C conference. There are a bunch of presentations that are entirely or mostly relevant to C, and not just in a broader sense of being relevant to *any* language. I plucked out a bunch from CppCon 2017 for you. These are *roughly* ordered from best to worst: * [Matt Godbolt “What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”] * Chandler Carruth “Going Nowhere Faster” * James McNellis “Everything You Ever Wanted to Know about DLLs” * John Regehr “Undefined Behavior in 2017 (part 1 of 2)” * John Regehr “Undefined Behavior in 2017 (part 2 of 2)” * Piotr Padlewski “Undefined Behaviour is awesome!” * [Tob…

u/Badel2in r/C_Programming • Mentions

On “Small-Size Optimization in C”

This reminded me about facebook's small string optimization, link to this video. Basically, they reuse the entire string struct as a char array for small strings.

3uclidon Hacker News • Mentions

On “Show HN: SuperString – A fast and memory-optimized string library for C++”

Side note: FB also developed a string library to replace std::string ( link ). Here's a video from explaining the motivation behind the project ( link to this video ).

GolDDrankson Hacker News • Mentions

On “Pointers Are Complicated, or What’s in a Byte?”

I'm not going to take the responsibility whether it's correct or not - here's the talk I learned it from: link to this video

jiggawattson Hacker News • Mentions

On “Analyzing multi-gigabyte JSON files locally”

You're all massively underestimating the memory requirements of a parsed data structure, which is very entertaining to watch. There's this rule-of-thumb that most programmers are wrong about performance costs by at least a couple of orders of magnitude, and it's on full display here. In most languages, dynamically allocated variable-sized objects have a minimum overhead of two pointer-sized fields: a pointer and a length. On a 64-bit platform, that's 16 bytes before you actually start having any data. Next, depending on the allocator used, the requested data size is probably rounded up, either to the next power of 2, or there may be a minimum allocation size of 1 pointer. That's "safe" on ma…

nyanpasu64on Hacker News • Mentions

On “Why Async Rust?”

Can you have the runtime run a job every second, to trim thread stack depths and return memory to the kernel (whatever was meant by link to this video ) while holding a FFI mutex (or a sharded one), and have every C function call first lock the mutex and expand the stack to the necessary depth?

SkiFire13on Hacker News • Mentions

On “The C bounded model checker: criminally underused”

At least Linux does this, and it is not a security hole because it only reuses pages with memory of the current process (as in the worst case you end up reading data from some other part of your program, but not other programs). And yes, it does happen in practice, most famously it has been mentioned in the "The strange details of std::string at Facebook" talk at CppCon 2016 link to this video

sqrt_1on Hacker News • Mentions

On “Writing into Uninitialized Buffers in Rust”

It was from C++con 2016 - Facebook take on small strings link to this video I believe it is about a page that was conditionally returned to the kernel.

sqrt_1on Hacker News • Mentions

On “Writing into Uninitialized Buffers in Rust”

I don't believe they are allocating 128 bytes, or accessing out of bounds memory. He explicitly states 128byte filename allocates 129 bytes. link to this video

u/kloetzlin r/C_Programming • Mentions

On “[Project] SmartString - A Lightweight, Fast, and Safe C Library Meant to Mimic String Functionality Present in C++”

I would like the idea of having some thing as good as std::string in C as well. Unfortunately, I think that is not possible. With your implementation I see the following problems: - Missing checks for mult overflow on malloc - the size of your struct includes the nullbyte, whereas length does not? - `s_str_length` calls `strlen`, but `s_str_size` does not? - push_back should not grow by + 1 - no reuse of `strlen` - `s_str_swap` doesn't swap size and capacity‽ See this CppCon talk for full details of a proper std::string implementation.

u/Boreddad13in r/C_Programming • Mentions

On “string implementation”

Thank you for the comment. Interesting take on the sprong operator, I like how it looks more clean, but I don't like the question that might arise of "why am I indexing into this?". I know they are the same thing, but someone new to the codebase might not understand at first glance. This is a very insightful comment. I have recently watched Nicholas Ormrod's talk on fbstring, and some points in this comment reminded me of this talk (if anyone reading this hasn't seen it, it's an incredible talk, highly recommend). In terms of optimization, do you recommend using some sort of bucket system for small strings (ie, have buckets of stack allocated strings of specific lengths, and that tell…