From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Pure (side-effect-free) calls into c/c++? Date: Sat, 11 Jan 2020 15:13:10 +0100 Message-ID: <42dc6ecc-ef82-2722-85c9-4f4ce4c42088@posteo.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="267737"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 Cc: guile-user@gnu.org To: linasvepstas@gmail.com Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jan 11 15:14:40 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 1iqHW4-000tvU-LR for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 15:13:28 +0100 Original-Received: from localhost ([::1]:57882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqHW3-00070n-EL for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 09:13:27 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51344) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqHVt-00070h-PF for guile-user@gnu.org; Sat, 11 Jan 2020 09:13:18 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqHVs-0007vT-Dl for guile-user@gnu.org; Sat, 11 Jan 2020 09:13:17 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]:38489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iqHVr-0007i7-TL for guile-user@gnu.org; Sat, 11 Jan 2020 09:13:16 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 4339F24010D for ; Sat, 11 Jan 2020 15:13:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1578751993; bh=NZjatsJrpaGYBgvD4mOMzOPy3QgSD/GhGn8qHwBL/J8=; h=Subject:To:From:Cc:Date:From; b=lvd3vPYiYq0GTHsXWYfj25CTEKLcRRr78t0EU9Si5JSNYPG5Gr6pwJkj9V3/3VcNI KbUXT46bWi9n+IAUc1ALGkJ3RRZWbMVYP8d57DLUnN897A9tg73bpM/bi5PGOaMaeS DId4waOKPQGQVUtR3tKNjHnwOKqBnmZicxekufmnWq8A7FIQ+cObufIWKctdXxAdiW gpmIHCRYNHHUucOvTHhE4/jEpdtWLMQba1O7FqoqYDhWPOxf+nobaMraZxuMuVZtMP orfzC9yxIx3NJmRFdn9++LZrFqCjdZPOhfvtms3ymyGLv/14T8hsX2GYqUiCkcjZY9 0K646SZjgqSMA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 47w1zC11q9z9rxN; Sat, 11 Jan 2020 15:13:10 +0100 (CET) In-Reply-To: Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 185.67.36.66 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:16029 Archived-At: 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