“Programming Phoenix” includes this statement:
“Templates are precompiled. Phoenix doesn’t need to copy strings for each rendered template. At the hardware level, you’ll see caching come into play for these strings where it never did before.”
This sounds awesome, but I want to understand how it works. Precompiled templates are definitely more memory-efficient, but so far, I’ve been unable to see a caching effect in action. I had a theory:
- If the same string is used for (eg) the header on each request, it will be found at the same location in memory (if it’s a refc binary)
- The completed template will be given to Cowboy as an iolist, and Cowboy will call
writev
on the socket with some of the same memory addresses each time - The fact that
writev
gets some of the same memory addresses for each request will signal the operating system to look for those strings in CPU cache
I’ve written a small Phoenix app to test this theory, but so far it seems to be wrong. (There’s a detailed writeup in the README.)
Can anyone point me to a way that Phoenix benefits from hardware caching, or tell me what I’m doing wrong in my experiment?
(I hope to discuss this in my ElixirConf Talk with @JEG2.)