This animation follows a bunch of electrons after they have been through the first LINAC of an XFEL. As they enter a second LINAC, shown as the copper construction in this animation, they have almost identical energies. The phase of the RF source of the LINAC is set such that the electrons at the back of the bunch gain more energy than those at the front as they pass through it. As all the electrons are anyway highly relativistic, this gain in energy of the rear electrons cannot help them advance on the forward electrons by increasing their velocity - instead, the energy is invested primarily into increasing their mass. By the time they exit the LINAC, the electrons have a clear energy distribution, indicated in this cartoon by the rear, high-energy electrons being blue, the front, lower-energy electrons being red. They now are forced through a magnetic chicane, shown by the four blue arrows representing four magnet dipoles. The high-energy (blue) electrons are bent less by the magnetic field than are the low-energy (red) electrons, meaning the former execute a shorter path than the latter, enabling the higher-energy electrons only now to catch up with the lower-energy electrons, resulting in a compression of the bunch length.
The code has an option to do a rotational pan. Comment this out and use a fixed view angle if you find this disturbing.
Cartoon showing mechanism behind bunch compression