How Julia works
After this whirlwind tour of Julia, we want to see why Julia is a good fit for the web world. To do that, we must have a good understanding of Julia’s internal workings.
The Julia JIT compiler works at runtime, starting from the Julia source code. Note that code from packages is most often precompiled. Type, method, and module definitions are written in an efficient serialized form so that the JIT can start compiling much faster. The first time a function is called with a certain combination of types of arguments, the correct machine code for those types is generated through LLVM. Moreover, the machine code is cached from then on, so after the initial compilation stage, the optimized code is looked up in the function’s vtable (see Types, flow controls, and functions), and you get the bonus of much-improved performance.
Julia apps will often be long-running processes, so there needs to be a mechanism for freeing memory resources. The Julia developer, however, is not burdened with this task. Julia has a garbage collector process. This is a simple mark-and-sweep GC causing low overhead. The best advice here is as follows:
- Avoid unnecessary memory allocations
- Use standard-library methods that modify variables (whose name ends with
!
, for example,sort!
) instead of creating new ones - Use immutable objects (
const
andstruct
) - Pre-allocate enough memory from the start to avoid GC altogether
Now that we know somewhat better how Julia works, we can argue why Julia can be used for web development.