4 ms·
Immediately as I started reading this, my mind said, "random() has global shared state so of course more threads will make it slower". I say this here not to at
by kelnos 2y ago
Immediately as I started reading this, my mind said, "random() has global shared state so of course more threads will make it slower". I say this here not to attempt to demonstrate my oh so special advanced knowledge, but just to marvel how bad our abstractions are sometimes. Maybe "bad" is the wrong word. Leaky, inadequate, inscrutable.
"Give me a random number" feels like it shouldn't require all this overhead and hidden complexity under the hood. But unless you've read a lot about implementation details, or have been bitten by that exact problem before, how would you know? You wouldn't, of course.
- ketralnis 2y agoI love the fancy modern type systems and all of the superpowers that they give you. But this is an area that I feel isn't adequately explored. If you call a function that calls a function that has side effects, or that closes the file descriptor you're using, or could panic, or that needs global shared state, or spins up a thread, or allocates, we don't have a good way to deal with that. There are some partial and underutilised solutions like monadic IO and linear/affine types and, uh, not having panics. I have some (bad) ideas but I think this is a space that's worth playing in.
- Yoric 2y agoAlgebraic effects?
- hnick 2y agoI had the same thought years ago, I think some people have looked into it a little, but nothing popular yet. Imagine if you could look at a function, and know, undeniably - this, and anything it can call, absolutely cannot alter the file system directly or make a network call or spawn a process. Maybe instead of just types, you need to supply capabilities (much like interfaces on classes). Sounds like it could be a real pain. Would make auditing easier though.
- mrkeen 2y ago> alter the file system directly or make a network call or spawn a process. Those three cases are excluded in pure functions.
- hnick 2y agoI've been waiting for functional programming to have its day for more than 20 years. It just seems fundamentally at odds with how most people want to work. You can make individual functions pure in any language, but do many languages enforce this at a core level? Or we have to behave ourselves and hope nothing we call is changed by another programmer later?
- mrkeen 2y ago> I've been waiting for functional programming to have its day for more than 20 years. The tech is there. It sounds more like functional programming has been waiting for you for 20 years. > It just seems fundamentally at odds with how most people want to work. "Most people", or "you"? If you believe in something, stand up for it.
- hnick 2y agoI've used it. I've never had a job using it. Most people haven't.
- 0xDEAFBEAD 2y agoHow about a spawning a separate process and using seccomp, or having the child process run as a different user perhaps? There are a couple of advantages to doing it through the OS. For one thing, the implementation is shared across programming languages. For another thing, it's potentially more secure against an attacker trying to inject code. I guess the disadvantage of doing it at the OS level is that you might have to write OS-specific code.
- arghnoname 2y ago
- fullstackchris 2y agoisnt this the whole point of what rust is trying to accomplish? i havent yet worked with rust but from what i hear they make it very hard (impossible?) to have memory issues and things like deadlocks. sounds great in theory but no idea if this is the reality of using it
- felipellrocha 2y agoIt is, yeah. Which is why when you compile 99/100 times it just works. I don’t think people understand that this is one of Rust’s best features.
- adhamsalama 2y agoRust solves race conditions, but not deadlocks.
- ketralnis 2y agoNo. Rust has no controls for side effects or allocations
- matheusmoreira 2y ago> "Give me a random number" feels like it shouldn't require all this overhead and hidden complexity under the hood. It doesn't. The libc is the real problem. The libc and its global state. Even something as simple as errno creates global state problems just like this one. There are buffers all over the place. Freestanding C is a better language just by virtue of not linking against libc.
- glandium 2y agoAt least in glibc, errno is thread local.
- mike_hock 2y agoHow would anyone come to the conclusion that an API that explicitly allows setting a global seed value without mentioning anything about thread-local state, wouldn't contend for some kind of shared resource?
- bheadmaster 2y agoAs others have already mentioned, the main culprit here is libc and its assumption that it is running in a single-threaded environment. The libc was designed with heavy use of global variables in mind. Modules in C are designed as compilation units with static global variables and exposed functions that operate on them. There's a reason many big projects (and programmers) use their own version of the standard library instead of libc.
- ykonstant 2y agoAre there any good, instructive open source examples of custom libc replacements you could link? Especially ones that circumvent the single-threaded assumption of libc; I'd love to use the code to educate myself.
- sn9 2y agomusl is one of the better ones [0]. None other than John Regehr recommends studying it [1]. [0] https://en.wikipedia.org/wiki/Musl https://en.wikipedia.org/wiki/Musl [1] https://blog.regehr.org/archives/1393 https://blog.regehr.org/archives/1393