Live data from Hacker News

Baby's First Garbage Collector

journal.stuffwithstuff.com

71–80 of 90 posts

Re: Baby's First Garbage Collector

#71
post #17
post #4

Earlier quoted context omitted.

I don't know of any language that has pointers and a GC. Are there any? C# lets you use pointers but only if you tell the GC to not move your object and promise to behave.

> "I don't know of any language that has pointers and a GC. Are there any?" Objective-C used to have a garbage collector, though (at least with regards to iOS) Apple removed this capability with the introduction or ARC. On the following URL is some discussion on the Objective-C garbage collector: http://cocoasamurai.blogspot.nl/2010/12/objective-c-memory-m...

iOS never got the GC, only the desktop version got it, and afaik it was deprecated rather than removed entirely.

Re: Baby's First Garbage Collector

#72
post #17

Earlier quoted context omitted.

> "I don't know of any language that has pointers and a GC. Are there any?" Objective-C used to have a garbage collector, though (at least with regards to iOS) Apple removed this capability with the introduction or ARC. On the following URL is some discussion on the Objective-C garbage collector: http://cocoasamurai.blogspot.nl/2010/12/objective-c-memory-m...

iOS never got the GC, only the desktop version got it, and afaik it was deprecated rather than removed entirely.

It's been removed entirely in Xcode 5.1.

Re: Baby's First Garbage Collector

#74
post #49

Earlier quoted context omitted.

I think `*object = unreached->next;` does that, since it's using a pointer to a pointer.

That's correct. It patches the "next" pointer of the previous object to point past the freed object. It's a pointer to a pointer to handle the case where you're freeing the first object in the list. In that case, it's "firstObject" that needs to be modified, not some Object's "next" pointer. Using a pointer to a pointer (while admittedly harder to read at first) lets you handle both of those cases with the same code.

This technique is talked about in this Stack Overflow answer: http://stackoverflow.com/questions/12914917/using-pointers-t..., which also references this Slashdot interview (http://meta.slashdot.org/story/12/10/11/0030249/linus-torval...) with Linus Torvalds where he gives this as his "favorite hack".

Re: Baby's First Garbage Collector

#75
post #21

So every once in a while I come across old timey C optimizations in the spirit of Duff's device or bit twiddling to swap variables, 'etc 'etc... While they have a certain kind of charm to them they seem to be almost universally bested by increasingly mature compilers and complex (or virtualized) hardware. So I'm kind of coming to the conclusion that clever pointer arithmetic games and even manual malloc/free are incr…

I grew up on assembler, cycle counting, and computer graphics. You're right, a lot of the old-timey optimizations are not applicable on today's hardware, but this is just the march of technology. I didn't have to worry about generating graphics scanline-by-scanline like those poor Atari 2600 programmers. They didn't have to worry about instruction pairing on the Pentium though. Each generation has their tricks.

As for 'manual' management, consider script engines that need to temporarily store the AST while compiling/interpreting a piece of script. These typically allocate each tree node by pointer-bumping from a large pre-allocated block of memory, and free the entire thing once done by resetting the pointer. Basically this is the same idea as a nursery in a generational collector, with the difference that you already know beforehand that you're not going to promote any of the elements.

What GC algorithm is best (mark/sweep, generational, soft real-time, hard real-time) depends on your needs I guess. In that light, it's too bad MSR canned Singularity: http://research.microsoft.com/pubs/69431/osr2007_rethinkings...

Re: Baby's First Garbage Collector

#76
post #66

Earlier quoted context omitted.

Depends. Do you want, for example, a browser that pauses for upwards of 50 ms at a time? How about a game? Go's garbage collector can very much result in 50 ms pauses. When The Verge reviews new phones, they definitely dock points for a laggy OS, and dropping three frames in a row regularly is considered laggy these days...

You act like most of those phones aren't Java based. Android's GC is actually pretty nice these days.

But all Android phones are still strictly laggier than the iPhone, and part of that is choice of runtime implementation.

Re: Baby's First Garbage Collector

#77

Earlier quoted context omitted.

I was introduced to programming through python. Now that I'm in school, I've been doing most of my work in C++. I have to say, as much as I like programming in python, my knowledge of programming is made so much better because I've had to write C++. From an educational perspective, I think non-GC languages will always have a place (if only for teaching about computers). In production environments, I don't think that…

