4 ms·
Please get rid of GC :( I want to have smart pointers instead
by joshsyn 10y ago
Please get rid of GC :(
I want to have smart pointers instead
- qznc 10y agoNo problem, just add the @nogc annotation to your main function. int main(string[] args) @nogc { ... }
- Rusky 10y agoI haven't used D much- how much of an impact does that have when using the standard library or other libraries? Is there a good way to use things that assume a GC even when it's disabled?
- bionsuba 10y agoThere are many parts of the std lib that assume GC usage. When you mark a function as @nogc, DMD does a compile-time check and will not compile your program if there's a GC call. Applying this to main means that your entire program will by necessity be GC free.
- Rusky 10y agoRight, I understand that it effectively eliminates GC usage, I'm just wondering how much of an impact that has on what tools you have. In the extreme, I can imagine it degenerating into basically writing C, for example.
- bionsuba 10y agoYou have access to all of std.range and std.algorithm, most of std.datetime, many functions in std.string and std.digest, and others scattered throughout the std lib. The main thing you'll want to look at is std.experimental.allocator, which has tons of tools for manual memory allocation. Most of the community packages out there use the GC. Some though are transitioning to a std.experimental.allocator interface, like this containers library https://github.com/economicmodeling/containers https://github.com/economicmodeling/containers
- kaleidic 10y agoSee Sociomantic 's Ocean library. But you don't need to go that far in practice in most uses. If you keep GC heap small, disable the GC in critical code paths, have threads that the GC doesn't get involved in, and use allocators most people will be fine.
- vardump 10y agoSmart pointers are a form of garbage collection. (I'm assuming you mean reference counting).
- steveklabnik 10y agoNot all smart pointers are reference counted.
- vardump 10y agoSure, smart pointers can also refer to bounds checking, etc. In this case, grandparent did mention a memory management technique, namely garbage collection. Memory management + plus typically required thread safety usually means reference counting.
- steveklabnik 10y agouniq_ptr<T> in C++ and Box<T> in Rust are both considered smart pointers, dealing with memory management, and don't do any reference counting, bounds checking, or anything else like that at runtime. They're a compile-time abstraction only.
- vardump 10y agoGood point. Somehow I wasn't considering unique_ptr<T> as a smart pointer, despite using it all the time for resource management to avoid writing wrapper classes. I do now, you're right.
- rbehrends 10y agoThis is not really accurate. A unique_ptr<T> move assignment must check the target location to see if it's null, and if not, free the underlying object. It also has to clear the source location. This is more than just a compile-time abstraction compared to raw pointers, even if modern compilers can elide the code most of the time. Also, the lack of checking leads to unique_ptr<T> dereferences potentially having undefined behavior (i.e. whenever you try to dereference a unique pointer that is null [1]). [1] http://en.cppreference.com/w/cpp/memory/unique_ptr/operator* http://en.cppreference.com/w/cpp/memory/unique_ptr/operator*
- robochat 10y agoActually, please don't get rid of the GC. The GC makes programming easier and there is rust now if a programmer believes that they can not possibly tolerate one. Complaining about the GC is like complaining about python's white space, lisp's parentheses, go's lack of generics etc. It's demanding that the entire language changes to suit the complainant. However, if you're going to have one and people keep bringing it up, you should work hard to make sure that it is the best that it can be. Golang has been very good in this regard with their very publicized work on reducing the stop the world time of their GC. Personally, I think that it would be a win if Dlang did something similar (in addition to the @nogc stuff). It doesn't need to concentrate on reducing the GC pause time, it just needs to be seen to be getting better.
- joshsyn 10y agoIsn't it possible to have GC as an library rather than a language construct? The way I see it, GC should just belong as deferred_ptr