News for hackers. Stuff that matters.
Re:My misalignment with Apple's love affair with Swift by chadcmulligan on Sunday June 10, 2018 @04:59PM (#17280112)
Its interesting how languages divide opinion. For me I think Swift is the best language around. One of the issues not addressed in the article (I believe) is performance, Swift is faster than Objective C because of the static type checking, which is a good thing.

It has the advantages of C# as a language but no garbage collection, granted the reference counting in Swift can be problematic, but it seems to be improving with each release.

The functional programming aspect of swift I find useful. There are a number of features in swift that decreases the amount of code - e.g. if let/guard. The use of optional types increases program safety.

The fact that you can use classes declared elsewhere without any consideration of dependency I find very powerful, and of course with great power comes great responsibility.

The power of swift collections is fantastic, and the syntax compared to Objective C is so succinct.

Many more...

Re:My misalignment with Apple's love affair with Swift by jakobegger on Sunday June 10, 2018 @06:36PM (#17280554)
I'm a big Swift fan as well, but the performance part simply isn't true. Swift was supposed to be potentially faster than Obj-C because of types, but in practice I have never seen that.

Since it is missing mutable arrays and strings, your code often ends up copying a lot of memory around if you're not extremely careful, and you quickly get accidentally quadratic complexity.

A lot of APIs are just wrappers around ObjC APIs, with lots of memory management calls required for bridging. You really need to be careful what calls you put in a tight loop.

And that's before you even start with the good stuff. Protocols with associated types are so slow that they are unusable for anything performance sensitive. Theoretically the compiler should be able to optimize it incredibly well since it's all statically typed, but in practice the compiler seems to never be smart enough. Call stacks get very deep very quickly (protocol witnesses, closures, ...).

In my experience the easiest way to make Swift code fast is to rewrite hotspots in ObjC.

Re:My misalignment with Apple's love affair with Swift by trevor-e on Sunday June 10, 2018 @08:43PM (#17280951)
Sorry but this is completely false, Swift is certainly faster than ObjC. There are several WWDC talks, independent reviews, and the language designers themselves supporting that.

In practice I agree it's easy to write code that's slow, but you can't knock the language itself for user error. They are still working on improving the user experience, such as how 4.2 will remove redundant generated retain/release calls. And hopefully they make the compiler smarter at figuring out which specialization functions to output for generics/protocols.

Re:My misalignment with Apple's love affair with Swift by jakobegger on Monday June 11, 2018 @09:38AM (#17284045)
Nothing the "language designers" claim can change the fact that I need to rewrite critical sections in ObjC.

There is a subset of Swift that is fast. But if you tell people that Swift is faster than Obj C, they will think that they can rewrite code in Swift and it will be faster. That may be the case at some point in the future, but right now that's just not true yet.

In my experience, the opposite is the case. Swift is a great language, it's safe, and nice to work with. But those bits where you are processing data in a tight loop -- you still may have to do that in Obj C.

Obviously this depends a lot on your problem, but I have seen 2x speedups and more for things like string processing (rewriting things from Swift in Obj C)

And as I said, that's without using the fancy parts in Swift (things like type erasure absolutely kill performance)

Re:My misalignment with Apple's love affair with Swift by Froyoh on Monday June 11, 2018 @12:11AM (#17281705)
This is false, that is false, I don't know who to believe.
Re:My misalignment with Apple's love affair with Swift by Derbasti on Monday June 11, 2018 @02:48AM (#17282306)
Simple: Swift is clearly fast for some applications, but slow for others. It is not a silver bullet. There are no silver bullets.
Re:My misalignment with Apple's love affair with Swift by woolvalley on Sunday June 10, 2018 @07:37PM (#17280745)
There is a lot of maturity left in the language for it to grow into. I typically say that it takes 10 years for a language to be mature enough to avoid stupid crap like you describe.
Re:My misalignment with Apple's love affair with Swift by mpweiher on Monday June 11, 2018 @02:15AM (#17282200)
> performance, Swift is faster than Objective C

No it's not.

> because of the static type checking, which is a good thing

That's a reason why you think it should be faster. Actual performance is measured, not argued.

When you actually measure Swift, it is horribly slow.

UPDATE: details in other posts here. In general, my measurements have found Swift code to be 50% to 50x slower.

Re:My misalignment with Apple's love affair with Swift by archagon on Monday June 11, 2018 @08:43PM (#17289862)
I've found it to only be slower in Debug, not Release builds. Release is often significantly faster than the equivalent ObjC, and only a tiny bit slower than native C.
Re:My misalignment with Apple's love affair with Swift by mpweiher on Wednesday June 13, 2018 @05:41AM (#17301614)
Hmmm...you do realize that Objective-C is a full superset of C, so "Objective-C vs. native C" is not a distinction that makes sense?

