Live data from Hacker News

Can you recreate Objective-C in C?

orangejuiceliberationfront.com

11–20 of 41 posts

Re: Can you recreate Objective-C in C?

#11
post #4

Earlier quoted context omitted.

There's a lot of caching involved. And it brings a lot of benefits.

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.

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?

#12

> 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…

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?

#13
post #12

> 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…

Can you link to any info about implementation details for objc? I’ve been trying to make my own language and this seems helpful.

[deleted]

Re: Can you recreate Objective-C in C?

#14

Earlier quoted context omitted.

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.

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++.

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?

#15
post #4

Earlier quoted context omitted.

There's a lot of caching involved. And it brings a lot of benefits.

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.

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?

#16
post #12

> 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…

Can you link to any info about implementation details for objc? I’ve been trying to make my own language and this seems helpful.

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?

#17
"C is a language that is turing-complete. So is Objective-C. So shouldn’t it be possible to recreate an Objective-C program in straight C?"

Major misunderstanding about turing completeness.

A language is so much richer than simply enabling you to write a map from inputs to outputs. Sure you could probably rewrite my Haskell program in Fortran in the sense that the Fortran program produces the exact same outputs for any given inputs...but it's hardly convincing to me that you "recreated" my program in Fortran after dropping my monad transformers and lenses.

Regardless, you wouldn't even be able to generally prove (assuming you lack dependent typing) that your program is functionally equivalent to mine (since this is undecidable).

Re: Can you recreate Objective-C in C?

#19
post #12

Earlier quoted context omitted.

Can you link to any info about implementation details for objc? I’ve been trying to make my own language and this seems helpful.

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).

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?

#20

Earlier quoted context omitted.

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).

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.

> 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 ;)

Post reply on HN