Hacker News new | past | comments | ask | show | jobs | submit login

I am also still exploring Swift, so forgive me if I got this wrong, but isn't the problem here the following:

Your RenderGradient() takes a RenderBuffer struct. Structs are passed by value. So two things happen in your RenderBuffer function:

1) You get a copy of the RenderBuffer, which means it and its contents (a lot of Pixel instances - Arrays are passed by value too!) will be copied every iteration

2) The work you do on the pixels in the RenderBuffer copy has no effect: you change the values of a copy and then ignore the copy. You don't return the modified RenderBuffer it from RenderGradient() or do anything with the result within RenderGradient().

So I think the reason you see such a big difference in this test between optimization and no-optimization is that in the first case, with optimization, the compiler knows that the work you do in RenderBuffer is a waste of time and simply does not do it.

While in the debug build the compiler does not apply optimization so your useless code is still executed.

You can confirm this by looking at the generated code. Or maybe simpler, render the RenderBuffer in an actual buffer or image for a visual inspection.




I think you are correct, and it (as expected) changes the results. I ran owensd code on my machine, in a completely non scientific and error prone setup. For release builds:

Objective-C: 0.0310617s

Safe Swift (incorrect): 0.0641295s (106.5% slower)

Safe Swift (correct): 0.0355441s (14.4% slower)

So the "safe" Swift version is running about 14.4% slower than the Objective-C version. It's not bad at all for a language that is so young! In debug mode, however, I didn't even have the patience to wait for the test run to finish...


Yes, the var should have been inout. It had an affect on the release build performance (twice as fast), but not the debug build. I've updated the blog post.

The performance problem is regarding all retain/releases though; not the copy of the data. That's why the debug builds are not affected.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: