Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Example / partial fix for printf with math expressions



On Sat, Feb 24, 2024 at 10:54 PM Stephane Chazelas
<stephane@xxxxxxxxxxxx> wrote:
>
> 2024-02-24 14:40:41 +0000, Stephane Chazelas:
> [...]
> > +                     zlongval = (curarg) ? mathevali(metafy(curarg, curlen, META_HEAPDUP)) : 0;
>
> That incur a performance penalty:
>
> The numbers end up being metafied (earlier), unmetafied by
> printf, metafied again by printf here and that metafication
> processed by the math handler I guess.

The reason this went unobserved for so long is that metafication only
matters to a couple of things in math:
1) proper handling of parameter references, like your a[x]++ example
2) those pesky error messages that started this whole thread

As far as I can tell there are no other circumstances in which
metafication is necessary for the syntax of math expressions.

> Maybe the performance could be *improved* instead if the
> unmetafication could be skipped altogether in printf in those
> cases?

That's certainly possible, but it would mean that every branch of the
big "case" statements for %-replacement would have to do their own
unmetafy(), rather than a single loop at the top covering the entire
argv array.  The -s/-S/-z options also reverse the unmetafy(), so some
heavy refactoring of bin_print() is needed to unroll this completely.

However, I think you could change META_HEAPDUP and META_USEHEAP to
META_NOALLOC, becuase you're re-metafying back into the same space
that was originally unmetafied?  That might cut the performance
penalty a lot.




Messages sorted by: Reverse Date, Date, Thread, Author