Hacker Newsnew | past | comments | ask | show | jobs | submit | simonask's commentslogin

All things... except non-nullable pointers, movable values, type punning, ergonomic sum types, and the list is frankly much longer.

The only way you could think that C++ lets you do everything is if you've never tried anything else.


You have to know a lot about C++ to tell at a glance what's wrong with this code. I fall in that category, and I can tell you I think that the signature of Sanitize() looks extremely reasonable!

What's completely unreasonable here is how easy it is for code that looks reasonable in C++ to be deeply dangerous. This is a perfectly normal piece of code, and it looks like the author is even conscientious about nice things like avoiding too many string copies. We shouldn't be out here defending the absolutely unhinged semantics that led to this.

C++ is so extremely hostile that people who try to write good code end up with a very defensive style of programming if they know what they're doing. In my experience, this tends to either almost fully eradicate any performance benefits they were chasing by choosing C++ in the first place, or blow up the time it takes to deliver the product.


I agree with your general sentiment, but imo this particular example is oversold. Maybe I'm wrong.

The difference between const ref string and string view is one of the first things you learn when you learn about `string view`.

To me, the signature of `Sanitize` is fishy because it's implied that the returned string view is of the const ref string argument. But I agree there are more than one ways to break it.


What is funny is that Rust has similar or worse issues regarding temporary lifetimes. Only some UB is caught (and in practice only sometimes), issues like deadlocks aren't.

https://fasterthanli.me/articles/a-rust-match-made-in-hell

Challenge to you, simonask: Explain why the curly braces here determines whether there is a deadlock or not. Bonus points if you do so without asking for help or looking anything up.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

https://play.rust-lang.org/?version=stable&mode=debug&editio...


I'm responding so future readers see the answer, because oooh boy am I not engaging with this account.

The curly braces shortens the lifespan of the `MutexGuard` so it gets unlocked before the body of the if-statement. Normally, the temporary used in the if-let pattern lives until the end of the if-statement.

This wouldn't compile if the value guarded by the Mutex wasn't trivially copyable, but it is.

(Note how none of this is UB, just a particularly contrived way to introduce a deadlock.)


No I'm sure it will compile. But will it silently corrupt the customer's data 5 years down the line due to a compiler upgrade that suddenly exposed UB in a rarely taken code path? Much less sure.

Exactly. C++ programmer confidence is a bell curve, where the X axis is skill level.

The more you know about programming in C++, the less confident you become that your code is actually correct, because you've seen before how the most seemingly innocuous decisions turned out to be massive UB footguns. Even worse if you know anything about how computers actually work - that's not relevant, C++ like C targets an abstract virtual machine with semantics unlike any computer ever built.

The simplest example is signed integer overflow: the bright young student knows all about Two's Complement and mistakenly believes all is well, and the wizened but weary mentor must waste everyone's time to avoid summoning nasal demons.

What an unbelievable waste.


I'd say C++ is so vast that you can keep on encountering bell curves like that. I start working on a project, building confidence in the subset of C++ I'm working with and when I start feeling good about myself, I'm hit with a completely foreign concept

cppquiz is a nice test beyond "trivia". There are a few hard questions addressing topics I've never encountered. For example:

```

#include <iostream>

volatile int a;

int main() { std::cout << (a + a); }

```

I would assume that here a is initialised to 0 since it has static lifetime so I'd expect this to just print 0. But the C++ standard specifies that:

> Reading an object designated by a volatile glvalue ([basic.lval]), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression (or a subexpression) in general includes both value computations (including determining the identity of an object for glvalue evaluation and fetching a value previously assigned to an object for prvalue evaluation) and initiation of side effects. When a call to a library I/O function returns or an access through a volatile glvalue is evaluated the side effect is considered complete, even though some external actions implied by the call (such as the I/O itself) or by the volatile access may not have completed yet.

and if you have side effects unsequenced relative to other side effects (as is the case with operations of the same operand), this is considered UB


In practice, it will be 0, assuming no external modification of a. Strictly under standard, its UB.

yes which is exactly why you don't keep those things in mind most of the times. They are mostly academic exercises yet they pop up in your actual code from time to time and you find yourself furiously scratching your head. But my point is that C++ is huge and there is no way any single human can keep the whole standard in their head.

Assertions were always an option. Unless I'm missing some new development, contracts are just a new way to spell assertions. That's good, but it doesn't move the needle at all in terms of memory safety.

making memory safe patterns easier to implement makes for more memory safety:)

Harder to read, easier to understand.

It's because it is an unbelievably difficult and quite finicky thing to support, especially if the 3D canvas uses a different rendering API than the UI framework itself.