I think the parent comment was pointing out that less and less, there's no real reason to need C++. The performance in safer languages is usually far more than adequate, while requiring less code (less bugs) and blocking entire classes of bugs. Even Tim Sweeny of Epic (Unreal Engine) said they'd gladly switch languages to improve productivity. And game engines are one of the few places that still do need to eek out a…

I once read a comment that said that C++ is like a very sharp knife. If you know how to use it, it's safer than a dull knife; if you don't, then you need to have your totin' chip taken away.

I like to compare it to the F4U Corsair: anyone but a talented and experienced pilot at the yoke will lead to certain disaster. But the thing is just so powerful and so absurdly versatile that until something decisively and significantly better comes along it remains a mainstay of any halfway decent air force.

A lot of people confuse being the Corsair of programming languages with being the Corvair of programming languages -- "unsafe at any speed". They're full of it. Their ignorance and lack of experience prevents them from seeing the scenarios in which the utility of C++ is absolutely indispensable.

Re: Baby's First Garbage Collector

#78
post #4

Not having studied the topic in depth, my first thought is whether true pointers and a garbage collector can coexist (without artificially removing parts of the language). The big condition for knowing if something is still in use is whether any references exist to it. But in a language like C , you could cast a pointer to something else (like a void pointer for some quasi generic linked list), store it somewhere, an…

I don't know of any language that has pointers and a GC. Are there any? C# lets you use pointers but only if you tell the GC to not move your object and promise to behave.

Various Lisp dialects over the years have supported the use of locatives, or pointer-like objects.

In general, objects which can be moved or copied around by the runtime behind the programmer's back cannot have indefinite-extent hardware-address pointers taken directly into them. This includes objects managed by most halfway-decent garbage collectors (which tend to be implemented as stop-and-copy, not mark-and-sweep), but also includes such things as drawing surfaces in SDL and DirectDraw. Many runtimes take the C# approach -- provide a memory locking operation that fixes the object in memory while you do some pointer stuff on it and a dual unlocking operation that releases the object so it can be kicked around by the runtime again.

Re: Baby's First Garbage Collector

#79
post #77

Earlier quoted context omitted.

I think the parent comment was pointing out that less and less, there's no real reason to need C++. The performance in safer languages is usually far more than adequate, while requiring less code (less bugs) and blocking entire classes of bugs. Even Tim Sweeny of Epic (Unreal Engine) said they'd gladly switch languages to improve productivity. And game engines are one of the few places that still do need to eek out a…

I once read a comment that said that C++ is like a very sharp knife. If you know how to use it, it's safer than a dull knife; if you don't, then you need to have your totin' chip taken away. I like to compare it to the F4U Corsair: anyone but a talented and experienced pilot at the yoke will lead to certain disaster. But the thing is just so powerful and so absurdly versatile that until something decisively and signi…

With C++ you trade a bit better performance for a truckload of complexity, busy work and unsafety. Whether that trade-off is worth it depends on the application, but the area in which this is a good trade-off is getting smaller and smaller. The analogy with the knife doesn't make much sense. A sharp knife is safer because you have to apply less pressure, and muscles are more precise and controlled when applying less pressure. C++ is the opposite.

Re: Baby's First Garbage Collector

#80
post #21

So every once in a while I come across old timey C optimizations in the spirit of Duff's device or bit twiddling to swap variables, 'etc 'etc... While they have a certain kind of charm to them they seem to be almost universally bested by increasingly mature compilers and complex (or virtualized) hardware. So I'm kind of coming to the conclusion that clever pointer arithmetic games and even manual malloc/free are incr…

The optimization tricks have merely changed, instead of the whole concept being redundant.

Nowadays you should aspire to reduce branches as much as possible to keep branch prediction happy. Dependent memory accesses should be kept to a minimum. Loops should be written in a way where autovectorizer can easily convert it to a bunch of SIMD instructions. One should understand what the compiler can optimize and write code that allows it to do it's job as efficiently as possible.

The same principles apply as they did in the ages gone by. There is a place for scripting languages and there is a place for heavily optimized library. It's just that the way to write those heavily optimized libraries has changed a bit. Instead of hand writing assembler one just has to write C in a way that allows the compiler to do it for you.

From a web development point of view this means that in general it's good to use and for the parts that require some oomph it's good to have something really optimized like ImageMagick on background.

From mobile perspective it doesn't really matter in what way is written but you better hope that the basic system is optimized as much as possible. Because that transfers immediately into battery life.

Post reply on HN