<<

Low Cognitive Overhead Bridging "Interoperability isn't just a technical problem, it's also a problem. We have to make it easy to call between languages, so that users can The demo shown at FOSDEM always pick the most appropriate tool for the job at hand." example [ this year showed fully dynamic "This shows how to call a function from " development spanning C objc_setAssociatedObject: { Objective-C and Smalltalk. The self. 42. developer can inspect the ’fishes’. existing class hierarchy and C enumValue: OBJC_ASSOCIATION_ASSIGN }. the code for any methods "Note how C enumerations can be accessed. When encountering this construct, the Pragmatic Smalltalk will generate exactly the same code as an implemented in Smalltalk. It Objective-C compiler would for this line: is also possible to add, modify, objc_setAssociatedObject(self, 42, @"fishes", OBJC_ASSOCIATION_ASSIGN); or replace methods, add It will get the types for the function by parsing the header and automatically map Smalltalk types to C and Objective-C types. The instance variables and classes Easy Development doesn't need to write any bridging or foreign function interface at run time. Invoking a code." nonexistent class or method (C objc_getAssociatedObject: {self . 42 }) log. ] pops up a dialog asking the user to implement it, all from within a running app. The final version is statically compiled, with no explicit user Cross-Language Interoperability for intervention required. Fast, Easy, and Maintainable Code David Chisnall The World Today: But I wanted to use the But libfoo is C, libbar Frobnicator framework, and it's I have a great idea is C++, and I want to only for . And Python is Our approach to cross-language for a new app! use Java! sloooow! interoperability involves compiling You should use libfoo Fine, then you'll need to write a multiple languages down to a shared and libbar! That will You should use Python, load of bridging code. It might be make it really easy! it has bindings to C simpler to just reimplement the binary representation. We currently and C++! bits of libfoo and libbar that you want to use in Java... compile dialects of Smalltalk and Efficient Interoperability JavaScript and an object-oriented parser generator down to the same object model as Objective-C. We can call C directly and use Objective-C as a hybrid language for more complex bridging. Objective-C effectively becomes a domain-specific language for calling C and C++ from high-level languages. The World We're Building:

Okay. And I think I'll A single object may have methods I have a great idea use the Frobnicator for a new app! framework too. Why are you still implemented in different languages,. Oh, I finished here? Go and write already. your app. Good have fun! You should use libfoo and libbar! That will make it really easy!

The 3 graphics editor uses C++ for high- Message Sending Speed ! 6 performance 3D modelling code the Smalltalk, Objective-C for model 5 descriptions, and Smalltalk for procedural 4 geometry generation and scripting. 3

Real-World Use 2 Normalised time 1

0 AverageInline Case CachedSpeculativeGCC Inlining RuntimeC++ (nonvirtual) C++Message (virtual)C (directSend call)

The Objective-C implementation Part of this work has focussed on optimising developed as part of this work is dynamic languages. The biggest single used in a number of open source performance cost for dynamic languages is and commercial games and the cost of message sending (dynamic applications, with millions of installs, Fibonacci Benchmark Time method invocation). We have significantly including Android ports of iOS apps. 6 improved the performance of this, both in 5 the best and common cases. We added

4 inline caching and speculative inlining to the compiler and improved the lookup code 3 in the runtime, making our average case 2

Time in Seconds twice as fast as GCC and our best case faster 1 than a direct C call. 0 C Objective−CSmalltalk Smalltalk, Overflow−Safe We have shown that it's possible to use late- bound dynamic languages without Fast Implementation sacrificing performance and that mixing Hardware Assistance Memory Management Benchmark Results languages doesn't need to be slow or 12 difficult. Even though the implementation in 10 One unfortunate side effect of mixing languages is that your Smalltalk is noticeably slower than the C code is as safe as the least-safe language you use. This applies 8 version for the Fibonacci benchmark, it even when you use a JVM written in C++. We are currently 6 does have one advantage: it gives the correct answer for any input, whereas the C exploring sinking enforcement of high-level language isolation 4

properties (such as object encapsulation) into the hardware, with Normalised time version silently overflows... the CHERI CPU designed as part of the DARPA-funded CTSRD 2 project. We are already able to enforce object isolation in a 0 AutomaticManual Refcounts RefcountsGNUstep AppleManual Automatic RefcountsApple Manual Refcounts Refcounts dialect of C running on this CPU.

This research is sponsored by the Defense Advanced Research Projects Agency (DARPA) and the Air Force Research Laboratory (AFRL), under contract FA8750-10-C-0237. The views, opinions, and/or findings contained in this article/presentation are those of the author/presenter and should not be interpreted as representing the official views or policies, either expressed or implied, of the Defense Advanced Research Projects Agency or the Department of Defense.