HNHacker News
TopNewBestAskShowJobs

OskarS

8,395 karma · joined September 4, 2016

submissionscomments
OskarS··on Working with Git Patches in Apple Mail (2023)
That is a very neat trick, I agree.

I personally approaches stashes as undoable "clean up", and I never have anything really important that I want to save there. If I do have something like that, I just commit with a "WIP <some-descriptive-string>" message and don't push it, then a "git reset --mixed HEAD^" when I want to get back to it.

However, just FYI: you can "grep" your stashes really easily if you want to. just "git stash list -p" gives you the diffs for all the stashes, by default in "less" where you can search them, but you can pipe it to grep if you want. I somewhat frequently do that with "git log", if I want to know "when did this variable change?" or whatever, just "git log -p" to get the log with diffs in less, then search for whatever it was with a slash.

OskarS··on JavaScript's New Superpower: Explicit Resource Management
Only experience with C# using and Python context managers, but they always seemed like such an unsatisfying solution compared to C++/Rust style RAII. I mean, I get these are GC languages, but why can't just this happen at end of your regular scope? Why do you have to have these artificial new scopes?
OskarS··on The first year of free-threaded Python
That doesn't match with my understanding of free-threaded Python. The GIL is being replaced with fine-grained locking on the objects themselves, so sharing data-structures between threads is still going to work just fine. If you're talking about concurrency issues like this causing out-of-bounds errors:

    if len(my_list) > 5:
        print(my_list[5])
(i.e. because a different thread can pop from the list in-between the check and the print), that could just as easily happen today. The GIL makes sure that only one python interpreter runs at once, but it's entirely possible that the GIL is released and switches to a different thread after the check but before the print, so there's no extra thread-safety issue in free-threaded mode.

The problems (as I understand it, happy to be corrected), are mostly two-fold: performance and ecosystem. Using fine-grained locking is potentially much less efficient than using the GIL in the single-threaded case (you have to take and release many more locks, and reference count updates have to be atomic), and many, many C extensions are written under the assumption that the GIL exists.

OskarS··on Implementing a Struct of Arrays
> template<template<template<class> class> class C>

Boy howdy!

OskarS··on Inheritance was invented as a performance hack (2021)
> The only one that springs to mind is a View hierarchy in UI libraries.

I'd like to generalize that a little bit and say: graph structures in general. A view hierarchy is essentially a tree, where each node has a bunch of common bits (tree logic) and a bunch of custom bits (the actual view). There are tons of "graph structures" that fit that general pattern: for instance, if you have some sort of data pipeline DAG where data comes in on the left, goes out on the right, and in the middle has to pass through a bunch of transformations that are linked in some kind of DAG. Inheritance is great for this: you just have your nodes inherit from some kind of abstract "Node" class that handles the connection and data flow, and you can implement your complex custom behaviors however you want and makes it very easy to make new ones.

I'm very much in agreement that OOP inheritance has been horrendously overused in the 90s and 00s (especially in enterprise), but for some stuff, the model works really well. And works much better than e.g. sum types or composition or whatever for these kinds of things. Use the right tool for the right job, that's the central point. Nothing is one-size-fits-all.

OskarS··on Duolingo will replace contract workers with AI
It's actually stunning how off-putting it is. I started Duolingo because I was in a relationship with someone and wanted to learn their language. I paid for subscription because I didn't want to be assaulted with ads and upsells (and I believe in paying for software in general and I think subscriptions can be a fair business model), but even as a paying customer it was miserable!

It's so fucking lousy with gems and upsells and quests and "try the AI call, you get one for free and then you can upgrade to a more expensive subscription!". The gamification of everything in Duolingo is so bad. And, as you say: I feel like I'm wasn't learning any of the fundamentals, can't you just tell me how to conjugate this verb and then testing me instead of making a dumb guessing game out of it?

Compare it to (say) YouTube Premium: I know a lot of people hate it, and that it's an expensive subscription, but I honestly think it's pretty fair. All the commercialization and upsell go away entirely, you get a very clean experience for something that I use more than any other streaming service. Don't mind paying for it at all. Not Duolingo though, I cancelled it after a week and a half.

