If you call this function on every node, then you stylesheet performance will be O(n^2) in the number of nodes.
The function is incorrect anyway. The preceding axis gives you your parent's preceding siblings and also the children of your parent's preceding siblings, so the string length of your cousins is being counted more than once.
Try defining a memo function something like this:
<xsl:function name="f:preceding-string-length" saxon:memo-function="yes">
<xsl:param name="n" as="element()"/>
<xsl:sequence select="sum(ancestor::*/preceding-sibling::*[1]/(f:preceding-string-length(.) + string-length(.)))"/>
</xsl:function>
Or use an XSLT 3.0 accumulator, which amounts to much the same thing.