Earlier today, a coworker asked what exactly Apex was. My lazy answer for those unfamiliar with Salesforce development is usually “it’s like Java, but for Salesforce.com,” to which he replied “is it built on Java?”
I realized that I truly do not know – a few searches turn up a lot of answers such as “Apex itself is based on Java” but I realized “based on” might be referring to the syntax and not the actual language that it compiles to. The syntax and conventions are obviously very similar, but was this just for familiarity’s sake?
tl;dr – what does Apex compile into on the Salesforce provider side?
@mattandneil showed me this, which explains that prior to January 2012, Apex would be compiled into an Abstract Syntax Tree (AST) node graph which would then be traversed for every request to that class! The AST object would be serialized and cached in memcached, and then be deserialized and re-traversed upon subsequent requests. This was obviously quite expensive!
Starting January 2012, the new (and current – April 2015) Apex runtime compiles directly to Java bytecode, eliminating the AST completely. While it must still be serialized and deserialized, the cost of this for bytecode is trivial when compared to the Apex AST.
The more you know 😉