When seeing the phrase “dynamic programming” in an algorithms class or leetcode study guide, the first question people ask is “what does ‘dynamic’ mean in this context?”. The key question is instead “what does ‘programming’ mean in this context?”, because it does not mean “computer programming”. Instead it refers to, as the Oxford English Dictionary … Continue reading "The key to understanding…
Once upon a time I worked in the field of Java Optimizations. The target system was a distributed data processing platform that ran across hundreds of thousands of machines. At such scales, a 0.5% improvement would easily make up my salary going forward, and 2% was a good result for the half. That doesn’t mean it was … Continue reading "That time I wasted weeks hand optimizing assembly because I…
tl;dr: My hobby project FiniteCurve.com is a web app that draws an image with a single, long, non-intersecting line. This post explains the process. Like many engineers, I’ve had a long standing fascination with mazes. One manifestation of this was elementary school drawings of ever-winding but never overlapping lines, like this: They were fascinating to look at but tedious to draw. When I learned…
Someone was partitioning an ancient laptop’s spinning rust, and asked where they should put their swap. At the start of the drive? At the end? As a swap file? Does it even matter? While I’m on my 4th generation SSD at this point, I was interested enough to benchmark and find out. I would have used a swap file thinking it wouldn’t matter much, but boy was I wrong! Test setup My only remaining…
In shell scripting you sometimes come across comparisons where each value is prefixed with "x". Here are some examples from GitHub: if [ "x${JAVA}" = "x" ]; then if [ "x${server_ip}" = "xlocalhost" ]; then if test x$1 = 'x--help' ; then I’ll call this the x-hack. For any POSIX compliant shell, the value of … Continue reading "What exactly was the point of [ “x$var” = “xval” ]?"
Reddit user mathisweirdaf posted this interesting observation : $ ls -lh /usr/bin/{test,[} -rwxr-xr-x 1 root root 59K Sep 5 2019 '/usr/bin/[' -rwxr-xr-x 1 root root 55K Sep 5 2019 /usr/bin/test [ and test are supposed to be aliases for each other, and yet there is a 4kiB difference between their GNU coreutils binaries. Why? First, for anyone surprised: yes, there is a /usr/bin/[ . I have a…
tl;dr: Use error-if-unset ${placeholders?} and join commands with && to make it easier and safer to copy-paste shell commands from technical documentation. I frequently read documentation that includes shell commands, and copy-paste them into a shell. It might look something like this: To install a JDK: sudo apt-get update sudo apt-get install openjdk-<VERSION>-jdk Obviously this example is…
tl;dr: With the Netpbm file formats, it’s trivial to output pixels using nothing but text based IO To show that there’s nothing up my sleeves, here’s an image: And here’s the complete, dependency free bash script that generates it: #!/bin/bash exec > my_image.ppm # All echo statements will write here echo "P3 250 250 255" # magic, width, height, max component value for ((y=0; y<250; y++)) { for…
tl;dr: We look at how Zsh and Fish is able to indicate a missing terminating linefeed in program output when the Unix programming model precludes examining the output itself. Most shells, including bash, ksh, dash, and ash, will show a prompt wherever the previous command left the cursor when it exited. The fact that the prompt (almost) always shows up on the familiar left-most column of the next…
ShellCheck v0.7.1 has just been released . It primarily has cleanups and bugfixes for existing checks, but also some new ones. The new check I personally find the most fascinating is this one, for an issue I haven’t really seen discussed anywhere before: In demo line 6: cat template/header.txt "$f" > archive/$((i++)).txt ^ SC2257: Arithmetic modifications in command redirections may be discarded.…