News for hackers. Stuff that matters.
Re:App Developers on Swift Evolution by pcwalton on Wednesday December 23, 2015 @03:53AM (#10782530)
Depends what you mean by "just fine". Objective-C method dispatch is very slow. It's significantly slower than that of JavaScript, for example.
Re:App Developers on Swift Evolution by dilap on Wednesday December 23, 2015 @10:37AM (#10783768)
Just fine in the sense that it's the dominant language on iOS, and iOS sets the bar, IMO, for snappy experiences on modern platforms.

Stuff implemented in java and javascript, not so much.

Maybe saying objc is cheating a little bit, because it's so easy to drop into straight C to optimize. But stuff like UITableView (edit: scroll => table) is all heavily based on objc message sending, and it's easy to get great scrolling perf using that api.

I don't know the exact number, but you can get a lot of message sends done in 16ms.

(Another example -- GitUp, insanely fast git client, objc.)

(On the other hand, I do remember that C++ based BeOS on 66 MHz PowerPCs back in the day was double-insanely fast, so maybe Obj-C is slow, on a language level, and we just don't know it because everything else is so much slower, on an implementation level. But then again that stuff was all virtual calls. I just don't buy the argument that at the _system framework_ level dynamic dispatch is a barrier to good performance.)

Re:App Developers on Swift Evolution by pcwalton on Wednesday December 23, 2015 @11:17AM (#10783964)
> Just fine in the sense that it's the dominant language on iOS, and iOS sets the bar, IMO, for snappy experiences on modern platforms.

> Stuff implemented in java and javascript, not so much.

I agree with the data but not the conclusion. iOS' performance is great in spite of using Objective-C as its language, not because of it.

The only performance-related features that Objective-C brings to the table are (a) being able to piggyback on the optimizations implemented in popular open source C++ compilers; (b) being compatible with C, so you can easily inline C into Objective-C when Objective-C's slow performance bites you; (c) not requiring tracing garbage collection. When you're actually in heavy Objective-C land with method calls in tight loops, performance is pretty bad, both because message sending is slow and because methods can't be devirtualized. Apple knows this, and they've basically reached the limit of what they can do without incompatible changes to the Objective-C semantics. Hence the performance-related design decisions in Swift.

(As for UIScrollView/UITableView, I agree that they're very fast relative to Android or current Web browsers for example. I know the reasons for this and they have nothing to do with the implementation language. Algorithmic and engineering concerns often trump programming language performance.)

Re:App Developers on Swift Evolution by dilap on Wednesday December 23, 2015 @12:26PM (#10784385)
(Oops, I meant table view, not scroll view. Thanks.)

I agree with you on the specifics, but somehow still reach different conclusions. :)

Edit: I guess if you take the lense of wanting Swift to replace the combination of ObjC + plain C, what you're saying makes sense.

It's a shame though if you end up losing the very-helpful ability to dynamically modify system provided behavior.

And even for relatively performance intensive stuff, like table scrolling, objc is plenty fast.

Every language obviously has its domain of performance-appropriateness -- C too will hit its limits; x264 w/o the asm optimizations is ungodly slow, for example.

I just think for "apps", generally, objc is a speedy language. Empirically this is true. Explanations that call for this being true "despite the language" are suspicious to me; if it's not the language, what's the magic pixie dust?

And personally I suspect the swift folks aren't going the more static route because of actual real-world user-facing performance walls they've run into, but because that's what they're into -- they're C++/static language/compiler guys.

Last example: Look at the most performance constrained stuff apple is currently doing, the watch. Think that's Swift? I'm pretty sure it's not.

Re:App Developers on Swift Evolution by Nang1975 on Wednesday December 23, 2015 @06:46PM (#10786325)
> (As for UIScrollView/UITableView, I agree that they're very fast relative to Android or current Web browsers for example. I know the reasons for this and they have nothing to do with the implementation language. Algorithmic and engineering concerns often trump programming language performance.)

I'd be curious to hear more about the reasons. I'm not familiar with the web side, but Android layout performance can be painful, especially on older devices running Dalvik.

Re:App Developers on Swift Evolution by mpweiher on Wednesday December 23, 2015 @06:02PM (#10786178)
>iOS' performance is great in spite of using Objective-C as its language, not because of it.

Nope. Objective-C is a great language for performance. Remember the 97-3 rule. That is, only roughly 3% of your code are responsible for almost all the performance. You just don't know which parts those are.

The dynamic features of the language give you awesome productivity to more quickly get to the point where you find out which 3% to actually optimize heavily. Doing this optimization is straightforward because you have all the tools at your disposal, the road to them is smooth and the performance model is predictable.

I have repeatedly achieved performance that's better (yes!) than equivalent C programs, and with a little bit of work you can actually maintain nice OO APIs.

>(b) being compatible with C so you can easily inline C into Objective-C

I really don't understand where this (common) misunderstanding is coming from. Objective-C is C, or more precisely a superset of C. You don't "inline C into Objective-C". You use different aspects of Objective-C as appropriate. If you are not capable of using the full capabilities of the languages, that is your fault, not a problem of Objective-C.

This seems to have been lost, with people (inappropriately) using Objective-C as a pure object-oriented language. It is not. It is a hybrid object-oriented language. In fact, the way it was intended to be used is to write your components using C and use dynamic messaging as flexible packaging or glue.

> because methods can't be devirtualized

Sure they can. I do it all the time:

   SEL msgSel=@selector(someMessage:);
   IMP devirtualized = [object methodForSelector: msgSel];
   for (i=0;i<LARGE_NUMBER;i++) {
       devirtualized( object, msgSel, arg[i] );
   }
Did you mean they can't be automatically devirtualized by the compiler? I hope you understand that these are two different things. Of course, it would be nice to have compiler support for this sort of thing, which would have been miles easier than coming up with a whole new language:

   for (i=0;i<LARGE_NUMBER;i++) {
       [[object cached:&cache] someMessage:arg[i]];
   }

Or some other mechanism using const.

> reached the limit of what they can do without incompatible changes to the Objective-C semantics. > Hence the performance-related design decisions in Swift.

Swift performance is significantly worse and waaaayy less predictable than Objective-C, and that is despite the Swift compiler running a bunch of mandatory optimizations even at -O0.

Re:App Developers on Swift Evolution by plorkyeran on Wednesday December 23, 2015 @06:38PM (#10786302)
Calling via a function pointer isn't devirtualization. It's still an indirect call, and does not allow the function to be inlined since the actual function is not known until runtime. It merely gets you from something 2-4x as expensive as a c++ virtual call to something roughly as expensive.
Re:App Developers on Swift Evolution by mpweiher on Wednesday December 23, 2015 @07:06PM (#10786376)
Last I checked, a C++ virtual function call loads the method pointer via an indirect memory load. Hard for the CPU to speculate through, so typically a pipeline stall.

A function pointer that's in a local variable, so loaded into a register is a completely different beast, as the measurements bear out.

In my measurements, a message-send is ~40% slower than a C++ virtual method call, whereas an IMP-cached function call is ~40% faster, and slightly faster than a regular function call.

Re:App Developers on Swift Evolution by plorkyeran on Wednesday December 23, 2015 @07:27PM (#10786438)
The vtable for an object being actively used will be in L1 cache, and when a virtual function is called in a loop, Intel's CPUs have been able to predict the target for many, many years. ARM may not; I've never had recent to deeply investigate virtual call performance on iOS.

Finding calling via a function pointer to be faster than calling directly suggests that you were not actually measuring what you thought you were measuring.

Re:App Developers on Swift Evolution by mpweiher on Wednesday December 23, 2015 @06:15PM (#10786232)
Actually, Objective-C message sending is extremely fast, only slightly slower than a C++ virtual method call and in the same ballpark as a function call (the relative cost has drifted from 40% to 2x higher depending on OS version and machine architecture) without much of an impact on applications.

In addition, message-sending is extremely cheap compared to other operations, for example roughly 50x faster than object allocation.

Last not least, in the few cases where messaging does become an issue, it is trivial to replace with more optimized dispatch, ranging from an IMP-cached send to a static inline function depending on the performance requirements and API boundaries in question.

Re:App Developers on Swift Evolution by mariodiana on Wednesday December 23, 2015 @09:45AM (#10783514)
When does another language then become a case of premature optimization? I'm serious. If performance is an issue, you can profile Objective-C code and then either deal with the runtime directly, or rewrite critical portions in C. But I would think that there is a lot of Objective-C code out there that runs just fine without any fancy tricks like what I'm describing.
Re:App Developers on Swift Evolution by pcwalton on Wednesday December 23, 2015 @11:19AM (#10783971)
Sure, but that doesn't mesh with Swift's goals, as I understand them. The team seems to be positioning Swift as a language that will scale down to low-level performance-critical pieces in a way that Objective-C can't (though I may be wrong here since I'm not involved in that community very much).
Re:App Developers on Swift Evolution by mpweiher on Wednesday December 23, 2015 @06:07PM (#10786198)
Well, they say they want that. But this is at best aspirational, it isn't true. Certainly not so far and as far as I can tell not in the foreseeable future. Quite frankly, I doubt it will ever be true, because in essence their claim is that they are building the Sufficiently Smart Compiler and despite all the problems now it will be totally awesome when they're done.

Swift currently can't hold a candle to Objective-C on the performance front, which is why all the heavy lifting is done in Objective-C. As a small example, see how many articles there are on "Swift JSON parsing", then check what percentage of those call into the NSJSONSerialization Objective-C class to the actual, you know, parsing.

Re:App Developers on Swift Evolution by mariodiana on Wednesday December 23, 2015 @12:11PM (#10784295)
No, I think you are right. My understanding is that Swift is supposed to scale down, as you say, and performance there is critical. I was considering things from the point of view of higher level programming. Thanks.
Re:App Developers on Swift Evolution by Alphasite_ on Wednesday December 23, 2015 @05:11AM (#10782726)
They aren't are they? Cached method calls are fairly fast from what i've read.

[1] https://www.mikeash.com/pyblog/performance-comparisons-of-co...

Re:App Developers on Swift Evolution by pcwalton on Wednesday December 23, 2015 @11:07AM (#10783917)
According to that chart, Obj-C method calls are either 4.5x or 7.8x slower than C++ virtual method calls, depending on whether you're looking at 32- or 64-bit code. That alone is pretty bad.

But it's even worse than that: I'm talking about devirtualized calls, which are far faster than virtual calls in C++. More importantly than just the call time, devirtualization can have outsized performance impact because it enables inlining, converting intraprocedural optimizations to interprocedural optimizations. The difference between inlining and not can easily be a 10x-level performance difference with an optimizing compiler like LLVM.

Re:App Developers on Swift Evolution by mpweiher on Wednesday December 23, 2015 @06:25PM (#10786271)
The chart is wrong for message sends. I've been measuring this for a long time, and get ~2.1ns for a message send vs. 1.2ns for a cached message send on a comparable machine.

If you want inlining, use a static inline function.