News for hackers. Stuff that matters.
Re:Can you recreate Objective-C in C? by saagarjha on Thursday August 09, 2018 @04:05PM (#17727987)
> Objective-C under the hood compiles every method call into a call to the function objc_msgSend().

Well, kinda. You need a family of functions (objc_msgSend, of course, but also objc_msgSend_stret, objc_msgSendSuper, and objc_msgSendSuper_stret), to get this to really work for when you have structures as your return value or are calling a superclass's implementation (or both!). Since the inline assembly that is objc_msgSend really has no idea what the types of function it's calling is, the compiler must call an appropriate function based on the type information it has.

Re:Can you recreate Objective-C in C? by sdegutis on Thursday August 09, 2018 @04:20PM (#17728102)
This is also why dynamic languages like PyObjC, RubyObjC, RubyCocoa, MacRuby, Nu, and F# had difficulty bridging certain functions and methods, because you have to either create an insanely complex function that's able to take any @encoding and turn it into a runtime type, or you have to shimmy it with guesses for the most common ones like NSRect et al.
Re:Can you recreate Objective-C in C? by ewmailing on Thursday August 09, 2018 @08:35PM (#17729773)
LuaCocoa author here.

While the objc_msgSend, objc_msgSend_stret, et. al, is an annoying detail, that isn't really the hard part. (And in fact, all the good bridges use libffi to do more direct invocations and avoids a lot of this problem.)

The hard part of bridging is those places where Objective-C introspection is not powerful enough to discover the types of parameters or signatures of functions. Typically, this is all the C stuff.

So for example, Obj-C's runtime introspection cannot tell you the make up of a struct. So it cannot tell you the size of NSRect, NSPoint, NSSize, or the individual data types inside the struct (and the names of each item if you need to access them).

And the Obj-C runtime introspection cannot tell you about what C functions exist and what the parameter types and return types are. So for example, there is no Obj-C runtime way to look up that CGPointMake takes two CGFloat parameters and returns a CGPoint struct.

Also, inline functions (marcos) in C/Obj-C are also problematic for these bridges since they need to call the functions at runtime.

Apple shipped a framework called BridgeSupport in Mac OS X 10.5 which contains XML data for all the things that cannot be determined at runtime. It also contains .dylibs with symbols for inline functions. BridgeSupport is still in macOS today, but it isn't getting much love and Apple keeps (accidentally?) breaking things in BridgeSupport every release.

Re:Can you recreate Objective-C in C? by chrisseaton on Thursday August 09, 2018 @04:13PM (#17728042)
When you think about it it's bonkers that this language is doing dynamic method calls without any inline caching and I presume no inlining?!. You would think this was intractable for good performance, but it seems to work fine!
Re:Can you recreate Objective-C in C? by verstaen on Thursday August 09, 2018 @04:17PM (#17728075)
There's a lot of caching involved. And it brings a lot of benefits.
Re:Can you recreate Objective-C in C? by chrisseaton on Thursday August 09, 2018 @04:24PM (#17728139)
There's no inline caching is there though? And so no 'mother of all optimisations' inlining. Or is my understanding of how Objective C works mistaken?

You wouldn't dream of implementing a dynamic language these days designed for performance without basic polymorphic inline caching. But Objective C seems to get by fine without it. Maybe it's not as essential as we think.

Re:Can you recreate Objective-C in C? by ridiculous_fish on Thursday August 09, 2018 @05:06PM (#17728508)
Do any statically compiled languages have inline caches? This would be a huge loss for ObjC, since it would greatly increase memory pressure by dirtying every text page.

ObjC has per-class out-of-line caches keyed by the selector (intern'd string).

It's true that there's no hope of inlining across a message send, but ObjC's C and C++ compatibility mitigates a lot of this expense. Apps routinely use C and C++ for perf critical sections.

Also some of Apple's APIs are inline C functions (NSMakeRect, etc) and others are designed to minimize dynamic dispatch. Iterating an NSArray typically requires only one message send (see the NSFastEnumeration protocol).

There's also more exotic techniques like IMP caching.

Re:Can you recreate Objective-C in C? by cameldrv on Thursday August 09, 2018 @07:17PM (#17729390)
AFAIK objc_msgSend is always called, but it's very heavily optimized and written in assembly language. Mike Ash had a great post: https://www.mikeash.com/pyblog/friday-qa-2017-06-30-dissecti...
Re:Can you recreate Objective-C in C? by twoodfin on Thursday August 09, 2018 @04:40PM (#17728276)
I wonder if /u/mikeash can still comment on this thread!

My recent experience has been that indeed inlining is essential for true "zero-cost abstraction"—say an array of integers with index set/get methods—but that once your abstraction is weighty enough that a method is performing more than a few dozen "interesting" instructions, inlining doesn't win much over how well the modern CPU can already optimize across calls.

EDIT: I should add I'm talking about static inlining. I'm sure your typical alien-technology JIT can identify the exact 800 instruction trace in your inner loop and pull it all together into a perfectly machine-sympathetic sequence that runs 50% faster than anything you could construct statically in the absence of profile-driven feedback.

Re:Can you recreate Objective-C in C? by saagarjha on Thursday August 09, 2018 @04:29PM (#17728176)
There’s no inlining for Objective-C methods, if that’s what you’re alluding to, because the language dictates that every method call must be observable. objc_msgSend is generally really fast, though; it’s usually as fast as or faster than a virtual function call.
Re:Can you recreate Objective-C in C? by machiavelli1024 on Thursday August 09, 2018 @04:27PM (#17728168)
There’s no inlining, because determining whether inlining wpuld be safe is probably Turing-complete. However, IMP cacheing is a thing and that’s enough for most cases. For the rest there is C and (Objective-)C++.
Re:Can you recreate Objective-C in C? by saagarjha on Thursday August 09, 2018 @04:34PM (#17728222)
Your comment was killed very quickly, strangely, but I though it had valid content so I revived it. The actual issue against inlining is that anybody can intercept method calls, even from places that the compiler cannot know about such as bundles loaded at runtime. So this isn’t even an issue with Turing-completeness; it’s an impossible problem to solve at compile time.
Re:Can you recreate Objective-C in C? by chrisseaton on Thursday August 09, 2018 @05:13PM (#17728560)
> at compile time

That's the key thing - other languages which solve this solve it dynamically.

Re:Can you recreate Objective-C in C? by saagarjha on Thursday August 09, 2018 @05:20PM (#17728608)
Yeah, that’s what a JIT does. Unfortunately that’s not something that Apple really wants to open the door to on their platforms, especially for native code.
Re:Can you recreate Objective-C in C? by Someone on Friday August 10, 2018 @02:25AM (#17731133)
I don’t think that comes into play here. Objective-C is C, and although you could JIT any language, C isn’t made for it, both philosophically (one of its main claims to fame is ‘close to the metal’) and technically (a source file cold be compiled multiple times with different macro definitions or with a different set of #included files)
Re:Can you recreate Objective-C in C? by saagarjha on Friday August 10, 2018 @03:06AM (#17731259)
C isn't made for JIT because it's static; that's why inclining exists. Objective-C has room to improve because it has dynamic function calls.
Re:Can you recreate Objective-C in C? by chrisseaton on Friday August 10, 2018 @05:46AM (#17731912)
C can also benefit from inline caching - function pointers!
Re:Can you recreate Objective-C in C? by cestith on Thursday August 09, 2018 @08:14PM (#17729689)
Did you mean to say "NP-Complete" where you said "Turing-complete"?
Re:Can you recreate Objective-C in C? by Someone on Friday August 10, 2018 @02:34AM (#17731162)
Nitpick: languages don’t do inline caching, implementations do.

Also, Objective-C being “C with a little bit of dynamism where you need it” doesn’t need it as much as languages where every call is, principally, a dynamic one.

Having said that, the current implementation does some caching, but inside objc_msgSend, not inline with the call (https://www.mikeash.com/pyblog/friday-qa-2012-11-16-lets-bui...)

Re:Can you recreate Objective-C in C? by shawn on Thursday August 09, 2018 @04:31PM (#17728197)
Can you link to any info about implementation details for objc? I’ve been trying to make my own language and this seems helpful.
Re:Can you recreate Objective-C in C? by tinus_hn on Thursday August 09, 2018 @04:41PM (#17728285)
Check out Mike Ashes blog:

https://mikeash.com/pyblog/

It has posts where he recreates the functionality of the runtime, but without the optimizations that are in Apples runtime (which is open source).

Re:Can you recreate Objective-C in C? by sdegutis on Thursday August 09, 2018 @04:56PM (#17728424)
Can confirm that mikeash knows a lot about this stuff and posted a lot about it.

Also bbum, he does a lot of posts on SO about this stuff, and I thought he wrote blog posts on the subject too but can't seem to find them.

Re:Can you recreate Objective-C in C? by saagarjha on Thursday August 09, 2018 @04:59PM (#17728458)
> Can confirm that mikeash knows a lot about this stuff

Well, he works on Apple’s runtime team now, so it would be bad if he didn’t ;)