HNHacker News
TopNewBestAskShowJobs

OskarS

8,392 karma · joined September 4, 2016

submissionscomments
OskarS··on Undefined Behavior in C and C++ (2024)
C and C++ are languages that brought us UNIX, the Linux kernel, macOS and Windows, the interpreters of virtually every other language in the world, powering virtually all software in the world as well as the vast majority of embedded devices.

Chill the fuck out.

OskarS··on A fast, growable array with stable pointers in C
They have their place. Certainly B-tree data-structures are tremendously useful and usually reasonably cache friendly. And if std::deque weren't busted on MSVC, there are times where it would be very useful. Linked lists have their place as well; a classic example would be an LRU cache, which is usually implemented as a hash table interleaved with a doubly linked list.

But yeah. Contiguous dynamic arrays and hash tables, those are usually what you want.

OskarS··on Why Exercise Is a Miracle Drug
This is not an obscure theory, it's called the "Endurance running hypothesis" [1], and it does seem that humans are one of very few species exceptionally well-adapted to very long-distance running (though dogs and wolves are another one). The idea is that ancient hominids in Africa practiced "persistence hunting"[2], where they would hunt e.g. antelopes and similar species by repeatedly chasing them down. The prey would sprint off when humans got close, but doing this repeatedly causes the animal to exhaust their energy or die of heat stroke, humans being much better adapted to running for many hours on end (this kind of hunting is known to happen to this day). It's plausible that persistence hunting also contributed to evolving our other super-powers, as it requires good spatial awareness, tool-use and communication.

[1]: https://en.wikipedia.org/wiki/Endurance_running_hypothesis

[2]: https://en.wikipedia.org/wiki/Persistence_hunting

