IssueRaiser

I am also still exploring Swift, so forgive me if I got this wrong, but isn't the problem here the following: Your RenderGradient() take...

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.

Posted by st3fan on Jan 31, 2015, 1:18 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.8,000 issues0 comments

hackernews (Software)

I am also still exploring Swift, so forgive me if I got this wrong, but isn't the problem here the following: Your RenderGradient() take...

st3fanJan 31, 2015, 1:18 PM

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.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue