Hello Linas, On 1/10/20 11:36 PM, Linas Vepstas wrote: > So, I've got lots of C code wrapped up in guile, and I'd like to declare > many of these functions to be pure functions, side-effect-free, thus > hopefully garnering some optimizations. Is this possible? How would I do > it? A cursory google-search reveals no clues. > > To recap, I've got functions f and g that call into c++, but are pure (i.e. > always return the same value for the same arguments). I've got > user-written code that looks like this: > > (define (foo x) > (g (f 42) (f x) (f 43)) > > and from what I can tell, `f` is getting called three times whenever the > user calls `foo`. I could tell the user to re-write their code to cache, > manually: viz: > > (define c42 (f 42)) > (define c43 (f 43)) > (define (foo x) (g c42 (f x) c43)) > > but asking the users to do this is .. cumbersome. And barely worth it: `f` > takes under maybe 10 microseconds to run; so most simple-minded caching > stunts don't pay off. But since `foo` is called millions/billions of times, > I'm motivated to find something spiffy. > > Ideas? suggestions? > > -- Linas
I don't know exactly how to do it, but in theory, you could provide the user a macro, which looks for calls of `f` and makes it so, that these calls are only done once. My macro skills are not so great yet, so I don't know how to do that. I am just thinking, that in theory this should be possible, perhaps with a simplified case, that assumes, that the user does not redefine `f` inside the expression given to the macro. Just outputting the idea, not sure it is a good idea. Regards, Zelphir