For example, say your UI is drawn with OpenGL, but you need an embedded Vulkan canvas. This takes a huge amount of API-specific code passing API-foreign texture handles and synchronization objects between contexts. You need to do this for every possible combination of APIs, and it's not symmetrical (hosting Vulkan within OpenGL is different from hosting OpenGL within Vulkan).

Every desktop-class rendering API does have extensions for dealing with foreign APIs, but it's a lot of work, to the point where rendering into shared memory from a subprocess is sometimes the most viable option.


> especially if the 3D canvas uses a different rendering API than the UI framework itself

See this is high on the list of things I do not want. I don't want my UI rendered from scratch in OpenGL, I want it drawn using the native widget API, which already supports embedding 3D canvases.

This is one of those things that is completely trivial in, say, Cocoa, but arbitrarily hard in most cross-platform UI toolkits.


Sure, but that's just because Apple already did the work, and they only have to support Metal and to a limited extent OpenGL anyway.

On a Windows desktop, you have D3D11 and below, D3D12, Vulkan, and OpenGL. I believe you can do most of it if you're using GDI, but even Microsoft's own myriad of UI frameworks have spotty support.


Windows users do not want you to use the system API (indeed most preinstalled apps don't) and Linux users don't have one.

I hate this. We have a whole operating system doing its absolute best to prioritize hardware resources. Don't be out here throttling yourself.

Especially for something like compiling shaders. It literally never happens unless the user is about to go play a game, which is going to consume most resources anyway. To the user, there's nothing more important running right now.


But why use that metric. The OS already has support for this.

Why not just re-nice the process or whatever equivalent for the threads that do the compiling? Tell the kernel what’s going on, it will handle it for you.

Don’t hack your own solution.


My motherboard crashes when I peg all the cores. Presumably it can't maintain the core voltage under load. Vendors need to guard against these scenarios by being extra cautious because it's a hard problem to diagnose that they'll get blamed for.

Your computer is unstable and broken. I would try swapping PSU or troubleshooting that issue. This is not normal and no, this is not the job of anyone else to solve or consider.

Yeah I had this problem and it ended up being the PSU. I tried a lot of different bios and driver versions swapped cpu, memory, then eventually upgraded motherboard with new cpu and memory (all these are the suggestions from people who had the same bios code on forums) and it kept coming back… then finally replaced of the PSU which actually fixed it.

Don’t be like me! Replace the PSU! Do the math on how much your system could possibly need and then double it!


Checking the voltage and specifically which voltage the automatic overclock mode of the board applies though could also be worth the time, before replacing the psu otherwise.

And doubling the wattage isn't necessary, just be comfortably higher on the 12v line and get a good one.


Guarding against that seems like a bad idea, but even then just use a couple less threads than physical cores. Don't get too clever.

You in particular should probably disable a couple cores until your hardware stops being defective. Have you looked into updates and voltage droop compensation settings?


That sounds like a faulty motherboard. Return it.

Users don't care that it's a problem with the motherboard. They have a working computer, they buy a game and it doesn't work, they blame and return the game.

Users can be unreasonable, but in this case I agree - if I reported an issue to a vendor and their response was "return your motherboard" I wouldn't work with that vendor again.


> in this case I agree - if I reported an issue to a vendor and their response was "return your motherboard" I wouldn't work with that vendor again.

Why would you say this? If the problem is your motherboard is in fact broken (or you have used a high-end CPU on a motherboard not rated to power it), and the vendor correctly diagnoses it despite it not being their problem, you think this is bad? You want them to change their software, making it worse for everyone who doesn't have broken hardware, just to make your broken hardware work?

Sometimes if such an issue affects a common hardware configuration it can make sense from a capitalistic sense to "make it work" for more users but if that's the case that implies you would know what the hardware configuration involves and test accordingly so the workaround would not have negative effects on users who don't need it, and either way that's not the situation we're talking about here where the user has already acknowledged that their hardware is wonky.


>everyone who doesn't have broken hardware

To be fair, such people don't seem to exist in real life. Between i586 and zen3, I have never owned a machine that boots the current kernel tip without there being one or more notes about some workaround in force, to deal with some detected broken-ness of hardware or firmware embedded therein.


I covered that though. Known design defects in specific pieces of hardware can be documented, detected, and handled. The user that set off this specific subthread and the user I replied to both took a position that software developers should throttle EVERYONE just because they or someone might have defective or incompatible hardware that can't handle being maxed out.

It certainly isn't a bad idea for developers of software that's likely to max out someone's system to provide options for throttling it, both for those who know they have bad hardware to work around those issues and for those who just aren't in a hurry and would rather trade a slower render/compile/whatever for a system that remains more responsive as an interactive user. It'd be insane and idiotic however to throttle by default.


We can't form the world around literal bad hardware. That's completely insane. The world would fall apart.

> I wouldn't work with that vendor again.

Possibly rightly so, if it's bad motherboard hardware. Could also be a bad power supply, bad ram, bad CPU, user misconfiguration, etc. It's a complex system, where everything has to NOT be bad for it to be deterministic.

There's a necessary level of competence required with self built systems, or any other task in human life that you're not paying someone else to do for you. That's fine. Go buy a MacBook or a pre built system if that threshold of competence can't be met. That's why they exist, for people that can't or don't want to bother.


It doesn't matter what's "fair". It matters what people do.

https://devblogs.microsoft.com/oldnewthing/20080206-00/?p=23...


Haha, that really depends. If you look at it from the viewpoint of a failsafe, like brakes in lifts that activate without central control, backup parachutes, etc. Then it makes some sense for resource intensive software to guard against weird edge cases like this. It's just that they measured the wrong thing.

We don't use elevator brakes or backup parachutes as an excuse to let the primary fail under normal load. Those are for emergencies.

I think the elevator analogy to this bug is that the elevator goes too fast and triggers the brakes if you input target floors more than 8 apart. You don't fix that by having the user or user software be more careful about button placement, you fix the hardware/firmware.


Those have nothing to do with the required assumption of determinism in the hardware that software must enjoy.

You're examples are flawed. Those are to safely halt operation on failure. What you're suggesting with the software is to limp along pretending everything is ok.

The proper test, more in line with your others, would be to catch failure and halt operation. Since, in your case c the human isn't competent enough for that, it would maybe be a self test initiated by the operating system, then a refusal to continue when the hardware problem is identified, emulating all the periodic inspections that are part of all the proper operation of all the physical systems your mentioned.


Ffs Apple's touchscreen keyboard is just abysmal these days. My apologies, it's too late to edit that mess.

... but is hardware deterministic?

That's a trick question as stated, and not really relevant to the discussion of if the end user software should put in safeguards for bad hardware. ;p

There's very very rarely cost pressure that allows non-determinism inside of the operating parameters, and these almost always involve radiation or gamma rays (which many of us, outside the banking industry, pretend aren't relevant) or some "impossibility", like the corruption of data that results on the same checksum.


