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:52:32 -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="211061"; 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:55:15 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 1iqLsd-000Z5w-MW for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 19:53:03 +0100 Original-Received: from localhost ([::1]:60054 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqLsc-0003Pv-Eq for guile-user@m.gmane-mx.org; Sat, 11 Jan 2020 13:53:02 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33437) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iqLsQ-0003Pp-5C for guile-user@gnu.org; Sat, 11 Jan 2020 13:52:52 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iqLsO-0007I7-6h for guile-user@gnu.org; Sat, 11 Jan 2020 13:52:50 -0500 Original-Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]:39126) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iqLsN-0007CP-QG for guile-user@gnu.org; Sat, 11 Jan 2020 13:52:48 -0500 Original-Received: by mail-lj1-x234.google.com with SMTP id l2so5563191lja.6 for ; Sat, 11 Jan 2020 10:52:47 -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=BoimEHTKsZojWla7TuX3SX65bne7l6NSdilDDn3igcU=; b=JmAahIwhLJFBdcV7pgz/AnKpIeWtbUK8pA7QeS3DZjR8ulgLqJzrCtPdk31ueB3UCf lZBo6SywEPxxzlYs99rCfbJWSc6BpKCLAC1PZCF9RNrIGlCDhg1oz1nNLz/W0sIY/reS cZd89O3lEVBWt9sndoUb9oeZtxTuMqtXYDl6Fo3FrfkT6b5pvLrZddqkigk0GyaWVhvy lLSwcEqm7ca+ajaxmpSrGipyilX4Jb6C9bdFOXsvbQTF9GI6My65pyF+fXoi0A04BOL9 zVqnWg2i3GnkTdEn54rR5T42PjPvwQoI+9yHacozl+yh80/ZS9Nsnu2gRHZtRFxIn9/g TnlQ== 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=BoimEHTKsZojWla7TuX3SX65bne7l6NSdilDDn3igcU=; b=ii0BVAiFA4RrlqDP7W2v/UMj1qKRhTR2c/QROgIOhzb7ubPU+WeCk0pvIuRcOpMT83 zlM1y6MH2WAA10W28e2wZRvSd0m+cXk8idMLDYt/OTfc8JM99ttfnzkzDx9pGMsmfX0/ eYOw08IWPkug88J8trwrk3pOmKxygvLA7JM0E7f+kjwcjiwEl9Q4CjjkXuaU73W5YsS3 dqIC+KT04Q1MJx5zfTFgsVOlAu+/vza+ZfmTMemkLtAmEgOb2WC1JJbGsHzgIeoAgpjz OrosbOqjHtHbhJcP43Vhk8o5JC8CSbkfBOxJ4U5P4mG6y4ewFRTkHzGBa/VCfIBTGzaW HadA== X-Gm-Message-State: APjAAAXMY4iVrnLuG6r4c5Kco7SoWG/PWoBBeQ9F7eHvyw+SXbwk+SQA vhqRMx1GfTDmpcJk8RZywWnJuEdAjRwdoYlhgxk= X-Google-Smtp-Source: APXvYqwW/vEBedR/+HVUZsxbPSsCpohR3P8ePYBFMm79gswp8in8KCrD1AlyD64OnizGqhoAxAQ9HOX6WD6xk1rhj4U= X-Received: by 2002:a2e:9cc:: with SMTP id 195mr6401908ljj.130.1578768765165; Sat, 11 Jan 2020 10:52:45 -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:16033 Archived-At: Or, thinking aloud a bit: boxes and symbols.... So, for example, if I was able to tell apart calls (f 42) from calls (f x) where x is a "symbol" (or "variable", *see below*) referencing an integer, then, for the former case, I could create a new symbol (in guile) and attach it to a box, fill that box with whatever (f 42) would have returned. Thence-forward, any call site in the guile code that had (f 42) in it would get replaced by the symbol (or the unboxed value)... At the moment, I don't know how to tell apart 42, the literal number, from x, a symbol that references a number. (Somehow, I can't make symbol? work ...) I also don't know how to "edit" the call site (the cell, the box?) that has (f 42) as the call-target, and replace it by a constant (or a boxed constant). But my naive thinking fails: (define x 0) (symbol? x) => #f (variable? x) => #f So I guess that x is not a symbol, from the guile point of view!? This is .. confusing. What is x, then, if not a symbol? (define (foo x) (format #t "its ~A ~A\n" (symbol? x) (variable? x)) (+ x 1)) (foo 42) its #f #f (define y 66) (foo y) its #f #f How can I tell apart "42" from "y" ? -- Linas On Sat, Jan 11, 2020 at 12:11 PM Linas Vepstas wrote: > 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 > -- cassette tapes - analog TV - film cameras - you