OskarS··on Parallel ./configure
Meson and cmake in my experience are both MUCH faster though. It’s much less of an issue with these systems than with autotools.
OskarS··on Some __nonstring__ Turbulence
Fair, good point.
OskarS··on A Visual Journey Through Async Rust
Very neat visualizations! Really good way to demonstrate the underlying principles. Great article.

I will add one thing: the author makes much of how problematic CPU-intensive tasks are. That's true, but also: if you need to do a CPU-intensive operation, you need to tie up a CPU to do it, there's no getting around it. What you might want to do is break the computation up in to smaller chunks with `.await`s sprinkled in there or dispatch it to a background thread, but at some point you have to pay the cost in CPU time.

What is much more problematic is blocking I/O operations. That also ties up a CPU and blocks the event loop, but for no good reason. All the CPU is going to be doing is sleeping waiting for the network or hard-drives or whatever it is to do it's work, when it could be doing other useful things, processing other events. That kind of thing leads to huge under-utilization of your resources, and it kills your concurrency.

This means you really can't mix async I/O with blocking I/O: if you go async, ALL your I/O operations need to be async. Or you have to do it off the event loop in a background thread or something, but then you're losing all the benefits of using async in the first place.

OskarS··on Some __nonstring__ Turbulence
But you would still need to change it everywhere, right? Like, instead of changing the annotation everywhere you have to change the type everywhere. Doesn't seem like a huge difference to me.
OskarS··on Advanced Shell Scripting with Bash (2006) [pdf]
Is Guile really that good at the shell scripting "thing"? Like, the bash superpower is that it makes it extremely easy to run subprocesses in incredibly flexible ways (in the background, foreground, in pipelines, with stdout/stderr redirected to files, etc. etc.). Can Guile do that smoothly? I generally find that doing things like this is way more awkward in most "real" programming languages compared to shell scripting languages.
OskarS··on Concurrency in Haskell: Fast, Simple, Correct
It's Knuth! [1]

"Beware of bugs in the above code; I have only proved it correct, not tried it."

[1]: https://www-cs-faculty.stanford.edu/~knuth/faq.html

OskarS··on That's a Lot of YAML
For a configuration language, comments are absolutely crucial. You want to be able to say "# This option is set because <so-and-so>" to explain why you are configuring it this way to the next person that reads the code (or you, in the future).

If the price to pay is that there is some risk some dummy might start parsing the comments as code, so be it. This is not a really a problem in "regular" programming languages, I don't see why it would be in a configuration language.

OskarS··on Nix Derivations, Without Guessing
To be fair, you get this all the time when you run nix-channel update, "<whatever> has been deprecated/removed, use <something-else> instead".
OskarS··on 3,200% CPU Utilization
> That will make the individual map operations thread-safe, but given that nobody thought of concurrency, are you sure series of operations are thread-safe? That is, are you sure the object that owns the tree-map is thread-safe. I wouldn't bet on it.

This is a very important point! And, in my experience, a common mistake by programmers who aren't great at concurrency.

Lets say you have a concurrent dynamic array. The array class is designed to be thread-safe for the explicit purpose that you want to share it between threads. You want to access element 10, but you want to be sure that you're not out of bounds. So you do this:

    if (array.size() > 10) {
        array.element(10).doSomething();
    }
It doesn't matter how thread-safe this array class is: these two operations combined are NOT thread-safe, because thread-safety of the class only means that `.size()` and `.element()` are on their own not going to cause any races. But it's entirely possible another thread removes elements in between you checking the size and accessing the element, at which point you'll (at best) get an out-of-bounds crash.

The way to fix it is to either use atomic methods on the class which does both (something like `.element_or_null()` or whatever), or to not bother with a concurrent dynamic array at all and instead just use regular one you guard with a mutex (so the mutex is held during both operations and whatever other operations other threads perform on the array).