Sounds like they don't have a working computer. Computer users occasionally do things that end up pegging all cores, so I'm pretty sure that someone in that case would know their computer has some kind of issue.

They'd probably be quite happy not to work with somebody that makes their broken hardware everybody else's problem, though.

Expecting software to deal with a user that can't be bothered to fix a pretty fundamental flaw with their machine is unreasonable.


Why aren't you storing all your variables with reed solomon encoding in case you have a user with bad ram?

Disable turbo boost in the bios

That seems like a you problem - undervolt your cpu.

It’s just reality that motherboards can malfunction or degrade in peculiar ways that a manufacturer’s quick boot up checks cant always catch. No matter the reliability claims.

If they want to cater to customers who foolishly reject reality and take every claim from manufacturer’s at face value… then foolishly misaimed blaming seems expected?

But if they just roll over and swallow the foolish blaming without even challenging customers to double check, then they are also facilitating more nonsense.


"I have a faulty hardware so everyone in the world should take care not to trip my hardware. Also I don't care about anyone else."

> To the user, there's nothing more important running right now.

One special case: game streamers would likely rather the game stutters than the stream encoding stutters. (Especially as long as the capture of their own face+voice remains smooth.)


Especially on Linux, the default throttling just doesn't work very well.

> To the user, there's nothing more important running right now.

Well, to a certain degree. I have a relatively uncommon GPU, and run games on Steam (and Linux), so literally every game does shader compilation for me. Even with a beefy CPU and beefy GPU, if the shader compilation uses more than 16 threads, my computer basically freezes until its done with it, as it's so performance intensive, kind of have to throttle things if I want to be able to use the desktop for the 10 minutes the shader compilation takes sometimes.

Edit: "Do you use Linux? That only happened to me when I was using a dram-less ssd for swap and doing IO heavy operations on the NFS mounted drive"

I do! I do have NFS drives, but not for Steam games or anything else relevant. I'm also not using swap at all. RAM seems fine during this, it's just pegging all cores to 100% usage immediately and stays there, making everything else suffer.


> Even with a beefy CPU and beefy GPU, if the shader compilation uses more than 16 threads, my computer basically freezes until its done with it

Strange. This never happens to me on Windows. I only notice that the fans spin up a little because all cores are pegged.


i've only seen the shader compilation on linux.

Do you use Linux? That only happened to me when I was using a dram-less ssd for swap and doing IO heavy operations on the NFS mounted drive, since the system will use ram as a buffer (which pushes things to swap)

My steam machine is netboot diskless nfs only.(probably a mistake, but I have a nice NAS and so leaned hard into it) However, having no swap worries me. for openbsd they document how to swap to nfs. But the linux docs say not to do that. What is the best practice here? swap to an iscsi block?

