From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Linas Vepstas Newsgroups: gmane.lisp.guile.user Subject: Re: Pure (side-effect-free) calls into c/c++? Date: Sat, 11 Jan 2020 12:11:04 -0600 Message-ID: References: Reply-To: linasvepstas@gmail.com Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="130954"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Guile User To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jan 11 19:12:51 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 1iqLES-000O9m-QA for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 19:11:32 +0100 Original-Received: from localhost ([::1]:59856 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqLER-0005m0-L0 for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 13:11:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44421) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqLEG-0005lf-FB for guile-user@gnu.org; Sat, 11 Jan 2020 13:11:22 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqLEE-0000db-NX for guile-user@gnu.org; Sat, 11 Jan 2020 13:11:20 -0500 Original-Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]:46628) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iqLEE-0000bB-DT for guile-user@gnu.org; Sat, 11 Jan 2020 13:11:18 -0500 Original-Received: by mail-lj1-x234.google.com with SMTP id m26so5476569ljc.13 for ; Sat, 11 Jan 2020 10:11:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to:cc; bh=6U/Ucc+YrURJK2w7D+USsTm4NViaTtCEL7mcR2CV9NU=; b=VOlith5wgMTGU2DuJ6MxPMooDneYuy84xjcPs6qa5jJsqN1dcznkH1+ITo7A2RjqCH GsLHmpFEoJMx8UgDIOEjn2mEEPyEQqQQp/KP1gAirJ6h9muHyfR5JLlNuk8ELCavh+Oz lKh6QbQXqs8i5XzTOAfKcuVm2RjDbdZ4SlpXSXqYEG2CTvf3lL5HLSUxh8t/idDuhPYs 6M13PmvqYLQzmYsDYE97iNSEe25C9lMIBVnM7KECA7PzXEo5vz6Fr0OExpHgar4d8QX3 S2vnQXYAZGdBpP1dVcckZyt49UD4bi8eBXAvsrCqXH+mM9CSLXXvxx637Ld+qsIa3uE7 4guQ== 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:reply-to :from:date:message-id:subject:to:cc; bh=6U/Ucc+YrURJK2w7D+USsTm4NViaTtCEL7mcR2CV9NU=; b=GSD8psSaIfDkNu3LMCyvEqMTxf2plmEOPeSlDQsQEPL2xfiVwC4cbueqmT6hOSZgTK fIwd+1JHmezHqeEgBAu3Sq5Uu1UogSV7Q8/OFRfoY1EWHWpAJ1vVW+DSUV1gqC4GUu5c zgQF/uLGRaB4bY+KsxenTvKO4U5R6Wt+qpDO9TmHLYIAbq2zcyUNNCa5H/NlDucTmXhb ry0bndlVUfWDdWIV+VG5/uwHVwUNTv6EzxvHwcWBwSzy9bIA1bkLkLz2ErzQXkl9KACV iRNTU5oePnsU5Z7yd27+zaLeTcF9qgzbyjlUjxbejafighjWh633Ygzwhb7w+Ffor0fU yKXw== X-Gm-Message-State: APjAAAWo1SiUQMwOp95pmnOg/BDasE5GExYa+Z0vQAGbBvRrsLcuwt7g GTtQr7WZlRfWzmlu+N1820cbjjJ5ERoAdYiIeGg= X-Google-Smtp-Source: APXvYqzOBCIMjJqQ7mizG8NzHSiLPwhpLAnCClhKBTWuekgUvDjm4XAlVJs1JYyq+KUnNK/1H9QKGXAn454x4Cfw8/Y= X-Received: by 2002:a2e:858f:: with SMTP id b15mr4182541lji.275.1578766277074; Sat, 11 Jan 2020 10:11:17 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::234 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:16032 Archived-At: Hmm Thanks. Perhaps I should have been more clear. I'm talking about a handful of values that behave like constants, and NOT about memoization. So here's a bit more detail. The only thing the C++ code is doing is stuffing the values into a giant hash table... in C++. So its already very fast. The return value is a pointer into that hash table. Replicating that hash table a second time, in guile is ... well ... it has tens/hundreds of millions of entries, it would blow out RAM. (And the other functions, those that are actually CPU-intensive, already got the Grand Wazoo treatment for memization; some in guile, some in C++) For this particular issue, I'm thinking I need to understand macros, somehow. The point being that there is just a tiny handful of values -- some dozens, maybe hundreds, that some human has written into the code, and are being treated as if they were literal constants (because, in the C++ code, they *are* constants -- they're really just fixed entries in a symbol table) and so I want to automatically replace these by the actual constant value (the location in the c++ table). To recap: a macro that would convert (define (foo x) (g (f 42) (f x) (f 43)) into (define c42 (f 42)) (define c43 (f 43)) (define (foo x) (g c42 (f x) c43)) so that guild can treat c42 and c43 as constants (boxes, I guess). -- Linas On Sat, Jan 11, 2020 at 8:39 AM Matt Wette wrote: > On 1/10/20 2: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 > > read this: http://community.schemewiki.org/?memoization > and look at https://docs.racket-lang.org/memoize/index.html > > > > -- cassette tapes - analog TV - film cameras - you