OskarS··on TrackWeight: Turn your MacBook's trackpad into a digital weighing scale
It's also not the same thing as Dropbox was offering: that's a description of a network drive, but the key thing about Dropbox is that it's a syncing engine. It's a much harder thing to do, but with very big benefits: much faster (since it's just reading off disk) and offline access.
OskarS··on C++ Coroutines Advanced: Converting std:future to asio:awaitable
I think the idea is that you're using some external library (e.g. database drivers) which do not use asio but returns a std::future. You can't just "not use std::future" if that's what your library uses without fully rewriting your external library.

The other option is as you mention polling using a timer, but I don't see how that's better, I'd rather move the work off of the event loop to a thread. And you then have to do the "latency vs. CPU time" tradeoff dance, trying to judge how often to poll vs. how much latency you're willing to accept.

OskarS··on The Scourge of Arial (2001)
First off all, these fonts are not "free" as in "free software", they are owned by Microsoft and to use them you have to agree to a Microsoft EULA (technically, though this isn't enforced). They're "free as in beer", not "free as in freedom" (this is my understanding at least, welcome to be corrected on this point).

Second: the objection isn't necessarily just that Verdana is free, it's that it's not a very good-looking font. Certainly in most people's opinion, it's nothing like as cool as Futura. IKEA, a massive multi-national company and an iconic brand, can and should do better. They say they did this to "align print and web", presumably meaning they wanted to use a font that was guaranteed to work in all browsers, but that's such a shame for print, really sacrificing great typography on the altar of browser support.

Third: font design is an art and a craft just like graphic design, photography, furniture design, script-writing, music production, whatever. There are certainly people who think literally everything should be free (as in "every Hollywood release should be Creative Commons"), but that's a rather extreme position. If you think it's a good thing that IKEA pays graphic designers to design their catalog, and furniture designers to design their furniture, you should think it's a good thing that IKEA pays typographers for their typeface design.

There are certainly issues with licensing in the world of typeface design (the emerging Monotype monopoly is really disturbing, for instance), but expecting giant companies to pay for good typography instead of using bad free typography is not some "anti-free software" stance.

OskarS··on The Scourge of Arial (2001)
Verdana isn’t terrible, it’s just that it’s so much worse than fonts like Frutiger and Gill Sans that inspired it (similarly to Arial/Helvetica). It’s screams ”free font that kinda rips off really good ones”, and for a giant company like IKEA, you’d expect them to do better. Especially since their previous typeface was Futura, an all-timer.
OskarS··on At Least 13 People Died by Suicide Amid U.K. Post Office Scandal, Report Says
I bet number 3 on your list there is super-appealing to many people. It sounds lovely to be the kind of person in a smaller community that everyone knows and says hi to, that helps you out with paying your bills or whatever it is. I’m guessing you’re also often the closest contact to the state in a smaller village, so there’s probably all sorts of applications and permits you’re asked to help out with.

Especially if you’re on the older side, it sounds like an absolutely wonderful way to spend your time. Assuming the post office doesn’t try to ruin your life afterwards.

OskarS··on Ruby 3.4 frozen string literals: What Rails developers need to know
I would actually say it’s the opposite in this case: it’s extremely common in scripting languages for strings to be immutable, mutable strings are usually only available in lower level languages. I’m very surprised Ruby had this feature at all.
OskarS··on Features of D That I Love
The "invariants" thing is fantastic, I haven't seen anything like that before and it's great. The C++26 contract stuff is fine, but this seems like a really great way of ensuring type invariants, I think I'd use this way more if it was in C++.
OskarS··on NativeJIT: A C++ expression –> x64 JIT (2018)
Yes, but what I suspect the commenter was saying is that you can build the expression usung operator overloading as well, so you can type ”a + b”, not ”a.Add(b)”.

I love it when libraries like this do that. z3 in python is similar, you just build your constraints using normal syntax and it all just works. Great use of operator overloading.

OskarS··on I Switched from Flutter and Rust to Rust and Egui
I really like this model of ”GPL3, but commercial licenses available”. Means you can have a sustainable business without sacrificing open source. I wish it was more widespread, the prevalence of extremely permissive licenses has led to so many enormous companies using open source without giving back.
OskarS··on Simplest C++ Callback, from SumatraPDF
Fair point, but generally speaking, callbacks tend to escape the scopes they are in (if you have a callback for ”user clicked mouse”, it’s likely not going to be triggered in your current scope), so stack-allocation isn’t really an option.

But yes, fair point: they can be stack or statically allocated as well.

OskarS··on Simplest C++ Callback, from SumatraPDF
> But I do know the code I write and you're wrong about performance of Func0 and Func1. Those are 2 machine words and all it takes to construct them or copy them is to set those 2 fields.

You also have to heap allocate your userData, which is something std::function<> avoids (in all standard implementations) if it’s small enough (this is why the sizeof() of std::function is larger than 16 bytes, so that it can optionally store the data inline, similar to the small string optimization). The cost of that heap allocation is not insignificant.

If I were doing this, I might just go the full C route and just use function pointers and an extra ”userData” argument. This seems like an awkward ”middle ground” between C and C++.

OskarS··on Simplest C++ Callback, from SumatraPDF
Indeed, that is exactly the case, lambdas are essentially syntax sugar for doing this.

The one thing the author's solution does which this solution (and lambdas) does not is type erasure: if you want to pass that closure around, you have to use templates, and you can't store different lambdas in the same data structure even if they have the same signature.

You could solve that in your case by making `void operator()` virtual and inheriting (though that means you have to heap-allocate all your lambdas), or use `std::function<>`, which is a generic solution to this problem (which may or may not allocate, if the lambda is small enough, it's usually optimized to be stored inline).

I get where the author is coming from, but this seems very much like an inferior solution to just using `std::function<>`.

OskarS··on Chatbots are replacing Google's search, devastating traffic for some publishers
If the paywall bothers you, then Kagi just isn't for you, the whole point of it is to have a business model where the users are customers, not the product.

I have no problem with the UI. It's snappy, clean, doesn't have a bunch of cruft, it's easy to find results, etc. It feels like Google of yore.

Same thing with the search results, I find them really excellent. In my experience, there's much less SEO spam. If you search for a programming language function, you get links to the proper documentation, not some trash "geeksforgeeks" site or whatever. I can't remember if I've ever had to go past page one.

Obviously YMMV on that, it's entirely possible it's different depending on what you're searching for. But I'm very happy paying for Kagi, and will continue to do so.

OskarS··on Apple has announced its final version of macOS for Intel
I have a sort-of controversial opinion about this: on the contrary, this is one of the reasons I really love using a mac.

As a developer, it’s super-annoying that Apple is so aggressive in deprecation stuff, it forces us to frequently spend a bunch of extra time and energy updating our stuff to make sure it’s compatible woth latest macOS.

As a user though, this means that the experience of actually using a mac feels fresh and modern, and there’s a unity in design language that isn’t available anywhere else. It doesn’t take much digging in Windows settings to find windows that feel straight out of Windows 98, because Microsoft is so terrified of breaking things. macOS just feels better because of Apple’s aggressive deprecation policies, and I very much prefer it.

OskarS··on IRS Direct File on GitHub
Just, as a person outside of the US: the idea that you would need an accountant at all for a private person that isn't a billionarie, is like, crazy.

I have a reasonably simple personal economy and it takes me all of five minutes to file my taxes in Sweden. My parents have a much more complicated setup (small private business, own a couple of properties, several deductions, etc.) and it basically is pretty straight-forward for them as well, certainly they don't need an accountant.

TurboTax, Intuit and anti-tax Republicans has really fucked with the US expectation of how complicated taxes needs to be.

OskarS··on Show HN: Fast Random Library for C++17
I’m assuming here that ”generator” has a decent enough constructor that seeds it properly. If it doesn’t, and you have to cycle it a few times to ”warm it up”, it’s not much more complicated, just factor out the initialization code into a function or lambda and use that to initialize the thread_local.
OskarS··on Writing your own C++ standard library part 2
Huh, TIL! I didn't realize that, I just always avoid this pattern because it's such a common source of bugs (and if I really need to, I just use the erase_if).

EDIT: just saw your example and checked cppreference, it says the return value "Iterator following the last removed element" for std::unordered_map. So i think you need to add an `it--` after your erase, otherwise it will "skip over" the next element. Right?

Also just read this little nugget on cppreference for unordered_map::erase:

> Removes specified elements from the container.The order of the remaining elements is preserved. (This makes it possible to erase individual elements while iterating through the container.)

This seems like a crazy guarantee to put in the standard, it must really limit the kinds of hash tables you can make that matches the unordered_map interface.

OskarS··on Writing your own C++ standard library part 2
Ah, ok. But then: you kinda can't do that at all. You certainly shouldn't.

For unordered_map (and every hash table in the known universe) erasing anything invalidates all iterators, so you can't iterate while erasing. For std::map, you can if you're very, very careful (erasing invalidates the iterator you're currently on, but if you cache the next iterator, THEN erase the current one, it'll probably work, but be very fiddly). Most languages forbid this entirely: e.g. Rust's ownership model doesn't allow it, Python throws an exception, etc. It's just a very bad idea in general.

OskarS··on Show HN: Fast Random Library for C++17
I think it looks great! Might be using this in a future project.

One note on API design: I think it's a FANTASTIC idea to have default `rand()` functions available, since very often, you don't really care about the generator and you're just like "just give me a random number, i don't care how". But if you do, you shouldn't have a `seed()` function, because that means you can then never upgrade it without breaking your API contract. It should always be seeded with entropy. This is why glibc's `rand()` is still using an LCG from the late neolithic, they can't ever update it without breaking a gazillion applications and tests. This is the classic example of "Hyrum's law". Doing this also helps with thread-safety: you can just make the seed/state thread-local and then it just works.

Basically, if you want the ability to seed your PRNG, you also need to specify the generator explicitly. The global one is for convenience only, and there should be no ability in the API to seed it.

EDIT: btw, this is a really nice thing about C++, doing this is dead easy:

    int rand() {
        thread_local generator my_generator { seed_with_entropy() };
        return my_generator.rand();
    }
OskarS··on Writing your own C++ standard library part 2
Both of these things are very easy to do in modern C++:

    // erase
    map.erase(2);

    // iterate
    for (auto &[k,v]: map) {
        // do stuff
    }
godbolt: https://godbolt.org/z/o8f6zhqxq
OskarS··on M8.2 solar flare, Strong G4 geomagnetic storm watch
I've read there was a huge solar flare in the 19th century that knocked out telegraph equipment all over the world. Do we know how strong that event was on that scale?
OskarS··on Atomics and Concurrency
I got curious about this, because really the most important guarantee is in the C++ memory model, not any of the ISAs, and conforming compilers are required to fulfill them (and it's generally more restrictive than any of the platform guarantees). It's a little bit hard to parse, but if I'm reading section 6.9.2.3 of the C++23 standard (from here [1]), operations on atomics are only lock-free, not wait-free, and even that might be a high bar on certain platforms:

    Executions of atomic functions that are either defined to be lock-free 
    (33.5.10) or indicated as lock-free (33.5.5) are lock-free executions.

    When one or more lock-free executions run concurrently, at least one should 
    complete.

        [Note 3 : It is difficult for some implementations to provide absolute 
        guarantees to this effect, since repeated and particularly inopportune 
        interference from other threads could prevent forward progress, e.g., by 
        repeatedly stealing a cache line for unrelated purposes between load-locked 
        and store-conditional instructions. For implementations that follow this 
        recommendation and ensure that such effects cannot indefinitely delay progress 
        under expected operating conditions, such anomalies can therefore safely be 
        ignored by programmers. Outside this document, this property is sometimes 
        termed lock-free. — end note]
I'm guessing that note is for platforms like you mention, where the underlying ISA makes this (more or less) impossible. I would assume in the modern versions of these ISAs though, essentially everything in std::atomic is wait-free, in practice.

[1] https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4950.p...

OskarS··on Atomics and Concurrency
> It’s probably best euphemismed by imagining that every atomic operation is just the normal operation wrapped in a mutex

Sort-of, but that's not quite right: if you imagine it as "taking a mutex on the memory", there's a possibility of starvation. Imagine two threads repeatedly "locking" the memory location to update it. With a mutex, it's possible that one of them get starved, never getting to update the location, stalling indefinitely.

At least x86 (and I'm sure ARM and RISC-V as well) make a much stronger progress guarantee than a mutex would: the operation is effectively wait-free. All threads are guaranteed to make progress in some finite amount of time, no one will be starved. At least, that's my understanding from reading much smarter people talking about the cache synchronization protocols of modern ISAs.

Given that, I think a better mental model is roughly something like the article describes: the operation might be slower under high contention, but not "blocking" slow, it is guaranteed to finish in a finite amount of time and atomically ("in one combined operation").

OskarS··on Making C and Python Talk to Each Other
Another thing to mention is that until very recently (Python 3.12, I think?) every interpreter in the address space shared a lot of global state, including most importantly the GIL. For my area (audio plugins) that made Python a non-starter for embedding, while Lua works great.

I agree though: biggest reason is probably the C API. Lua's is so simple to embed and to integrate with your code-base compared to Python. The language is also optimized for "quick compiling", and it's also very lightweight.

These days, however, one might argue that you gain so much from embedding either Python or JavaScript, it might be worth the extra pain on the C/C++ side.

OskarS··on The UI future is colourful and dimensional
I was just having a discussion with our customer support manager for an coming update to our software. He made this point, ”I really like it when buttons have names. Then I can tell the customers ’click the EXPORT button’ instead of ’click the little button that kinda looks like a square with an arrow pointing out of it’”.

It really is a very good point.

OskarS··on What are people doing? Live-ish estimates based on global population dynamics
Presumably time of day matters? I’m guessing more people give birth or have c-sections during the day (not sure if that’s true of fully natural births, but for induced and c-sections, seems very likely).
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.

← PreviousPage 5 of 34Next →