zram+earlyoom sounds like the right answer there. Compressed ram works as the low memory signal

fedora45/proton/steam user.

What you are describing is not normal. Which GPU? I have not seen this on Nvidia 4060/4070, or AMD 580, 6600, or 9060/9070.


Well, as mentioned, the hardware is relatively uncommon; 9970X + Pro 6000 GPU.

> Even with a beefy CPU and beefy GPU, if the shader compilation uses more than 16 threads, my computer basically freezes

Sounds like a scheduling problem that should be adressed at the OS level.


Seems a deep one if so. I've tried a bunch of alternative schedulers to try to avoid the issue, like Bore and BMQ, but seems to happen regardless. Been like this on previous computers too, fwiw, unless I turn down the concurrency for shader compilation stuff will lag heavily until it finishes.

> tried a bunch of alternative schedulers

1. Have you tried enabling full preemption as well? I run Debian, and before I did that I had lots of stutters from any kind of background load thanks to dynamic preemption.

2. Even then, ZFS was causing stutters until I lowered it's priority as well (spl_taskq_thread_priority=0)

3. On non-ZFS drives, try setting your I/O scheduler to Kyber for NVME or BFQ for everything else. "mq-deadline" isn't great for interactive use on a SATA SSD, even for non-boot drives, and even NVME sometimes stutters under "none" for some people.

> I'm also not using swap at all.

4. zswap (combined with zstd compression, "min_ttl_ms=1000", and "vm.page-cluster=0") and "defer+madvise" memory compaction can help. (Something about the kernel really liking having swap on-hand.)

Steam runs shader compilations at batch policy and nice 19, so without RAM or I/O bottlnecks even gaming shouldn't be too bad (though from personal experience it still induces some stuttering). It's just most distros are tuned horribly for this. (Not to sound flippant, I understand the frustration of "just try this" and it doesn't work, but it's everything I can think of RN.)


Many thanks for all the things to try, definitely will take a closer look and explore what you've mentioned next time it happens! :)

When you do, I forgot to mention I had difficulty getting a couple of settings to stick on reboot. Some had "proper" methods that were bugged, others seemingly needed a startup script anyways, so I just stuck those two groups into one:

    echo zstd > /sys/module/zswap/parameters/compressor
    echo 0 > /sys/module/spl/parameters/spl_taskq_thread_priority
    echo 1000 > /sys/kernel/mm/lru_gen/min_ttl_ms
    echo defer+madvise > /sys/kernel/mm/transparent_hugepage/defrag
...and some quick-n-dirty scripting[1] for ZFS drives.

Fellow HNer 'networked' pointed out[3] that the compressor type was because Debian wasn't loading the zstd module on startup for some reason, so you could fix it that way as well. Dunno your distro, hopefully you don't need these hacky workarounds that continue to annoy me.

[1]https://www.reddit.com/r/zfs/comments/1qerlvn/how_to_only_se...

[context]https://www.reddit.com/r/linuxquestions/comments/1qenr06/id_... and https://wiki.archlinux.org/title/Improving_performance#Kerne...

[3]https://news.ycombinator.com/item?id=47505661

[4]https://openzfs.github.io/openzfs-docs/Performance%20and%20T...

[5]https://wiki.archlinux.org/title/Zswap


You could try cpu pinning or ulimits to limit the program from using one core so there's always one free.

But it's also possible that the problem is in the kernel and the shaders are actually doing some system call that's locking up the kernel. Like maybe there's a bug in your cuda or nvidia driver that's causing the kernel to lock.


I'm actually ok with a blocking progress bar. The whole async approach falls apart once it starts degrading performance.

The upside is that I never get any spam or scammers on Signal, like we see on all other messaging platforms.

Maybe it's just because it's less widespread where I am, but nevertheless.


I never get spam on messages either which is e2e encrypted service you can register with mail alone.

> Why did you publish anything, if you didn't want it to be used by third parties?

Because 3 years ago, everyone had a reasonable expectation that any publicly available work would be appropriately credited and/or compensated for, in accordance with both copyright law and open source software licenses.

You realize that even very permissive software licenses, like MIT or Apache-2.0, still require attribution? Now a few companies are making big bucks selling your ideas (and sometimes verbatim code) without adhering to the terms of that license.

The social contract is utterly broken. The legal situation is utterly questionable. The only reason they get away with it is that the inmates are in charge of the asylum in the US.


> The only reason they get away with it is that the inmates are in charge of the asylum in the US.

Wait, because a party you don't like got elected, the whole judical system is also broken? I find that hard to believe.


I'm European. Describing the Republican Party as "a party [I] don't like" is unbelievably out of touch. The entire world outside of the US sees and says plainly what's happening. And yeah, seen from the outside, the US justice system sure does seem broken in a number of ways.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: