News for hackers. Stuff that matters.
Re:Windows NT Kernel Contributor Explains Why Performance is... by seanmcdirmid on Saturday May 11, 2013 @07:20AM (#5690319)
> I always considered frameworks like WPF (or Flex, for that matter) 'intern code' - not that interns necessarily wrote them, but they reek of not-experienced-enough engineers trying to solve problems by writing a bunch of new code, instead of fixing existing code.

This is unfair and unfounded accusation. If you look at the time frame, all the major platform were working on their first hardware accelerated UI toolkits and went through the similar teething problems (Cocoa anyone?). WinForms was a dead end, there was no fixing to do. WPF has turned out well enough, and WinRT has evolved into something very efficient (e.g. by using ref counting rather than GC).

> Plus, say what you want about GDI (there's a lot wrong with it at this point), but it's still a surprisingly efficient and flexible way to do 2D rendering

Not anymore. I get it that some love to use antique APIs and computer systems just for the sake of being retro, but when every computer ships these days with a GPU, using GDI is not even close to pragmatic.

Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Saturday May 11, 2013 @09:18AM (#5690509)
> ...WinRT has evolved into something very efficient (e.g. by using ref counting rather than GC).

Ah you mean by causing cache stalls when doing increments/decrements after each assignment and allowing for cyclic references?

Re:Windows NT Kernel Contributor Explains Why Performance is... by jjoonathan on Saturday May 11, 2013 @11:13AM (#5690826)
If the ref counting is manual or optimized, it doesn't happen after every assignment, only ones which change object ownership. If you're really nuts about avoiding cache hits, you can pack most ref counts into the low bits of the class pointer, since allocations tend to be 16-byte aligned, including allocations for class objects. Assuming you use the object at least once following a change of ownership, the overall cost in cache misses becomes 0. Or you can store the references in a global table with better cache properties (IIRC this is what ObjC does). Or you can rely on the fact that cache lines are typically large enough to grab a few words at a time, so fetching the class pointer will automatically fetch the refcount (again with the assumption that you use objects at least once per assignment). Honestly, I'm having difficulty imagining how cache behavior could become a problem unless you wanted it to.

As for not allowing cycles, I consider that a feature. The headache of memory management doesn't go away with GC. You still have to avoid inadvertently keeping references to objects through the undo stack & so on. Unintentional strong refs creep through GC code just as easily as memory leaks creep through code with manual memory management. Almost universally, I find that GC'd projects large enough to have memory management best practices implicitly do away with this freedom at the first opportunity by calling for acyclic or single-parent object ownership graphs. These restrictions primarily make it easier to think about object lifecycle -- the fact that they allow refcounting to suffice for memory management is icing on the cake.

Re:Windows NT Kernel Contributor Explains Why Performance is... by pcwalton on Saturday May 11, 2013 @02:58PM (#5691684)
> If the ref counting is manual or optimized, it doesn't happen after every assignment, only ones which change object ownership.

The Rust compiler does this. Even so, 19% of the binary size in rustc is adjusting reference counts.

I am not exaggerating this. One-fifth of the code in the binary is sitting there wasted adjusting reference counts. This is much of the reason we're moving to tracing garbage collection.

All of those optimizations that you're talking about (for example, packing reference counts into the class pointer) will make adjusting reference counts take even more code size.

> As for not allowing cycles, I consider that a feature. The headache of memory management doesn't go away with GC.

The fact that memory management is still an issue doesn't mean that we shouldn't make it work as well as we can.

In large software projects, cycles are a persistent issue. Gecko had to add a backup cycle collector over its reference counting framework, and observed memory usage decreased significantly once it was implemented.

Re:Windows NT Kernel Contributor Explains Why Performance is... by zooko_LeastAuth on Saturday May 11, 2013 @08:18PM (#5692636)
That's the evolution Python went through as well: reference counting, then (in CPython 2.0) an adjunct cycle-collector, then (in PyPy) a non-reference-counting garbage collector.

Hopefully in future versions of PyPy an even better garbage collector. :-)

Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Saturday May 11, 2013 @05:57PM (#5692254)
Very nice explanation, except you are forgetting WinRT is actually an extension of COM.

This means each increment/decrement is a virtual method call with the corresponding invocation costs.

This is why the .NET runtime caches the WinRT objects, and even re-uses them instead of making a 1:1 use like C++/CX does.

Re:Windows NT Kernel Contributor Explains Why Performance is... by seanmcdirmid on Saturday May 11, 2013 @09:39AM (#5690547)
The caching misbehavior of referencing counting has been greatly exaggerated, especially in the context of UI where responsiveness is much more important than raw CPU speed. Also, the ref counting tradeoff seems to work better for device (e.g. all the cool kids [1] are doing it).

[1] https://developer.apple.com/library/mac/documentation/Genera...

.NET still does GC, it is only the WinRT APIs (something like COM) that manage resources through ref counting. There is some cool interop magic that makes this somewhat transparent to the programmer.

Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Saturday May 11, 2013 @10:37AM (#5690696)
> The caching misbehavior of referencing counting has been greatly exaggerated, especially in the context of UI where responsiveness is much more important than raw CPU speed. Also, the ref counting tradeoff seems to work better for device (e.g. all the cool kids [1] are doing it).

Apple's ARC is a compiler hack. It only works if you happen to use set of Cocoa libraries that are recognized by the compiler and allow for some magic incantation.

You cannot make any Objective-C library ARC aware.

ARC is a good solution for Objective-C, because Apple never managed to have their GC working safely in all situations, given the underlying C type system.

> .NET still does GC, it is only the WinRT APIs (something like COM) that manage resources through ref counting. There is some cool interop magic that makes this somewhat transparent to the programmer.

True, but C++ does not and you still have the performance penalty of reference counting.

The only way to have reference counting with good performance is do what Parasol or Objective-C do, by removing unnecessary increment/decrement operations thanks to dataflow analysis.

Which is something that you cannot do just by using the WinRT runtime and need to rely on the cleverness of the compilers.

Actually, are you aware that reference counting tends to be presented on the first chapter of many CS books about garbage collection as poor man's GC?

Re:Windows NT Kernel Contributor Explains Why Performance is... by gurkendoktor on Saturday May 11, 2013 @11:19AM (#5690854)
> Apple's ARC is a compiler hack. It only works if you happen to use set of Cocoa libraries that are recognized by the compiler

I don't understand. How does ARC rely on more than what NSObject provides? I'm coming from the C++ shared_ptr world and ARC does not feel much different so far (less explicit, but easier to optimise).

Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Saturday May 11, 2013 @06:04PM (#5692273)
No, it is an hack.

Just read this document.

https://developer.apple.com/library/mac/#releasenotes/Object...

You need to write code with lots of special cases to be able to have ARC active and not fall into strange compiler errors.

Re:Windows NT Kernel Contributor Explains Why Performance is... by astrange on Saturday May 11, 2013 @09:30PM (#5692775)
The compiler errors are actually quite clear.

Most of the restrictions (bridge casts and naming methods) are interoperation with non-ARC code, and from working on Java/JNI I can assure you GC<>manual interaction was worse.

It is of course still quite possible to get abandoned memory through retain cycles.

Re:Windows NT Kernel Contributor Explains Why Performance is... by mikeash on Saturday May 11, 2013 @10:29PM (#5692879)
What part of that is supposed to explain your rather bizarre claim that ARC only works with special Apple libraries?
Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Sunday May 12, 2013 @01:30AM (#5693268)
> The compiler understands Objective-C methods that return Core Foundation types follow the historical Cocoa naming conventions
Re:Windows NT Kernel Contributor Explains Why Performance is... by mikeash on Sunday May 12, 2013 @09:40AM (#5694169)
That's very much an uncommon special case, though. You don't need that to use ARC, and rarely come across it.
Re:Windows NT Kernel Contributor Explains Why Performance is... by mikeash on Saturday May 11, 2013 @12:22PM (#5691082)
It doesn't even rely on NSObject. Any root class that provides a compliant implementation of retain, release, and autorelease will work with ARC.
Re:Windows NT Kernel Contributor Explains Why Performance is... by seanmcdirmid on Saturday May 11, 2013 @07:42PM (#5692564)
> True, but C++ does not and you still have the performance penalty of reference counting.

Not very relevant. For UI code, responsive resource allocation and de-allocation is much more important than saving a few % CPU cycles.

> Actually, are you aware that reference counting tends to be presented on the first chapter of many CS books about garbage collection as poor man's GC?

Yes! Sometimes the conventional wisdom doesn't hold in the end, or it doesn't hold for all use cases. But I should have been more clear that ref counting is GC, whereas what I refer to as GC involves sweeping reference roots periodically to identify garbage.

Re:Windows NT Kernel Contributor Explains Why Performance is... by pjmlp on Sunday May 12, 2013 @01:45AM (#5693293)
> > True, but C++ does not and you still have the performance penalty of reference counting.

> Not very relevant. For UI code, responsive resource allocation and de-allocation is much more important than saving a few % CPU cycles.

I beg to differ. I consider missile control radar systems of military applications a bit more important than UIs.

There are military applications using real time GC systems, which I can list here if you want.

I also have a German magazine here (Making Games Magazin 1/13) that explains how Witcher2 for the Xbox 360 makes use of a GC.

Anyway one thing I do agree, automatic memory management, regardless of the form, should be the any of any programming language.

Pure manual memory management should be treated like Assembly. It will never go away and should only be used for the very few cases where there is no other way around the problem at hand.

Re:Windows NT Kernel Contributor Explains Why Performance is... by seanmcdirmid on Sunday May 12, 2013 @08:10PM (#5696482)
I'm not anti-GC, I just think that reference counting is appropriate for UI code. .NET still supports GC, and the collector is very good compared to our competitors; I'm not saying we should give that up!

I should have mentioned that the primary reason WinRT seems to use reference counting is for interop reasons: its much easier to great a UI toolkit that is programmable in both C# and C++. Note that WPF never had a C++ interface, while WinRT has one without the need for any bridge code.

Reference counting has some interesting applications where it is more appropriate trace-based GC. Heck, sometimes we want old-fashioned arenas (Pascal!), it would be nice if our languages could give us some choices.

Manual memory management is annoying but sometimes necessary. I can see why native programmers don't want to give it up as it constitutes the primary performance difference these days between native and managed languages.

Re:Windows NT Kernel Contributor Explains Why Performance is... by kevingadd on Saturday May 11, 2013 @07:33AM (#5690332)
GDI has been GPU accelerated literally forever. Vista may have dropped hardware acceleration for GDI, but it was promptly brought back in Windows 7.

Since the Win32 UI stack uses GDI, it was hardware accelerated before WPF even existed.

Re:Windows NT Kernel Contributor Explains Why Performance is... by seanmcdirmid on Saturday May 11, 2013 @08:02AM (#5690376)
Not the same thing! This is like comparing pre-DX8/programmable shader hardware acceleration to the fixed-function crud we had to deal with a long time ago. Ya, you can emulate fixed functions with programmable shaders, but its a poor way to make use of a modern GPU.

Here is a good discussion of the topic:

http://msdn.microsoft.com/en-us/library/windows/desktop/ff72...

Excerpts:

> When the GDI DDI was first defined, most display acceleration hardware targeted the GDI primitives. Over time, more and more emphasis was placed on 3D game acceleration and less on application acceleration. As a consequence the BitBlt API was hardware accelerated and most other GDI operations were not.

> In order to maintain compatibility, GDI performs a large part of its rendering to aperture memory using the CPU. In contrast, Direct2D translates its APIs calls into Direct3D primitives and drawing operations. The result is then rendered on the GPU. Some of GDI?s rendering is performed on the GPU when the aperture memory is copied to the video memory surface representing the GDI window.

> Existing GDI code will continue to work well under Windows 7. However, when writing new graphics rendering code, Direct2D should be considered, as it takes better advantage of modern GPUs.