HNHacker News
TopNewBestAskShowJobs

OskarS

8,399 karma · joined September 4, 2016

submissionscomments
OskarS··on I wrote the least-C C program I could
One other thing that would be great that sometimes people use the preprocessor for is having the names variables/enums as runtime strings. Like, if you have an enum and a function to get the string representation for debug purposes (i.e. the name of the enum as represented inside the source code):

    typedef enum { ONE, TWO, THREE } my_enum;

    const char* getEnumName(my_enum val);
you can use various preprocessor tricks to implement getEnumName such that you don't have to change it when adding more cases to the enum. This would be much better implemented with some compiler intrinsic/operator like `nameof(val)` that returned a string. C# does something similar with its `nameof`.
OskarS··on I wrote the least-C C program I could
This is obviously terrible, but my favorite part of this whole thing is the fact that he included the parentheses in the IF/THEN macros, so you didn't have to do it for the condition in the if statement. So, like, this line doesn't need parentheses around the condition:

    IF (n=to-from)<=1 THEN return FI
All modern languages that try and do a refresh on the C style (Rust and Swift notably) do this, it's clearly the right idea. They should just update the C and C++ syntax to make those parentheses optional at this point.

(PS. some people would also recoil at the assignment-as-expression used in that line, but that's just good clean fun!)

OskarS··on Can Slack-mania be cured with systemized discipline?
The problem here isn’t Slack. Clearly the participants prefer discussing things like this in Slack over doing it in issues or Jira or whatever (which I understand, it’s a much nicer medium for discussion, IMHO), otherwise they would already be doing that. Your suggestion is essentially “just subvert the IT/legal policies by discussing it elsewhere, in a less efficient way”.

Clearly the problem here is the policy.

OskarS··on Heart-disease risk soars after Covid, even with a mild case
My brother passed away at 35 from an undiagnosed heart condition. He was in incredible physical shape and had run a half-marathon a few weeks earlier. He had never smoked, he drank moderately and he was a vegetarian. He had no history of any kind of heart problems (or any serious medical problems, in fact), but one day he felt dizzy and had trouble staying upright, so he took a sick day from work, took a nap and never woke up. This was in 2017.

It's certainly rare, but young fit people do have heart problems. You can't conclude that this had anything to do with the vaccine, this is not how science works. Certainly the long-term health effects of the vaccines should be studied, but bandying about these kinds of anecdotes is dangerous: it's the same kind of thing that has (wrongly!) convinced so many people that vaccines cause autism.

By the way, this is not to minimize what you went through, I cannot imagine how scary or painful it must've been. I hope you will have a strong path to recovery, and I wish you all the best.

OskarS··on MadMax: Analyzing the Out-of-Gas World of Smart Contracts (2020)
Isn’t the computational cost of running the smart contracts absolutely miniscule in comparison to actually doing the proof-of-work computation? Or is the proof-of-work part of the smart contract in some way?

(geniuine question, I know very little of how Ethereum works)

OskarS··on On building 30k Debian packages
The main use of it is for things like language servers. In order to properly show things like compiler errors in your editor, your language server (or linting engine or whatever) needs to know EXACTLY the command you used to compile. The most obvious thing is preprocessor macros, but also things like which C/C++ standard are you using, have you turned off some number of warnings, etc.

compile_commands.json is a way to solve that problem in a "build system independent" way. It's just a list of exactly what commands where used for what files. As long as you generate that file correctly, you can use whatever build system you want (cmake, hand-written make file, 1000s of lines of Perl 5, a shell script from 1983, whatever) and your editor can correctly show errors, warnings, provide auto-completion, all that good stuff.

An alternative (at least for clangd) is to use compile_flags.txt, where you can just specify whatever flags you want the compiler to use

EDIT: if you're asking why this person in particular wants compile_commands.json files, it seems like he just wants a unified format for the compile commands of all the files in all the packages so he can use it for his analysis or whatever.

OskarS··on Linear O(N) sorting with Radix Sort (2000)
An invaluable source for information like this (that did indeed exist in 2001) is The Art of Computer Programming by Donald Knuth. Knuth is extremely diligent in documenting the history of all algorithms he describes (arguably, TAoCP is the best source-book for the history of all computer science) and he has the following to say on the topic of Radix sort:

> The sorting method just described is not immediately obvious, and it isn't clear who first discovered the fact that it works so conveniently. A 19-page pamphlet entitled "The Inventory Simplified," published by the Tabulating Machines Company division of IBM in 1923, presented an interesting Digit Plan method for forming sums of products in their Electric Sorting Machine ... This punched-card tabulating method leads naturally to the discovery of least-significant-digit-first radix sorting, so it probably became known to the machine operators. The first published reference to this principle for sorting appears in L. J. Comrie's early discussion of punched-card equipent [Transactions of the Office Machinery Users' Assoc., Ltd (1929)]

> ...most people rejected the idea of radix sorting within a computer, until H.H. Seward['s master thesis in 1954] pointed out that... [<technical details omitted>]

Presumably, this is a method that has been discovered and rediscovered many times by many programmers and punch-card operators, but that H. H. Seward made it practical for digital computers in 1954.

PS. TAoCP is awesome. Even if you can't follow the more advanced math, just having this book on the shelf to look up stuff like this is lovely if you do this kind of thing professionally. Knuth is just a fantastic scholar and teacher. Just flipping through the things is a joy.

OskarS··on The Awk state machine parser pattern (2018)
I’ve found that AWK is frequently surprisingly readable actually, as long as you understand the execution model. I think people tend to think of it (with some justification) as in the same vein as Perl, but it isn’t nearly as surface-level cryptic. The syntax is just “C-style” with dynamic typing and built in regular expressions.

I have a couple of AWK scripts to handle my personal finances (basically, consuming various bank/credit card statements and turning them to Ledger files) and it’s just the perfect language for that kind of task. My scripts look fairly similar to the examples in the blog post, they also use the same state-machine trick. With the possible exception of Perl/Raku, it’s my favourite language for that kind of thing.

OskarS··on Hoare’s Rebuttal and Bubble Sort’s Comeback
When used as an algorithm (as opposed to a sorting network), insertion sort only inserts "as far as it needs to". In other words: it moves the new element to the correct position, then stops going back through the array. This is the aspect of insertion sort that makes it very fast for "almost sorted" data: each element is only moved a few spots. However, this is a branch that is difficult to predict: this is the author's point. For very small amounts of data, branch misprediction for this branch dominates.

This is not the case when insertion sort is expressed as a sorting network. Take a look at this image [1]: the final "insert" has comparators that go through the entire network. The "algorithm" version wouldn't do that: it would stop (using a branch) as soon as the entry hits the correct place. If you remove that branch (which, remember, is the thing that makes insertion sort faster in the general case), insertion and bubble sort are indeed very similar.

The point here is that "sorting algorithms" and "sorting networks" are not the same thing: a sorting algorithm can branch, a sorting network cannot. The same principles of bubbling and insertion can apply to both, but that does not mean that an insight that applies to sorting networks necessarily applies to sorting algorithms.

[1]: https://en.wikipedia.org/wiki/Sorting_network#/media/File:Re...

(PS: I actually wrote most of that wikipedia article years ago! I did the illustrations too! I basically just wrote down and made diagrams for whatever I could understand from The Art of Computer Programming vol 3. The insight about how bubble sort and insertion sort are essentially the same when expressed as sorting network is from Knuth, though I doubt it's original with him)

OskarS··on Kakoune Code Editor
> Doing the motion first lets you visually confirm the range before invoking the command. You also have the opportunity to adjust the range if you didn't get it quite right.

It should be noted here that Vim more or less works like this in visual mode. If you go "v <motion> <command>" instead of "<command> <motion>", you can visually confirm the range. I can see the argument that it should be the default, though I like the fact that <motion> alone in normal mode just moves you around without preparing for a command.

OskarS··on Netflix’s Prices Are Rising Faster Than Cable
Apple Arcade games have neither ads nor in-app purchases, it's the whole point of the service.
OskarS··on Netflix’s Prices Are Rising Faster Than Cable
> As far as I can tell they've succeeded at becoming HBO.

Have they? Have they really? As far as I can tell, nothing that Netflix has ever done in-house has ever come close to the best of the cable channels. Like, The Witcher is fine, but Game of Thrones it is not, neither in quality nor buzz/cultural significance. House of Cards is no Succession and Ozark is no Breaking Bad. Nothing they have could even come close to comparing to stuff like The Sopranos or The Wire in terms of cultural impact. The closest they've ever come, in my opinion, is probably with Making a Murderer, and they've spent the last couple of years churning out pale imitations that make Netflix look like "True Crime TV".

You're paraphrasing that quote from the Netflix CEO that goes like "We want to become HBO faster than HBO can become us". To me, HBO has won that race handily.

OskarS··on Declarative memory management (2019)
getch() is buffered, the performance is fine.
OskarS··on Speeding up my Advent of Code solution with Dijkstra's Algorithm
That assumes that the path only ever goes right and down. There’s no basis for that assumption in the general case, it’s entirely possible that the shortest path has to take a “detour” to avoid some high cost sections. This is a shortest-path problem, not a DP problem.
OskarS··on Ambiguous PNG Packer: Craft PNG files that appear different in Apple software
> At the risk of repeating comments, welcome to Unicode Hell. And possibly to cross platform development :)

As who has spent a huge amount of time the last few weeks due to an extremely obscure and hard to reproduce Unicode bug in Windows, I can vouch for this. So can my ever receding hairline. Turns out, Unicode is hard.

OskarS··on Lego has designed a set that can't be taken apart
I was mistaken, mine was printed/painted as well.
OskarS··on Threepp: A C++17 Port of Three.js
This is a reasonably common technique in game engines, actually, usually referred to as an “uber shader”. The idea is to have one massive shader with a bunch of if’s that does everything, and then you don’t have to pay the cost of shader state changes.

There are lots of arguments for/against the uber shader technique, but I’m guessing it was probably intentional for performance reasons, not just an accident of development.

OskarS··on Lego has designed a set that can't be taken apart
You’re right, I misremembered: the keys didn’t use stickers, they were painted on. But as you say, they were standard Lego pieces, just painted.
OskarS··on Lego has designed a set that can't be taken apart
I just bought and built the relatively recent Lego typewriter set [1], and I had this exact same question: how much of it was going to be purpose built, and how much would be just generic pieces. The answer is: almost all pieces are just regular Lego pieces.

A small number of components (the curved edge just above the keyboard, for instance, or the roller handle) seemed purpose-made for this model, but, like 99% of the pieces were totally generic. And this is in a set with more than 2000 pieces with lots of very intricate mechanics (there's a roller, a carriage, air pistons, and a ratchet/escapement mechanism that moves the carriage), but virtually all of it was made up of standard bricks, rods, connectors and generic components. I was super impressed. Even the keys are just Lego disks with stickers on them. The spacebar is just regular Lego with a smooth surface.

I feel like people have been making this complaint against Lego forever, and it's always "when I was kid, we just got a box of bricks and let our creativity take over!". The harsh truth is that Lego didn't change. We just grew up.

[1]: https://www.lego.com/en-us/product/typewriter-21327

OskarS··on Theoretical Computer Science Cheat Sheet [pdf]
I dunno, this seems like a perfectly reasonable tool. Saying "what was the double angle formula for sin, again?" or "what was the error term in Stirling's approximation" doesn't mean that you don't understand the fundamentals.
OskarS··on Constructors and evil initializers in C++
I'm sorry, but the final version is FAR AND AWAY the worst version of all the code examples on the page. This tiny class in the stack-allocated version is lightweight, takes up very little space, has great cache locality and performs fantastically: all operations on it will probably compile down to one or two instructions touching memory almost certainly in cache.

The heap-allocated version is so much worse! Not only do you have to pay for allocating and freeing it, every time you reference it, you're gonna have to chase a pointer. Imagine if you had 1000 of these in a vector: the stack allocated ones would be right there next to each other, incredibly cache friendly. The heap-allocated ones are gonna cache miss constantly, and there's ZERO chance the loop is going to auto-vectorize. Show this to a data-oriented design person and watch their eyes start bleeding.

And for what? So you can return null? Just return a std::optional from the factory method if you care so much (or boost::optional if you're on pre-C++17). Or use exceptions. Or just accept the fact that it might return the default value. All of those options are better than the final one. If you're choosing to program in C++, it means you should care about performance. If you don't care about performance for this kind of stuff, just use a higher-level language.

OskarS··on Bash patterns I use weekly
Yes, git bisect is the way to go: in addition to the stuff you mentioned, his method only dives into one parent branch of merge commits. git bisect handles that correctly. A gem of a tool, git bisect.
OskarS··on I hate password rules
> or a binary that runs on my phone is a deal breaker

Phone numbers, fair enough, but TOTP is an open standard and there are plenty of open source implementations for the client side. It’s also available in most password managers (I use 1passwords implementation).

“MFA can’t require me to run a binary on my phone” is a bit extreme. TOTP is fine.

OskarS··on macOS Monterey's new network quality tool is surprisingly good
APFS has CoW semantics, so I would assume it uses the APFS filesystem features.
OskarS··on YAML: It's Time to Move On
So it doesn't have first class functions then?
OskarS··on YAML: It's Time to Move On
> Starlark is a dialect of Python. Like Python, it is a dynamically typed language with high-level data types, first-class functions with lexical scope, and garbage collection.

If it has first-class functions, how can you avoid infinite recursion? Like, what stops me from running the omega combinator in it? This is why Meson (a similar language) does not allow those kinds of shenanigans, to keep the language non-Turing-complete.

OskarS··on A 100LOC C impl of memset, that is faster than glibc's
Modern compilers have quite a deep understanding of memcpy, and they will recognize the pattern and put in optimal assembly (on x86, probably "rep movsb" or whatever), even if you don't literally call memcpy. This is why the GCC implmentation of memcpy is, like, trivial: [1]. The compiler will recognize that this is a memcpy and sub the better implementation.

I wonder though: it seems to me that memory bandwidth should far and away be the limiting factor for a memcpy, so I would think even a straight-forward translation of the "trivial" implementation wouldn't be that far off from an "optimal" one. I guess memory prefetching would make a difference, but would minimizing the number of loads/stores (or unrolling the loop) really matter that much?

[1]: https://github.com/gcc-mirror/gcc/blob/master/libgcc/memcpy....

OskarS··on I'm “still afraid to use spaces in file names” years old
For a download folder in particular, this does sound like a great idea. You'd break the list in the browser or whatever, but who cares about that?
OskarS··on I'm “still afraid to use spaces in file names” years old
Overly aggresive is right! I don't know if this is genius or deranged! I'm leaning towards genius and stealing the idea.

By the way: what's your beef with en dashes? I mean, if it was "everything should be 'HYPHEN-MINUS' (U+002D)", then fine, but why specifically en dashes and not em dashes?

OskarS··on Write thin to write fast
Robert Bringhurst in "The Elements of Typographic Style" (a bible for this kind of thing, and one of the most beautiful books ever made), has this to say on the topic:

"Anything from 45 to 75 characters is widely regarded as a satisfactory length of line for a single-column page set in a serifed text face in a text size. The 66-character line (counting both letters and spaces) is widely regarded as ideal. For multiple-column work, a better average is 40 to 50 characters.

If the type is well set and printed, lines of 85 or 90 characters will pose no problem in discontinuous texts, such as bibliographies, or, with generous leading, in footnotes. But even with generous leading, a line that averages more than 75 or 80 characters is likely to be too long for continuous reading."

← PreviousPage 15 of 34Next →