Hi, On Tue, Aug 21, 2012 at 7:37 AM, Ludovic Courtès <l...@gnu.org> wrote: > Hi, > > Noah Lavine <noah.b.lav...@gmail.com> 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.
What I was hoping to do is have a macro that is also identifier-syntax, so if format is used in a way that can't be macroexpanded, it falls back to the procedure. I think I've seen an example of this before, but I'm not sure where. > Another (IMO less elegant) option would be to have an optional compiler > optimization pass that would do something similar. Yes, but I agree it's less elegant, because it splits the format logic across two pieces of code, and puts some of it in the compiler, which shouldn't really have to know about it. This is a problem that I'd like to solve more generally, by letting functions tell the compiler how to optimize themselves, but that's a big project, and using a macro seemed like a much simpler way to get the same effect here. However, I wonder if the partial evaluator would actually solve this problem if it knew how to do cross-module inlining. That's another big project, but it could be a way to solve this problem and keep format as a function. > Thanks, > Ludo’. Thanks, Noah