OskarS··on Microsoft is killing Skype
Slack is primarily a business tool, and for a business tool $10/user/month is extremely reasonable for the value (perceived or real) it brings. The company has to make money, and you do that by charging for your products and services, and that price is not exorbitant.
OskarS··on Man from uncontacted tribe emerges in Amazon, villagers demonstrate a lighter
The point is to let them choose for themselves. If they want to contact us, that's perfectly fine. But it should be their choice to make, not ours.
OskarS··on Man from uncontacted tribe emerges in Amazon, villagers demonstrate a lighter
The argument here is that this is exactly how (mostly) European societies hell-bent on colonization have been justifying their actions for five hundred years, with different variations of "amazing stuff" (salvation, weapons, trade goods, technology, medicine...). It's almost universally led to these tribes being subjugated, enslaved, exploited or just murdered outright. Humanity has a terrible track-record on this topic, and we should learn from our mistakes and just leave them alone.
OskarS··on Sid Meier's Civilization VII
I liked VI better than V (the districts were fun!) but never touched any mods. I think some of the ideas that were maybe a bit half-baked in VI comes together really nicely in VII.
OskarS··on I Like and Use Global Variables
I'm sorry you feel offended, but you did write an article online with a deliberately provocative title. If you do that, you need to be able to deal with criticism. And not considering concurrency when mutating globals in C/C++ is not acceptable (never mind good) practice. You can say "threads are out of scope" till your blue in the face, but if you write an article with the thesis "globals are good, actually", you have to be able to deal with people saying "they're dangerous because of thread safety". That is a legitimate criticism of your thesis. In addition, my other criticisms of your code (overflow and properly scoping statics) have nothing to do with concurrency.
OskarS··on Sid Meier's Civilization VII
I've put around 20 hours into this game at this point, and I think the criticisms are way overblown. The UI is a bit clunky in spots, but the game is very solid, and very fun. I've played every Civ since IV, and mechanics-wise, this is my favorite one (actually, Alpha Centauri... but this one is a close second). I have not found it particularly buggy (as opposed to clunky).

It feels like with every game release there's this army of perpetually pissed of gamers just waiting to pounce on anything, and when you play it it's like "this is what people were so mad about?". Same thing with Dragon Age a few months ago: everyone talked about it like it was some disaster, but I played it, and it's... fine? I mean, it's not the greatest game ever, but it's not terrible. It's not as good as Inquisition, but better than Dragon Age 2. It's certainly nowhere near as bad as Mass Effect: Andromeda.

OskarS··on I Like and Use Global Variables
I'm sorry, but no. This is one of the primary reasons why global variables is bad, because of concurrency issues. If you add this to a code base, are you guaranteeing that at no point in the next decade, NOBODY is going to add a second thread? What if this is a library? What if some other library you're using runs a thread?

Doing this is planting a time-bomb in your code-base. It's going to lay dormant for a long time, and then explode, and be impossible to debug. This is why mutating global variables is `unsafe` in Rust, which is the right call. There is literally no C or C++ code base where I would allow this to pass code review, regardless of how single-threaded the author thinks it is.

> needlessly complex

It looks more complex than it is because I set the memory order to relaxed as an optimization. You could also write this:

    int64_t counter() { 
        static std::atomic<int64_t> prv_counter { 0 };
        return ++prv_counter;
    }
It's really not hard.

If this was a junior making this mistake, I would sit down and talk to them and patiently and explain the issues. If you're writing a blog post on the internet saying "this is a good way to write code, do it like this!", I'm sorry, but I'm going to be very harsh. We haven't even discussed using `int` and not properly scoping the static, also rookie mistakes. Someone is going to read this article, and think this kind of code is ok. It is not.

> "If you're using threads, global and static variables should be thread local. If they are not then the discussion becomes about sharing data across threads and synchronization, which is a different topic."

This is nonsense. First off all, a counter like this wouldn't work if it was a thread_local, because then you'd get duplicate values. If duplicate values are ok, why is it global state at all? The only reason you'd make it global state is to produce unique values for that process run. And the general sentiment is like saying "this is how you drive drunk safely. We're not discussing keeping control of your car and crashing into a tree, that's a different topic".

This IS why global variables are bad! Not the only reason, but a pretty darn big one!

OskarS··on I Like and Use Global Variables
No, this is absolutely not true at all. Calling this function from multiple threads is undefined behaviour in C++ (unless synchronized using some other mechanism), you get NO guarantees what so ever on program behaviour.

Best case scenario is that the loads and stores are interleaved, which leads to multiple threads returning the same value when calling counter(), which will guarantee crashes elsewhere in the program (the purpose of functions like these is to produce UNIQUE values, after all). But it's undefined behaviour in any case, it's just unacceptable to put in a C/C++ code base.

