[m-rev.] for review: optimize calls to string.format and related predicates
Julien Fischer
juliensf at csse.unimelb.edu.au
Thu Sep 3 21:34:45 AEST 2009
On Wed, 2 Sep 2009, Zoltan Somogyi wrote:
> Besides a review of the change itself, I am seeking feedback on what
> exactly the optimization should do.
>
> For optimizing e.g. io.format("%d_%d", [i(X), i(Y), !IO), this diff currently
> generates
>
> V1 = int_to_string(X),
> V2 = "_"
> V3 = V2 ++ V1
> V4 = int_to_string(Y),
> V5 = V4 ++ V3
> io.write_string(V5, !IO)
>
> Should it instead generate
>
> V1 = int_to_string(X),
> io.write_string(V1, !IO),
> V2 = "_"
> io.write_string(V2, !IO),
> V3 = int_to_string(Y),
> io.write_string(V3, !IO)
>
> This avoids memory allocation, but it requires more overheads in the I/O
> system, e.g. retrievals of the current output stream. What do you think?
The former seems preferable in the case where X, Y or both are (become)
known, e.g. through the predicate being inlined, since that may lead to
some of the string concatenations being optimised away.
Although, it mightn't be if we had an optimisation that merged
consecutive calls to io.write_string/{3,4}. Something like:
io.write_string("Hello", !IO),
io.write_string(" World", !IO),
into
io.write_string("Hello World", !IO)
Most of the time it won't matter which of the two choices are applied -
for those where it does I wonder whether we could use the profiling feedback
system to tell us what the better choice is.
Julien.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list