News for hackers. Stuff that matters.
Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @10:35AM (#23917452)
Don’t, is always the simplest answer. Global state that is non-constant, causes all sorts of testing issues down the line. It should be avoided.

Edit: since a lot of folks are reading this as saying “never” rather than “avoided”. The point here is to keep access to data and state restricted. The entire application does not need to know that some piece of data is global. If you design a central container in a generic manner, it can restrict access to globals in a way that allows for easier refactoring in the future. It also allows one to design users of those globals in a way that doesn’t leak the fact that these things are global.

By doing this, you don’t leak globals through the application, and you keep things isolated in a way that allows for easier testing.

Re:Rust – How do I create a global, mutable singleton? (2015) by _28jh on Wednesday July 22, 2020 @10:51AM (#23917649)
I hate when I ask something about globals on SO and I get the same blanket answer. Why does every programmer feel the need to tell me that "globals make it hard to reason about the state of your program?" Unfortunately most systems are global based. OpenGL has one global state. A database connection is a global. Either you can use globals to model these things properly or you can complicate it by not using globals. The library will be using globals under the hood anyways.
Re:Rust – How do I create a global, mutable singleton? (2015) by cesarb on Wednesday July 22, 2020 @11:10AM (#23917840)
> OpenGL has one global state.

As far as I know, it doesn't, it's actually thread-local state; you can have multiple states, and you use glXMakeCurrent/eglMakeCurrent to choose which one is active for the current thread.

And the protocol which can be considered the successor of OpenGL (Vulkan) replaced the thread-local state with an explicit reference to the state.

> A database connection is a global.

The product I develop at work uses multiple database connections, to several different databases, which can be added and removed at runtime. They most definitely aren't global.

Re:Rust – How do I create a global, mutable singleton? (2015) by AnIdiotOnTheNet on Wednesday July 22, 2020 @11:06AM (#23917803)
Because that's the academic answer that doesn't have to deal with real world constraints and pressures. I too have a deep hatred of the "you don't want to do that" answer for this reason: I do deal with real world constraints and pressures and I'm asking my question because "the right way" can't be done in my circumstances!
Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @11:11AM (#23917858)
To quote myself, “it should be avoided”, doesn’t mean never.

DB connections as global state, I might go as far as saying global for a single thread, ie it might be reasonable to have a thread-local-storage for checked out connections. I still would keep access to that thread-local restricted, and keep most of the code more testable by having the connection be a parameter to each function.

Re:Rust – How do I create a global, mutable singleton? (2015) by kstrauser on Wednesday July 22, 2020 @01:26PM (#23919379)
I disagree with that in general (but certainly won't argue against certain very specific use cases). Being diligent about testing has made me loathe global objects; it's so much easier to write a test for a function that makes a database query when I can pass in a mock database object, rather than having to patch the environment to make that same function use a global mock. If you can get to the point that almost all tested functions operate only on things passed into them as arguments, some previously difficult things can become almost trivial.

I don't dislike global state because of some lofty goals I memorized back in college and have held up as supreme truth, but because practical experience tells me that it makes my life as a programmer more complicated than it has to be.

Re:Rust – How do I create a global, mutable singleton? (2015) by hedora on Wednesday July 22, 2020 @11:17AM (#23917915)
What happens if you need to use your code to migrate from one database to another, but all the low level logic uses a single global database connection?

How do you test that your program behaves the same way with a reference software renderer and the GPU if there’s only one global handle to the video card?

In both cases, how do you add more threads without creating a global lock?

Re:Rust – How do I create a global, mutable singleton? (2015) by simion314 on Wednesday July 22, 2020 @11:26AM (#23918019)
There are cases where you only have 1 instance of something, never 2 or more. Most developers use such things dailyu, like in JS you have the window object, most of the time you don't pass the window as a parameter in all your functions but in one project I worked it was a requirement to pass the document as a param in all functions that needed it. So the my opinion is that it depends, sometimes having a World,App,Window etc global helps more then it causes issues and is preferable to a solution that is pure that costs you more. FYI I use a functional style as much as possible, no side effects but sometimes pragmatism wins, in my current project I guarantee we won't change our database or upgrade the current framework to a new cooler one.
Re:Rust – How do I create a global, mutable singleton? (2015) by estebank on Wednesday July 22, 2020 @12:36PM (#23918787)
> There are cases where you only have 1 instance of something, never 2 or more.

I've seen plenty of cases where this assumption stops being true and only through pain and sweat the design could be disentangled after the fact. On the other hand I've never been more than mildly inconvenienced by in practice global state that had an API that didn't assume it.

Writing the logic as if it could be more than one at a time also makes it much easier to write isolated tests and reduced integration tests.

I have always seen singleton designs in codebases where testing was wanting.

Re:Rust – How do I create a global, mutable singleton? (2015) by simion314 on Wednesday July 22, 2020 @01:47PM (#23919608)
As I said it depends, do you pass in js the Math object around because you assume that in future you want to replace the Math.abs function with something different ?

Sure I seen the issues you mentioned too and many other issues where the initial requirements or assumptions changed.

Re:Rust – How do I create a global, mutable singleton? (2015) by swsieber on Wednesday July 22, 2020 @11:01AM (#23917756)
The OpenGL is a much better example than the database connection, FWIW.
Re:Rust – How do I create a global, mutable singleton? (2015) by TheCoelacanth on Wednesday July 22, 2020 @01:34PM (#23919461)
In what sense is a DB connection global? You can easily have multiple DB connections in one program.
Re:Rust – How do I create a global, mutable singleton? (2015) by logicslave on Wednesday July 22, 2020 @11:04AM (#23917784)
Its dogma. Like a religion. A whole generation of people believing in things with no root evidence except that the knowledge is ubiquitous
Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @11:20AM (#23917942)
I have plenty of history with global state, various contexts in large applications, that make testing code unnecessarily complex.

So, yes, global state is sometimes necessary, but it absolutely should be avoided, and access to it should be restricted.

Re:Rust – How do I create a global, mutable singleton? (2015) by aphextron on Wednesday July 22, 2020 @10:44AM (#23917560)
It's not a simple yes/no question. For instance, is a database not ultimately a single mutable global state?
Re:Rust – How do I create a global, mutable singleton? (2015) by chousuke on Wednesday July 22, 2020 @11:05AM (#23917794)
Databases are also designed for safe concurrent access with enforceable constraints on data. Would you like your application behaviour to depend on a transient "database" someone made up on the fly? :)

I think the key is that your database stores data that lives outside your application, making it observable outside any particular application instance and allowing you to perform ad-hoc queries to validate consistency and fix problems. If you have such mutable state inside a process, it can be very difficult to debug problems when a series of interactions sends your process into an inconsistent state that is only detected some hours later when (if) it finally crashes.

Re:Rust – How do I create a global, mutable singleton? (2015) by baq on Wednesday July 22, 2020 @10:55AM (#23917699)
A database connection perhaps, but different connections to the same database will see different states of the database depending on the isolation level of the current transaction.
Re:Rust – How do I create a global, mutable singleton? (2015) by dleslie on Wednesday July 22, 2020 @11:08AM (#23917828)
The hardware itself is implicit global state, and often with devices that behave like singletons.
Re:Rust – How do I create a global, mutable singleton? (2015) by kjeetgill on Wednesday July 22, 2020 @12:00PM (#23918408)
This is very nearly the exact advice I would give a person working on my team. But I also think when the question is asked broadly it can be ... Presumptuous. Even though I've come to the same conclusions from my experience just discarding the premise of the question (without answering it especially) won't sit will.
Re:Rust – How do I create a global, mutable singleton? (2015) by Const-me on Wednesday July 22, 2020 @01:16PM (#23919262)
The OP’s question was about OpenGL. GPUs have gigabytes of external mutable state. Specifically, in my PC the figure is “11GB of that state”.

You can’t avoid that.

If a language doesn’t offer convenient ways to deal with that state, it simply means that language ain’t a good choice for anything GPU-related, be it graphics or GPGPU.

Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @03:57PM (#23920918)
What do you by convenient?

There is an extremely good response to how to design this in Rust on the SO post. This doesn't change the way that you should design the rest of the code.

The convenience that Rust gives you in this context is strong ownership (i.e. can limit access to the Global except in very desirable patterns) and data-race free with compile time checks around mutability.

The standard library has all of these options available, additionally there are a bunch of libraries from the ecosystem that make it even easier.

Re:Rust – How do I create a global, mutable singleton? (2015) by Const-me on Wednesday July 22, 2020 @04:33PM (#23921258)
> can limit access to the Global except in very desirable patterns

It’s desirable when you don’t do much I/O, only implementing some algorithms that compute something.

It’s not desirable at all when the majority of your complexity is I/O. An extreme example is a videogame, these can update global mutable state (the VRAM, abstracted behind GL or D3D) with the bandwidth measured in gigabytes/second, often concurrently so from multiple threads.

Update: and even for computations, when you want performance, rust’s ownership shenanigans often get in the way. See this C++ code for example: https://github.com/Const-me/SimdPrecisionTest/blob/master/rc... You gonna have hard time porting that to Rust, because this vector https://github.com/Const-me/SimdPrecisionTest/blob/master/rc... is updated concurrently by multiple threads of the pool, without any locks.

Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @05:41PM (#23921928)
You're comparing an implicitly unsafe language implementation, C++, with a language that guarantees safety by default, Rust. There are many people working on non-blocking lock-free datastructures in Rust, but this often requires unsafe implementations. Essentially removing the safety restrictions to do things like what you're showing in the C++.

Looking at the code you're showing, I might model this with deque from the Crossbeam project: https://docs.rs/crossbeam/0.7.3/crossbeam/deque/index.html

I believe that's all lock-free, and offers a safe interface for working with it.

Re:Rust – How do I create a global, mutable singleton? (2015) by Const-me on Wednesday July 22, 2020 @06:13PM (#23922183)
I have no doubts it’s possible to replicate the functionality in Rust. Most languages are Turing-complete, after all. What I have doubts about is performance parts, i.e. replicating the functionality without moving data from a single std::vector into randomly-allocated heap objects, or using synchronization primitives.

Another thing is usability, and especially readability. OpenMP C++ code is very readable, it’s just a for loop. The example on the page you have linked, on the other hand…

And by the way, when I want memory safety, I don’t code C++, I use C# instead. Much easier to use than Rust, IDE and libraries are awesome, C interop is same as in Rust, and last but not least even very complex software compiles in a few seconds. Despite what most people think it’s very suitable for lower-level system software, including latency sensitive stuff working on slow ARM CPUs, see this for an example: https://github.com/Const-me/Vrmac/tree/master/VrmacVideo

Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @06:33PM (#23922309)
Readability is probably always going to be a very personal thing as it has a lot to do with familiarity. Having worked with all the languages you mention, I don't find Rust to be any less readable, and definitely enjoy reading it more than C or C++.

I get how coming from C or C++, deciding that it's not worth turning to a new language in this space that meets the same requirements. I made that same choice years ago when I switched to Java primarily for memory safety. This is no longer a trade-off you need to make with Rust (i.e. turning to a garbage collector), and that's one of the reasons that I've enjoyed it so much. Obviously, YMMV.

Re:Rust – How do I create a global, mutable singleton? (2015) by Const-me on Wednesday July 22, 2020 @06:54PM (#23922454)
> This is no longer a trade-off you need to make with Rust

I don’t need to make that trade-off either. In the software I’m working on, I often use C# for less performance-critical parts where I want memory safety, and calling C++ DLLs for code which needs to be unsafe for some good reasons.

Apparently, Java had a design goal to force people to code their entire software in Java. C# didn’t, it was designed for native interop from the very beginning, hence value types, real stack, DllImport, unsafe, pointer arithmetic, delegates compatible with C function pointers, HRESULT status codes of exceptions, and so on. At some point I have even made COM interop for Linux: https://github.com/Const-me/ComLightInterop It’s a small project with less than 5k lines of code, because all the heavy lifting was already in the .NET runtime.

Re:Rust – How do I create a global, mutable singleton? (2015) by bluejekyll on Wednesday July 22, 2020 @08:25PM (#23922980)
That is a great point about C#, C FFI is very simple as compared to Java.

Though, you are incurring the cost of C#'s GC for the rest of the program. But, even in that case, if you're using C/C++ then all of that is unsafe and needs to be proved, where as if you used Rust for that FFI, then you'd have less unsafe surface area to prove.

Re:Rust – How do I create a global, mutable singleton? (2015) by Const-me on Wednesday July 22, 2020 @09:10PM (#23923220)
> incurring the cost of C#'s GC for the rest of the program

With some care, it's possible to code C# without using GC much, or at all. That Linux h264 video player I've linked above doesn't allocate anything on GC while playback, it's mostly using stack (including stack-allocated spans) and very rarely for some mkv files calls .NET equivalents of malloc/free to allocate on unmanaged heap. Otherwise it probably wouldn't work due to GC issues, I think I only buffer like 150ms of uncompressed audio. Total compressed bitrate can exceed 10 mbit/sec, uncompressed is much more, I didn't expect GC to handle that much data well, especially on raspberry pi.

> all of that is unsafe and needs to be proved

It depends on the project.

If you're working on avionics then yes in theory, but in practice you still have to use C because there's no FAA-certified builds of Rust compiler.

If you're working on a videogame, desktop software, or a mobile app, good engineering and management practices are usually enough. Proving would be nice for them too if it would be free, but not at the cost of development time.

> then you'd have less unsafe surface area to prove

I'm not sure it becomes much less. C interop, and COM interop too, works both ways, one can easily call .NET code from C++ in the same process. I don't usually write C++ code for disk I/O, logging, compression, encryption, serialization, instead I'm passing C#-implemented COM interfaces to native code.