Subject: bug#12033: closed (Re: bug#12033: format should be faster)

From: [email-masked] (Ludovic Courtès)
Date: Tue, 21 Aug 2012 13:37:29 +0200

Hi,

Noah Lavine <[email-masked]> skribis:

>> --------------------code-------------------
>> scheme@(guile-user)> ,profile (let lp ((i 10000)) (if (> i 0) (begin
>> (format #f "0x~2'0x, 0x~2'0x, 0x~2'0x" i i i) (lp (1- i)))))
>> %     cumulative   self
>> time   seconds     seconds      name
>> 22.58      0.56      0.23  tilde-dispatch
>> 12.90      1.00      0.13  format
>> 12.90      0.13      0.13  number->string
>>   8.06      0.13      0.08  format:out-char
>>   4.84      0.80      0.05  format:format-work
>> --------------------end-------------------
>>
>> In this case, we tried "0x~2'0x" and it's so slow that we can't bare it.
>> i=10000 is fast, but we need (* 600 80000)
>> And we found that "tilde-dispatch" cost too much. Is there any possible
>> to optimize it?
>
> It seems clear that in this case, Guile "should" know how to dispatch
> on the format string just once, outside of the loop, instead of doing
> it in every iteration.

I think Andy would say: “inline cache!”.  :-)

> What do people think of declaring format as a macro?

That’s tempting, but it breaks the ABI (so not for 2.0), and it breaks
for users who do ((@ (ice-9 format) format) #t "foo"), for instance.

Maybe we could have a ‘format*’ macro that does as much as possible of
the dispatch at compile-time?  The difficulty would be to factorize
dispatch code with the ‘format’ procedure.

Another (IMO less elegant) option would be to have an optional compiler
optimization pass that would do something similar.

Thanks,
Ludo’.