From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Christopher Lam Newsgroups: gmane.lisp.guile.user Subject: Re: Pure (side-effect-free) calls into c/c++? Date: Sun, 12 Jan 2020 03:03:03 +0000 Message-ID: References: <365fc4d0-8c2e-4578-b38f-05ebf6c20064@www.fastmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="35238"; mail-complaints-to="usenet@blaine.gmane.org" Cc: guile-user To: =?UTF-8?Q?Linus_Bj=C3=B6rnstam?= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Jan 12 04:04:42 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iqTXb-0011gj-Lj for guile-user@m.gmane-mx.org; Sun, 12 Jan 2020 04:03:51 +0100 Original-Received: from localhost ([::1]:34732 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqTXa-0007yx-8q for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 22:03:50 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35070) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqTXL-0007ye-S8 for guile-user@gnu.org; Sat, 11 Jan 2020 22:03:37 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqTXK-0001ra-6m for guile-user@gnu.org; Sat, 11 Jan 2020 22:03:35 -0500 Original-Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]:46488) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iqTXJ-0001lD-SC for guile-user@gnu.org; Sat, 11 Jan 2020 22:03:34 -0500 Original-Received: by mail-ed1-x52b.google.com with SMTP id m8so5384504edi.13 for ; Sat, 11 Jan 2020 19:03:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pLWtgOYFgHrS7GDccZjoJ6j6mIuGKcXiC6P4KVp8dMc=; b=bn4eXwm7YYxkJI4Iw/9t5KgrnePLpIlTIdA241snlon9bmVLYrYQO8ax957td5X5O5 t133q+5QGqzNIOAVch/2ZCTg3/4yhF/4L0RO7BqbFTRwYYLmwAKCv/otqiKcSwwywjDt txMBmVx+R4PNBwLxLr0ighCp2gKGx5azJrW/4nXabh2Hyz7l+HVQncvxqLVyd9IQz/Ty x6dTCPXe5/zWXYGUbsOpz4ar6+x07T4d5pVvc4NcyAkY5LLLviA1St8Scku9Osht8f8W Y5NAM7sPDlS+oYwgmxgyz23dQ96gJsJX2J66dRRseMYfYgYcRZnEw1I8p877RLB7F72H 5nrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pLWtgOYFgHrS7GDccZjoJ6j6mIuGKcXiC6P4KVp8dMc=; b=WhqHd0G1n0ZcXfe6c5H4I3paGo7sjDKFyd2FggIgvpRWb3JkiFo3++fnAw/jRdvB7x b6t6mwFLk3zbUNp3f2jX0OglYX143jzwYWTVL9ke8/2zOjHmoVTEiqQfTRgf2IKSEG/d BbhYX8s2NM+qYdXyL5yeG6hGokMzEWOltPZzgOCwQQpBAj599l8dP3cJsA1736Q5O73I MxC4ZWUmImkJlB/Z8g/qSOyqBGIkpInzFp0rxIu+eLLbmB/NNIWJkSKbN7XmvNbdSrw+ f481hN7fhDGTnFBCOO0y9DT4GlAsou8hOe3KPwMC8/jwXnOqdpWvU+QChe/lPDHXCEGp lD2g== X-Gm-Message-State: APjAAAU2mkVRvrSnvuLmUM0r3LgaRD7C51Lcvq6JFPmaWPmwHO84so1r uWopBNoo4CYnMIJyFnOoD43FXi6P4udnfW3vncYGNLE0mP4= X-Google-Smtp-Source: APXvYqxtCIA9EamSHyxwrxY6TNvjAalZoawV9bTEQg6FWNu6Fo7rI1OF9L3BE1kAK0f420QfiGKZ2lKxzz+llw/nqyc= X-Received: by 2002:aa7:c611:: with SMTP id h17mr11058299edq.155.1578798210569; Sat, 11 Jan 2020 19:03:30 -0800 (PST) In-Reply-To: <365fc4d0-8c2e-4578-b38f-05ebf6c20064@www.fastmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::52b X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:16037 Archived-At: 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) =3D> 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=C3=B6rnstam 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 fro= m > 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=C3=B6rnstam > > 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 declar= e > > 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 th= e > > 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 > > > >