If you are comparing primitive types in Swift with objects in Objective-C, then you are comparing apples to oranges. You need to compare objects in Swift with objects in Objective-C and primitive types in Swift with primitive types in Objective-C.

Rephrasing, my findings are similar:

Swift in debug mode is so incredibly slow that even using primitive types with Swift is slower than Objective-C objects. In release mode, it approaches, but rarely reaches native Objective-C speed.

More details here:

http://blog.metaobject.com/2014/09/no-virginia-swift-is-not-...

Re:My misalignment with Apple's love affair with Swift by sheeshkebab on Sunday June 10, 2018 @05:37PM (#17280286)
Faster than ObjC? Unless your objc code is written using nothing but id and reflection, I’m not sure how can this be the case...
Re:My misalignment with Apple's love affair with Swift by saagarjha on Sunday June 10, 2018 @05:40PM (#17280300)
> Unless your objc code is written using nothing but id and reflection

That's how all Objective-C code is written–every method call goes through objc_msgSend.

Re:My misalignment with Apple's love affair with Swift by sitharus on Sunday June 10, 2018 @05:55PM (#17280383)
To be fair to objc_msgSend it's orders of magnitude faster than most reflection APIs. https://www.mikeash.com/pyblog/performance-comparisons-of-co... has some benchmarks, it's a bit old but I don't think objc_msgSend would get slower.
Re:My misalignment with Apple's love affair with Swift by saagarjha on Sunday June 10, 2018 @09:35PM (#17281132)
Yup, I'm not saying objc_msgSend is slow: it's just that it's not faster than a direct call, and I believe about on-par with a virtual function call.
Re:My misalignment with Apple's love affair with Swift by Derbasti on Monday June 11, 2018 @02:50AM (#17282318)
And let's not forget that ObjC is downwards compatible with C. If you need a particular loop or function to be fast, you can write it in C, in the same ObjC source file.
Re:My misalignment with Apple's love affair with Swift by archagon on Monday June 11, 2018 @08:42PM (#17289850)
For one, ObjC can't treat objects as values or initialize them on the stack. Also, the everything-is-an-object paradigm is terrible for arrays of values, since every NSNumber or NSValue has to be individually malloced and released. (Though maybe not the case anymore? It was certainly a chokepoint several years ago.)

Both of these factors have forced me into using lower-level Objective-C++ code on numerous occasions.

Re:My misalignment with Apple's love affair with Swift by mercutio2 on Tuesday June 12, 2018 @08:36PM (#17299315)
NSNumbers are generally tagged pointers, unless you’ve got a really large number.
Re:My misalignment with Apple's love affair with Swift by archagon on Tuesday June 12, 2018 @08:50PM (#17299370)
This didn't help me in my app, unfortunately, because I needed to encode CGPoints as NSValues. I suppose I could have resorted to ordinary, interleaved CGFloats, but it was way easier and more productive to just use C++ vectors. I knew exactly what I was getting with those, and I didn't have to do the boxing/unboxing dance.
Re:My misalignment with Apple's love affair with Swift by fulafel on Sunday June 10, 2018 @11:33PM (#17281544)
> [...] no garbage collection, granted the reference counting in Swift can be problematic, but it seems to be improving with each release.

nitpick: reference counting is a form of GC in standard CS terminology. It saves some memory compared to other GC methods but is slowish.

Re:My misalignment with Apple's love affair with Swift by chadcmulligan on Monday June 11, 2018 @03:19AM (#17282427)
reference counting is deterministic, GC isn't, which was always the apple engineers objection to GC - no pause while the collector runs.
Re:My misalignment with Apple's love affair with Swift by beureum on Monday June 11, 2018 @04:37AM (#17282743)
There exist pauseless GCs, Apple's objection to GC is that GCs need more RAM, and that would reduce their profit margin on iPhones. A fair reason if you ask me.
Re:My misalignment with Apple's love affair with Swift by fulafel on Monday June 11, 2018 @10:36AM (#17284553)
Reference counting has pauses, as object deallocations cascade. You can address this by having a deallocation work list that runs a bit at a time, but then again there is also a large body of work on other kinds of GCs that address pauses.
Re:My misalignment with Apple's love affair with Swift by jrs95 on Sunday June 10, 2018 @08:53PM (#17280990)
The performance improvements aren't exactly because of static typing. Obj-C method calls are slower because it uses dynamic dispatch (literal message passing vs a C style function invocation IIRC), whereas Swift generally only does this for interop with Obj-C.