From: "Linus Björnstam" <linus.internet@fastmail.se>
To: "Christopher Lam" <christopher.lck@gmail.com>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Pure (side-effect-free) calls into c/c++?
Date: Sun, 12 Jan 2020 11:35:30 +0100 [thread overview]
Message-ID: <e0cf1242-2279-4f38-a64c-6cd9fc4f9994@www.fastmail.com> (raw)
In-Reply-To: <CAKVAZZJ4HRLpKJ3XhjY2YCG50a=6k0i6DAm=0PBR7V99siq6AQ@mail.gmail.com>
Thanks!
I haven't used this macro in a billion years and wrote it as a comfort thingie for the first 100 project Euler problems.
This thread got me to start thinking about how to memoize "smarter" and having a macro that allows you to trade a bit of speed for being able to specifically memoize the last n or the most common n arguments.
--
Linus Björnstam
On Sun, 12 Jan 2020, at 04:03, Christopher Lam wrote:
> I can add a contribution! The good thing about memoize is it's simple
> to create. You forgot a catch however: if the memoized return-val is #f
> then your memoizer
> https://hg.sr.ht/~bjoli/misc/browse/default/memoize.scm will not
> recognise that #f is a valid cached return-val and will call the lambda
> again. (FWIW I shudder think what a *fast* memoizer would do).
>
> Here's how I did mine:
>
> (define (memoize f)
> (let ((h (make-hash-table)))
> (lambda args
> (cond
> ((hash-ref h args) => car)
> (else (let ((res (apply f args)))
> (hash-set! h args (list res))
> res))))))
>
> (define-syntax-rule (lambda/macro args body ...)
> (memoize (lambda args body ...)))
>
> (define-syntax-rule (define/macro (f . args) body ...)
> (define f lambda/macro args body ...))
>
>
>
> On Sat, 11 Jan 2020 at 17:40, Linus Björnstam
> <linus.internet@fastmail.se> wrote:
> > I have a macro called lambda/memo and define/memo for these situations: https://hg.sr.ht/~bjoli/misc/browse/default/memoize.scm
> >
> > If the function gets called with a gazillion different arguments the memoizatiin hash gets large, and there are no mechanisms to stop that from happening. It also lacks a fast path for single argument functions.
> >
> > You can disregard the repo license. Use that function is you like, if you like to.
> >
> >
> > --
> > Linus Björnstam
> >
> > On Fri, 10 Jan 2020, at 23:36, 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
> > > --
> > > cassette tapes - analog TV - film cameras - you
> > >
> >
prev parent reply other threads:[~2020-01-12 10:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-10 22:36 Pure (side-effect-free) calls into c/c++? Linas Vepstas
2020-01-11 14:13 ` Zelphir Kaltstahl
2020-01-11 14:38 ` Matt Wette
2020-01-11 18:11 ` Linas Vepstas
2020-01-11 18:52 ` Linas Vepstas
2020-01-11 21:56 ` Taylan Kammer
2020-01-12 2:15 ` Linas Vepstas
2020-01-12 4:12 ` Linas Vepstas
2020-01-12 2:12 ` Linas Vepstas
2020-01-12 3:21 ` Taylan Kammer
2020-01-12 4:32 ` Linas Vepstas
2020-01-12 5:52 ` Linas Vepstas
2020-01-11 17:40 ` Linus Björnstam
2020-01-12 3:03 ` Christopher Lam
2020-01-12 10:35 ` Linus Björnstam [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e0cf1242-2279-4f38-a64c-6cd9fc4f9994@www.fastmail.com \
--to=linus.internet@fastmail.se \
--cc=christopher.lck@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).