Every spring, throngs of garden warblers make a treacherous, multiweek journey from their winter homes in Africa to their summer breeding grounds in Europe.

The small, brown-and-white songbirds fly thousands of miles, across the Sahara and the Mediterranean Sea, en route to their destinations. It’s an exhausting, arduous trip, and the warblers make numerous pit stops to rest and refuel along the way. During these layovers, the birds need to catch up on sleep, replenish their fat stores, and somehow manage to avoid being eaten by predators, including the hungry raptors that migrate alongside them.

A study published on Monday in Current Biology revealed one way that migrating warblers manage these dangers and demands: They adjust their sleep postures depending on their physical condition and physiological needs. Plump, well-muscled birds tend to sleep with their heads held upright, while scrawnier warblers tuck their heads into their feathers, a posture that makes them more vulnerable to predation but helps them conserve their much needed energy.

“Migratory warblers have to make trade-offs between staying safe and saving energy,” said Leonida Fusani, a behavioral physiologist at the University of Vienna and the lead author of the paper.