OskarS··on I Like and Use Global Variables
There are many reasons why global variables are bad, but extremely high on the list (if not first) is concurrency issues. The fact that the author think it's acceptable in any C or C++ code base to put in code like:

    static int prv_counter;
    int counter() { return ++prv_counter; }
Is insanity. Like, this is programmer malpractice. This function can only ever be called from one thread (not to mention: using `int` instead of `int64_t` is also a trivial mistake, this easily overflows). This is the kind of thing that enters a code base, works fine for long enough that everyone forgets about it, then causes horrible security issues and crashes. The idea of saying this is "good use" of a global variable is... this person should not be giving advice on good coding.

If you want to do this (and you shouldn't, because global state is bad for 14 other reasons), at the very least, make it thread safe and not trivially overflowing:

    static std::atomic<int64_t> prv_counter { 0 };
    int64_t counter() { return 1 + prv_counter.fetch_add(1, std::memory_order_relaxed); }
(the 1+ is because the original author used pre-increment instead of post-increment)

Like, this is not awesome, and you shouldn't do it, but it's at least not a total disaster.

EDIT: actually, this is also bad, because the `prv_counter` is not really private at all. The better way to do that would be:

    int64_t counter() { 
        static std::atomic<int64_t> prv_counter { 0 };
        return 1 + prv_counter.fetch_add(1, std::memory_order_relaxed);
    }
Three different serious issues in two lines of code, fun!
OskarS··on Noether's Theorem Revolutionized Physics
I think he sets quite a high bar here. Like, just going through the basics of Noether’s Theorem is so far beyond what normally passes for popular science (which usually boils down to “the universe is really big you guys” and “quantum stuff is weird and nobody understands it”). Like, just using words like “conservation laws” and “Lagrangian” is risky already.

Personally, I would have liked it to dig deeper (as I already heard the basics of Noether’s theorem, but am not a physicist or have studied it in any great depth), but Quanta is not a scientific journal, it’s a pop-sci magazine. The article is a great intro.

OskarS··on Donald Knuth's 2024 Christmas Lecture: Strong and Weak Components [video]
Implementing an algorithm like Dancing Links is a hugely rewarding experience as a programmer. You learn by doing.
OskarS··on Donald Knuth's 2024 Christmas Lecture: Strong and Weak Components [video]
I cannot recommend checking out Volume 4B enough, then. He goes much deeper, improves the algorithm, adapts it to different constraint problems, and provides hundreds of really cool exercises (including word problems like crosswords, I think). It's the best.
OskarS··on Donald Knuth's 2024 Christmas Lecture: Strong and Weak Components [video]
It does improve memory usage! In the version in the paper, each item has four links, in the book version they only have two. So memory usage is roughly cut in half.
OskarS··on Donald Knuth's 2024 Christmas Lecture: Strong and Weak Components [video]
You know in the sparse matrix, every item has four links (left, right, up and down)? In the version in the book, each item only has two links, up and down. The left/right links become implicit, because he stores the items contiguously in an array (so right of item 14 is item 15), and inserts "spacers" so you know when a row ends and the next one begins. This doesn't algorithmically improve the runtime, but it uses half the memory and gives it much better cache behaviour (btw: this is sometimes a charge levied at Knuth, he only cares about algorithms and not practical runtime when it comes to things like cache coherency. This is ludicrous, as this example shows.)

That's the change to the "basic" algorithm, but he then goes on to add a bunch of new features to basic Dancing Links, modifying it so that it's suitable for different kinds of constraint solving (adding required rows, things like that).

If you've implemented Dancing Links in the past, you owe it to yourself to check out the book version! He also has hundreds of really great exercises about it.

OskarS··on Donald Knuth's 2024 Christmas Lecture: Strong and Weak Components [video]
On my off hours, I’ve been working through volumes 4A and 4B. They are really wonderful, I highly recommend them. They’re not practical for the vast majority of programmers, but the way he designs and writes about algorithms is remarkable, truly unique. The Dancing Links implementation in 4B in particular (updated significantly from his famous paper) is like a work of art, it’s such an intricate and gorgeous data structure, and blazing fast as well. Man still got it, in his 80s.
← PreviousPage 6 of 34Next →