Scott Wolchokhttps://wolchok.org/Recent content on Scott WolchokHugoen-usSun, 19 Jan 2025 19:21:04 -0800Neat GitHub Commitshttps://wolchok.org/posts/neat-github-commits/Sun, 19 Jan 2025 19:21:04 -0800https://wolchok.org/posts/neat-github-commits/<p>A notable amount of code I&rsquo;ve written in my professional life has made its way onto GitHub. Here&rsquo;s a collection of some of my commits that I think are neat. (Disclaimer: I wrote these while employed at Meta, where I am still employed at the time of writing. However, this is my personal space and my employer does not necessarily endorse the things I stay in my personal spaces. In particular, I am merely pointing to and summarizing public information below.)</p>From the Vault: Unionizing for Profithttps://wolchok.org/posts/unionizing-for-profit/Sat, 18 Jan 2025 13:00:59 -0800https://wolchok.org/posts/unionizing-for-profit/<p>Back in 2021, I wrote about unions in my professional capacity (disclaimer: this is my personal space and my employer does not necessarily endorse what I say here) on the PyTorch Dev Discussions forum. Stick with (or skip) the C++ 101 &ldquo;What is a union?&rdquo; section and you&rsquo;ll be rewarded with the advanced material in the second half. <a href="https://dev-discuss.pytorch.org/t/unionizing-for-profit-how-to-exploit-the-power-of-unions-in-c/444">Please enjoy!</a></p>The Sad Truth About C++ Copy Elisionhttps://wolchok.org/posts/sad-truth-about-cxx-copy-elision/Sat, 03 Apr 2021 03:30:13 +0000https://wolchok.org/posts/sad-truth-about-cxx-copy-elision/<p><a href="https://en.wikipedia.org/wiki/Copy_elision">Copy elision</a> is a C++ compiler optimization that, as its name suggests, eliminates extra copy and move operations. It is similar to the classical <a href="https://en.wikipedia.org/wiki/Copy_propagation">copy propagation</a> optimization, but specifically performed on C++ objects that may have non-trivial copy and move constructors. In this post, I&rsquo;ll walk through an example where an obvious optimization you might expect from your compiler doesn&rsquo;t actually happen in practice.</p>Privacy Policyhttps://wolchok.org/privacy/Sat, 13 Mar 2021 20:37:17 -0800https://wolchok.org/privacy/<p>This site uses Google Analytics. You can find out more about how Google Analytics collects and processes data at <a href="https://www.google.com/policies/privacy/partners/">https://www.google.com/policies/privacy/partners/</a> .</p>How to Read ARM64 Assembly Languagehttps://wolchok.org/posts/how-to-read-arm64-assembly-language/Sun, 14 Mar 2021 01:14:59 +0000https://wolchok.org/posts/how-to-read-arm64-assembly-language/<p><a href="https://en.wikipedia.org/wiki/AArch64">ARM64</a> is a <a href="https://en.wikipedia.org/wiki/Instruction_set_architecture">computer architecture</a> that competes with the popular Intel <a href="https://en.wikipedia.org/wiki/X86-64">x86-64</a> architecture used for the CPUs in desktops, laptops, and so on. ARM64 is common in mobile phones<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>, as well as <a href="https://aws.amazon.com/ec2/graviton/">Graviton</a>-based <a href="https://aws.amazon.com/ec2/">Amazon EC2</a> instances, the <a href="https://www.raspberrypi.org/">Raspberry Pi</a> 3 and 4, and the much <a href="https://debugger.medium.com/why-is-apples-m1-chip-so-fast-3262b158cba2">ballyhooed</a> <a href="https://en.wikipedia.org/wiki/Apple_M1">Apple M1</a> chips, so knowing about it might be useful! In fact, I have almost certainly spent <em>more</em> time with ARM64 than x86-64 because of the iPhone.</p>Parameter Passing in C and C++https://wolchok.org/posts/parameter-passing/Sun, 07 Mar 2021 05:36:54 +0000https://wolchok.org/posts/parameter-passing/<p>Now that we know <a href="https://wolchok.org/posts/how-to-read-assembly-language">how to read assembly language</a>, we can talk about how parameter passing works at the machine level and its consequences for writing faster code. We will focus on x86_64, but ARM64 works in a roughly similar way.</p>How to Read Assembly Languagehttps://wolchok.org/posts/how-to-read-assembly-language/Fri, 26 Feb 2021 23:50:07 +0000https://wolchok.org/posts/how-to-read-assembly-language/<p>UPDATE: This article now has an <a href="https://wolchok.org/posts/how-to-read-arm64-assembly-language">ARM64 port</a>.</p> <p>Why, in 2021, does anyone need to learn about assembly language? First, reading assembly language is the way to know <em>exactly</em> what your program is doing. Why, <em>exactly</em>, is that C++ program 1 MiB (say) instead of 100 KiB? Is it possible to squeeze some more performance out of that function that gets called all the time?</p>Inlining and Compiler Optimizationshttps://wolchok.org/posts/inlining-and-compiler-optimizations/Sat, 23 Jan 2021 05:22:14 +0000https://wolchok.org/posts/inlining-and-compiler-optimizations/<p>Why is <a href="https://en.wikipedia.org/wiki/Inline_expansion">inlining</a> so important in C++? Clearly, it reduces function call overhead: if a function is inlined, there is no need to spend time setting up its arguments, jumping to it, creating a stack frame, and then undoing all that upon returning. More interestingly, though, inlining enables other compiler optimizations. In this article, I will show examples of <a href="https://en.wikipedia.org/wiki/Constant_folding#Constant_propagation">constant propagation</a> and <a href="https://en.wikipedia.org/wiki/Loop-invariant_code_motion">loop-invariant code motion</a> (LICM). Then, I will explain how inlining enables these optimizations to apply more widely and show an example of the consequences when that doesn&rsquo;t happen.</p>C++ Performance Trap #2: Unnecessary std::functionhttps://wolchok.org/posts/cxx-trap-2-std-function/Thu, 14 Jan 2021 23:31:07 +0000https://wolchok.org/posts/cxx-trap-2-std-function/<p><a href="https://en.cppreference.com/w/cpp/utility/functional/function"><code>std::function</code></a> does what it says on the tin: it holds any callable object. For example, you can use lambdas with or without captures. It has a couple key drawbacks:</p>C++ Performance Trap #1: Constant-size std::vectorhttps://wolchok.org/posts/cxx-trap-1-constant-size-vector/Thu, 14 Jan 2021 23:31:03 +0000https://wolchok.org/posts/cxx-trap-1-constant-size-vector/<h2 id="c-performance-traps-series-introduction">C++ Performance Traps Series Introduction</h2> <p>The C++ standard library has many classes and functions that are easy to use and relatively safe. However, in situations where performance and efficiency really matter, straightforward use of the standard library may not always be the best choice. This post is the first in a series that will catalogue some of those opportunities for improvement. It is meant to be accessible to people who are <em>not</em> C++ gurus or language lawyers.</p>How This Site Is Madehttps://wolchok.org/posts/how-this-site-is-made/Thu, 14 Jan 2021 23:31:00 +0000https://wolchok.org/posts/how-this-site-is-made/<p>TLDR: <a href="https://gohugo.io/">Hugo</a>, <a href="https://github.com/">GitHub</a>, <a href="https://www.netlify.com/">Netlify</a>.</p> <p>Putting writing on a website in 2021 is surprisingly pleasant, despite a few glitches.</p> <p>I wanted to have a plain old static site. I can write raw HTML well enough, and I knew about <a href="https://getbootstrap.com/2.3.2/">Bootstrap</a>, but that still seemed like too much work. Instead, I looked at a <a href="https://snipcart.com/blog/choose-best-static-site-generator">couple</a> <a href="https://www.staticgen.com/">overviews</a> of static site generators, and I ended up picking <a href="https://gohugo.io">Hugo</a> as the one most fitting my sensibilities. You write pages and posts in Markdown, run Hugo, and get static HTML/CSS/JS out. It has a built-in web server with hot reloading so that you can run <code>hugo server</code>, edit your site, and see it in your browser right away. The builds are really fast; I am using <a href="https://www.samsung.com/us/computing/chromebooks/under-12/chromebook-3-11-6-xe500c13-k02us/">a <del>potato</del> cheap Chromebook from 2018</a> and the site still builds in less than 100 ms.</p>Reading Materialhttps://wolchok.org/reading/Thu, 14 Jan 2021 23:30:31 +0000https://wolchok.org/reading/<p>The goal of this page is to collect interesting Internet written works.</p> <h1 id="tech-blogs">Tech blogs</h1> <p>(If none of these are new to you, then I guess you&rsquo;re just not in <a href="https://xkcd.com/1053/">today&rsquo;s lucky ten thousand</a>. Sorry!)</p>About Mehttps://wolchok.org/about/Thu, 14 Jan 2021 23:30:28 +0000https://wolchok.org/about/<p>I&rsquo;m a software engineer. I work on performance, reliability, and efficiency. I&rsquo;ve also worked on developer tooling in JavaScript, Python, and Rust. The opinions stated here are my own, not those of my employer.</p>