Well, maybe this optimization could be used in Twig for 5.3+, still compiling with the duplicate expression for 5.2. there is already some optimized compilation for 5.4+ IIRC (or at least there used to be)