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

One thing that i want to add is that this whole exercise is actually a straw man. You yourself start your post with

"Problem Statement : Design an algorithm that fills in a buffer of pixel data"

If that is the problem, then do that. Do not fill an array with Pixel object references. It is not the same problem.

I am not denying that Swift has performance issues. I do think your test and proof is artificial: nobody would use the 'Swift Safe' method to manipulate pixel data. This is exactly why the unsafe features exists.

I think what you need to discover is how to use structs, classes and raw buffers. None of those is a golden hammer. If you have performance problems with one of those then that does not mean that it is flawed. It is likely simply a bad choice for the problem at hand.




First off, a struct is not an object. Structs in Swift, like many languages, are value types. They have the same packing structure in Swift as in C, especially in the example that I gave.

You can read more about them here: https://developer.apple.com/library/ios/documentation/Swift/...

I can write the same problem with components of the array as bytes if you prefer, but then performance will be even worse. The root cause of the performance issues is the array access and set. The more the do, the worse the performance.

So what you are saying, is that no person would ever create an array of data and expect to do anything with that data? And they would never do that in a processing loop? Ok...

If you actually read the blog post, you can see that I already gave an example using raw buffers (the UnsafeMutablePointer<Pixel>). Which means that every index into the pointer moves 4 bytes. Again, using UnsafeMutablePointer<Byte> is not doing anything other than creating a harder API surface to work with.

Also, the UnsafePointer types exist because without them, there would be no way to interact with C code. And if I need to use the UnsafePointer types everywhere I have arrays so that I can have reasonable performance in debug and release builds, then there is a bigger problem that needs to be addressed.


> So what you are saying, is that no person would ever create an array of data and expect to do anything with that data? And they would never do that in a processing loop? Ok...

No. I'm saying the memory layout of Array<Pixel> is undefined. You cannot assume that it is a contiguous chunk of memory for example.

Therefore it is the wrong solution for a render buffer.

After reading https://developer.apple.com/swift/blog/?id=6 I am not entirely sure about this. Can you find a more clear definition?




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

Search: