Compile All Classes?

The ability to Compile All Code has been around as long as Apex Code has been a thing. You'll want to watch Peek Under the Hood of the New Apex Compiler, a Dreamforce 16 presentation that was later uploaded, that explains basically how the system works today, as well as plans for the near future. To summarize the video though, code is compiled mostly "on demand", and generally occurs the first time some code requests a class has that expired from the caches, usually because it has been modified or one of its dependencies have been modified.

Generally speaking, though, you don't usually need to use this feature in production. It's primary benefit is for use during development in a Sandbox or Developer Edition org. For example, given class A and B, if B is called from A, and you delete B entirely or change it, A is invalidated, and has to be recompiled the next time it's called. You can skip this compilation time for the first request after invalidation by forcing a compilation before you need it. This only speeds up the first call to the class after it's been invalidated, as they later use the cache, as explained in the video I linked.

If a piece of code is constantly performing slowly, it is because the code is slow (not optimized/bulkified/whatever). There should only be one "slow load" that occurs after each upgrade, not every single call, or until you "compile all classes." It's a convenience feature that's not often needed, and primarily of benefit during development to speed up load times while testing.

Also, as explained in the video, in the near future, compilation during deployment will occur before the actual deployment takes place, so deployments will take slightly longer, but will reduce the occurrences of the "slow load" that occurs for the first request after an upgrade, because the code will be pre-compiled and only has to be loaded from the cache.


Anytime you deploy code to your salesforce org, be it a managed package update or some custom code, all your classes and triggers are marked "invalid" and must be compiled at some point before they can execute again. For most orgs it is fine to let this occur automatically when the classes are referenced. However, for orgs with a lot of managed code and/or custom code, compiling all classes can improve performance.

Even more important -- for an increasing number of orgs (including mine), some managed functions FAIL TO RUN if there's too much code that needs recompiled during a request cycle. The error message that's returned is that a dependent class is invalid and needs recompiled and the error preventing a recompile is a red herring.

It's at the point where we need to run "Compile all classes" repeatedly and "Compile all Triggers" as well, and then run all our tests and finally the managed package will begin working again. This is a robust, well-know managed package and it is not any fault of theirs. Salesforce has acknowledged the issue is theirs, but have not corrected it yet.

The compile all triggers options isn't available by default. You have to request that it be added to your org if you're having this issue.