B
Bill Bumgarner
friday.com · Personal blog · on HN since 2008
Bill Bumgarner is a former Apple engineer who writes about Objective-C internals, memory debugging, and runtime behavior.
What HN saysHN commenters repeatedly link his objc_msgSend series as a thorough explanation of how Objective-C messaging works, and list his blog among good Cocoa resources.
17HN people shared it
10linked it in comments
7stories submitted
104story points
10 years agolast shared
2008people per year2026
Most-shared posts
- 1Objc_msgSend(), instruction by instruction8 people · 47 points · first shared 2010 · 3 comments on HN
- 2Bbum2 people · first shared 2011
- 3Apple's Leopard Garbage Collector Now Open Source1 people · 28 points · first shared 2008 · 10 comments on HN
- 4Using Heapshot (OS X/iOS) to Find Undesirable Memory Growth1 people · 25 points · first shared 2011 · 1 comments on HN
- 5On Death and Taxes; Some Digital Tools to Make End-Of-Life Easier1 people · 2 points · first shared 2016 · discussion on HN
- 6Objective-C 2.0 Class Extensions Explained1 people · 1 points · first shared 2009 · discussion on HN
- 7RetainCount is useless.1 people · 1 points · first shared 2011 · discussion on HN
- 8Objc msgsend tour part 3 the fast path1 people · first shared 2011
- 9Pyobjc 20 pyobjc in leopard1 people · first shared 2008
- 10Ios 4 3 imp implementationwithblock1 people · first shared 2013
What HN people wrote when they linked it
chc · 2014-04-09
Here are a couple of Stack Overflow answers that illustrate how you do it in Objective-C:
http://stackoverflow.com/a/2573949
http://stackoverflow.com/a/18689338
If you're interested in the nitty-gritty of how objc_msgSend works, bbum from Apple wrote up a great low-level walkthrough a few years back: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map…
msbarnett · 2011-10-17
Awesome, I wasn't aware of that one.
Other good ones I know of are the already mentioned Cocoa with Love
http://cocoawithlove.com/
Cocoa is my girlfriend
http://www.cimgf.com/
and bbum's weblog-o-matic
http://www.friday.com/bbum/
ootachi · 2010-11-25
According to the link you posted, that's over four times slower than C++ virtual method dispatch (and note that most method calls in ordinary C++ code are not virtual). Also, IMP caching is hardly easy and is quite fiddly: see for example the benchmark code in that link as well as [1].
I highly recommend reading the tour of objc_msgSend [2]. It's very eye opening: for example, there's a cache scanning loop even in the fast case.
[1]: http://www.mulle-kybernetik.com/artikel/Optimization/opti-3-imp-deluxe…
[2]: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map…
stevejohnson · 2013-08-04
Try these if you'd like an actual explanation of how Obj-C messaging works.
http://www.mikeash.com/pyblog/friday-qa-2012-11-16-lets-build-objc_msg…
http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map…
js2 · 2013-07-15
Another good discussion of objc_msgSend() is:
http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map…
Codhisattva · 2012-09-16
This is a good companion http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map…
But keep in mind that both articles deserve to be updated.
jimbokun · 2008-04-07
On the Mac, you can use pyobjc to write Cocoa apps. Included with Leopard.
http://www.friday.com/bbum/2007/10/27/pyobjc-20-pyobjc-in-leopard/
rayiner · 2013-07-24
Practical example: objc_msgSend (which is called for every message send in an Obj-C program) is written in assembly so it can jump to the target method implementation without disturbing any caller-save/callee-save register state: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map (and also presumably because C won't guarantee that a tail call is actually compiled as a jump).
Practical example: objc_msgSend (which is called for every message send in an Obj-C program) is written in assembly so it can jump to the target method implementation without disturbing any caller-save/callee-save register state: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map (and also presumably because C won't guarantee that a tail call is actually compiled as a jump).
Assembly is more relevant today than it has been in awhile, largely because Intel has gone pretty gung-ho with vector instructions that compilers mostly can't figure out how to emit on their own.
For example, say I want to convert an array of bytes into a bitmask such that every zero-valued byte is converted into a set bit. On a 64-bit system, you can do this 64-bytes at a time:
unsigned long bits = 0;
for(unsigned char i = 0; i < 64; ++i) {
bits |= ((unsigned long)(bytes[i] == 0) << i);
}
This isn't a particularly efficient use of the CPU. If you've got a CPU that supports AVX2, you've got two very powerful instructions available to you: VPCMPEQB and VPMOVMSKB.…
leephillips · 2013-05-15
Maybe not "daily", but very frequently: here is a small selection of some of the small sites on my newsblur newsfeed list: [0]
http://www.jamesaltucher.com : Much self-pity, but also sometimes quite funny.
http://www.astrobio.net/latestnews_rss/news.xml : Is there life on other planets?
http://kaygun.tumblr.com/
http://www.friday.com/bbum : Coding, hardware projects, generally interesting.
http://bestyoucanbe.blogspot.com/ : Insights into the education system in regard to the raising of his neurologically non-typical son.
http://blog.lmorchard.com
http://ex-muslim.org.uk : Oppression of atheists in the Muslim world.
http://clojurefun.wordpress.com : Clojure!
http://scientopia.org/blogs/goodmath : Bad science, denialism.
http://blog.willbenton.com : Music, logic, programming, etc.
http://www.groklaw.net : Maybe not "small"; detailed analysis of important legal cases.
http://hicksdesign.co.uk/journal : Web design.
http://www.holovaty.com/ : "I'm co-creator and co-Benevolent Dictator For Life of the open-source Django Web framework"
http://prometheus.med.utah.edu/~bwjones : Combines talented photography, photoessays, and study of the retina.
http://lee-phillips.org : I regularly check my site to see if it's still working.
http://chronicle.com/blogs/linguafranca : Language issues; can be amusing or stupid.
http://freethoughtblogs.com/maryamnamazie : Freedom for women in the Muslim world.
http://math-blog.com : Infrequently updated, but often…
eridius · 2013-01-22
Yes, but if you message the subclass with the alias, only the superclass's method gets invoked, because the alias apparently contains the IMP of the superclass's method. Or it must, if what you said before is to be believed.
This is pretty nasty, because it breaks the idea of message sends and subclass overrides of methods. There's two ways you could solve it:
1. Set the IMP to a static function that looks up the method it should forward to and re-send the message there. This is hard to do in a manner that supports all arguments, and you'd need at least 2, possibly 3 versions anyway (one for regular, one for strret, and on some architectures, one for fpret). It would probably need to be written in assembly in order to forward the arguments.
2. More recently (in iOS 4.3, and whatever the corresponding OS X release was), the runtime got a nice little function called imp_implementationWithBlock(). This takes a block and returns an IMP. Bill Bumgarner has a nice blog post about it (http://www.friday.com/bbum/2011/03/17/ios-4-3-imp_implementationwithbl… You could use this function on every alias in order to invoke the correct method, passing along the correct arguments.