Re: Avoiding GC in D and code consistancy
On 12/31/17 7:50 AM, Steven Schveighoffer wrote: Note, you can use a "sink" version of toString as well, and avoid the gc: void toString(void delegate(const(char)[]) sink) @nogc { // use formatValue to push into the sink } I guess I'm missing some parameters here, go with what Seb linked to :) -Steve
Re: Avoiding GC in D and code consistancy
On 12/31/17 6:16 AM, Tim Hsu wrote: On Sunday, 31 December 2017 at 07:32:50 UTC, Ali Çehreli wrote: On 12/30/2017 11:16 PM, Tim Hsu wrote: > Struct version of Vector3f can't derive toString > method. writeln() prints unformated struct members. I know I can use > helper function here. But is there any other way? The normal way that I know is to insert a function like the following into Vector3f: string toString() { import std.string : format; return format("%s,%s,%s", x, y, z); } > class version of Vector3f. Require new operator in opBinary(). scoped! > won't work here. > > Is there a better to write vector3f class while avoiding GC? Yeah, it doesn't make sense that a type of x, y, z should be a class. I would stay with a struct here. Sorry I am a bit disappointed. It seems writeln itself will check if the struct to be printed has toString. If not, it use default struct printer. Note, you can use a "sink" version of toString as well, and avoid the gc: void toString(void delegate(const(char)[]) sink) @nogc { // use formatValue to push into the sink } -Steve
Re: Avoiding GC in D and code consistancy
On Sunday, 31 December 2017 at 07:16:46 UTC, Tim Hsu wrote: I came from C++ looking forward to D. Some languages require programmers to use GC all the time. However, A lot of time we don't really need GC especially when the time of destruction is deterministic in compile time. [...] You can use a custom toString method which doesn't do any allocations: https://wiki.dlang.org/Defining_custom_print_format_specifiers However, the building blocks (formattedWrite and writeln) aren't "@nogc" at the moment.
Re: Avoiding GC in D and code consistancy
On Sunday, 31 December 2017 at 07:32:50 UTC, Ali Çehreli wrote: On 12/30/2017 11:16 PM, Tim Hsu wrote: > Struct version of Vector3f can't derive toString > method. writeln() prints unformated struct members. I know I can use > helper function here. But is there any other way? The normal way that I know is to insert a function like the following into Vector3f: string toString() { import std.string : format; return format("%s,%s,%s", x, y, z); } > class version of Vector3f. Require new operator in opBinary(). scoped! > won't work here. > > Is there a better to write vector3f class while avoiding GC? Yeah, it doesn't make sense that a type of x, y, z should be a class. I would stay with a struct here. Ali Sorry I am a bit disappointed. It seems writeln itself will check if the struct to be printed has toString. If not, it use default struct printer.
Re: Avoiding GC in D and code consistancy
On 12/30/2017 11:16 PM, Tim Hsu wrote: > Struct version of Vector3f can't derive toString > method. writeln() prints unformated struct members. I know I can use > helper function here. But is there any other way? The normal way that I know is to insert a function like the following into Vector3f: string toString() { import std.string : format; return format("%s,%s,%s", x, y, z); } > class version of Vector3f. Require new operator in opBinary(). scoped! > won't work here. > > Is there a better to write vector3f class while avoiding GC? Yeah, it doesn't make sense that a type of x, y, z should be a class. I would stay with a struct here. Ali
Re: Avoiding GC
On Friday, 28 October 2016 at 11:50:20 UTC, hardreset wrote: On Thursday, 27 October 2016 at 07:52:09 UTC, Guillaume Piolat wrote: On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote: Is there a page somewhere on how to program D without using the GC? The information is scattered. How do I allocate / free structs / classes on the heap manually? Classes => https://github.com/AuburnSounds/dplug/blob/master/core/dplug/core/nogc.d#L122 Thanks. I notice you avoid GC altogether in dplug. Whats the reason for total avoidance as apposed to just avoiding it in the real time code? Not a lot of reason. It's very recent work, I'm still struggling making threadpool works. - Reason #1 was that it gives a way to unload shared libraries on OSX. This bug has been fixed in LDC but would require to make druntime and phobos a shared library to ship. That makes releases 3x larger so I went with disabling the runtime instead (one month of work and still going...). - Reason #2 is that GC does use more memory. Next release of our products use 2x fewer memory. All in all it's _painful_ not to use the D runtime, suddenly you can't use third-party code, and there is no performance enhancement to expect apart from reduced memory usage. Don't avoid the runtime on principles alone.
Re: Avoiding GC
On Thursday, 27 October 2016 at 07:52:09 UTC, Guillaume Piolat wrote: On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote: Is there a page somewhere on how to program D without using the GC? The information is scattered. How do I allocate / free structs / classes on the heap manually? Classes => https://github.com/AuburnSounds/dplug/blob/master/core/dplug/core/nogc.d#L122 Thanks. I notice you avoid GC altogether in dplug. Whats the reason for total avoidance as apposed to just avoiding it in the real time code?
Re: Avoiding GC
On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote: Is there a page somewhere on how to program D without using the GC? The information is scattered. How do I allocate / free structs / classes on the heap manually? Classes => https://github.com/AuburnSounds/dplug/blob/master/core/dplug/core/nogc.d#L122 New would be GCed memeory wouldnt it? Yes. Delete is being depreciated? I don't think you ever want delete when there is .destroy
Re: Avoiding GC
On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote: Is there a page somewhere on how to program D without using the GC? How do I allocate / free structs / classes on the heap manually? New would be GCed memeory wouldnt it? Delete is being depreciated? thanks. There is the following: https://wiki.dlang.org/Memory_Management
Re: Avoiding GC
On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote: Is there a page somewhere on how to program D without using the GC? How do I allocate / free structs / classes on the heap manually? New would be GCed memeory wouldnt it? Delete is being depreciated? thanks. Probably you want to read: https://dlang.org/phobos/std_experimental_allocator.html Anyway GC works really good for common software development.