Bottom line: string-pixel-width is hardly efficient enough when
invoked in a tight loop, so if you need to call it many times for many
strings, perhaps a better idea would be to insert all the strings into
a work buffer, then measure their width one by one using
window-text-pixel-size, each time calling it with different FROM and
TO values? That involves switching a window's buffer and then
switching it back, but maybe it will be faster?
Alternatively, if you only need the width of the widest string in a
collection, you could concatenate them separated by newlines, then
call string-pixel-width on the result.
It took me a while to understand what this means, that's some leaky abstraction we have here :)
I've found a solution after some refactoring. The simplest solution is just don't use relative :align-to. As long as it's not used, `string-pixel-width` will behave as expected.
Thanks for all your help.