From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.bugs Subject: Re: Quite sneaky bug in SRFI-9 Date: Fri, 11 Mar 2011 14:55:26 +0100 Message-ID: <87aah1n4mp.fsf@gnu.org> References: <87fwqvg6xk.fsf@gmx.at> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1299851753 16523 80.91.229.12 (11 Mar 2011 13:55:53 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 11 Mar 2011 13:55:53 +0000 (UTC) To: bug-guile@gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Fri Mar 11 14:55:49 2011 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Py2om-00062i-Lr for guile-bugs@m.gmane.org; Fri, 11 Mar 2011 14:55:49 +0100 Original-Received: from localhost ([127.0.0.1]:50870 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Py2om-0006ri-5g for guile-bugs@m.gmane.org; Fri, 11 Mar 2011 08:55:48 -0500 Original-Received: from [140.186.70.92] (port=45832 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Py2oh-0006qG-5e for bug-guile@gnu.org; Fri, 11 Mar 2011 08:55:44 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Py2of-0000AB-UV for bug-guile@gnu.org; Fri, 11 Mar 2011 08:55:43 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:46028) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Py2of-00009m-IX for bug-guile@gnu.org; Fri, 11 Mar 2011 08:55:41 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Py2oc-0005w1-CW for bug-guile@gnu.org; Fri, 11 Mar 2011 14:55:38 +0100 Original-Received: from 193.50.110.208 ([193.50.110.208]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Mar 2011 14:55:38 +0100 Original-Received: from ludo by 193.50.110.208 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Mar 2011 14:55:38 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 101 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 193.50.110.208 X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 21 =?iso-8859-1?Q?Vent=F4se?= an 219 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu User-Agent: Gnus/5.110013 (No Gnus v0.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:EW2QfiEkIbpqUIXBmCOVd9tw1Sg= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: bug-guile@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:5315 Archived-At: --=-=-= Content-Type: text/plain Hi Andreas, Andreas Rottmann writes: > Hi! After being puzzled for some time, I found the root cause for an > issue with my code was actually a quite serious bug in SRFI-9's > define-inlinable' (here we go again -- but this time it's an actual > bug, I promise ;-)): The current implementation of `define-inlinable' > duplicates actual parameters passed to the macros it generates, for > example (note the `(find-next-solution! s 10000)' expression): > scheme@(guile-user)> (tree-il->scheme (macroexpand '(solution? > (find-next-solution! s 10000)))) > $6 = (if ((@@ (srfi srfi-9) struct?) (find-next-solution! s 10000)) > ((@@ (srfi srfi-9) eq?) ((@@ (srfi srfi-9) > struct-vtable) (find-next-solution! s 10000)) > (@@ (dorodango solver) solution)) > #f) > Ouch, good catch! > - It's certainly not as efficient as the current, broken implementation. > Ideally, the expansion would be a `let' block instead of the > `call-with-values' invocation. Yes, that would be nice. This patch seems to do the trick: --=-=-= Content-Type: text/x-patch Content-Disposition: inline diff --git a/module/srfi/srfi-9.scm b/module/srfi/srfi-9.scm index fad570b..54bc8a8 100644 --- a/module/srfi/srfi-9.scm +++ b/module/srfi/srfi-9.scm @@ -64,6 +64,12 @@ (cond-expand-provide (current-module) '(srfi-9)) +(define-syntax letify + (syntax-rules () + ((_ (var ...) (val ...) body ...) + (let ((var val) ...) + body ...)))) + (define-syntax define-inlinable ;; Define a macro and a procedure such that direct calls are inlined, via ;; the macro expansion, whereas references in non-call contexts refer to @@ -80,15 +86,17 @@ (syntax-case x () ((_ (name formals ...) body ...) (identifier? #'name) - (with-syntax ((proc-name (make-procedure-name #'name))) + (with-syntax ((proc-name (make-procedure-name #'name)) + ((args ...) (generate-temporaries #'(formals ...)))) #`(begin (define (proc-name formals ...) body ...) (define-syntax name (lambda (x) (syntax-case x () - ((_ formals ...) - #'(begin body ...)) + ((_ args ...) + #'(letify (formals ...) (args ...) + (begin body ...))) (_ (identifier? x) #'proc-name)))))))))) --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit I can’t think of any way to do it more elegantly. > Has anyone done benchmarks of the benefits of the current (broken) > SRFI-9 accessors and predicates vs. regular procedures on real code? > Given the argument duplication issue, it might be the case that > `define-inlinable' actually slowed things down :-). I benchmarked it indirectly via ‘vlist-cons’ et al. in ‘vlists.bm’. The bonus is that there are special opcodes for ‘struct-vtable’, ‘make-struct’, and ‘struct-ref’. So when you inline constructors and accessors, you also get to use directly those opcodes, which makes quite a difference. I would like to keep this property. Not doing this gives an incentive to using lists everywhere since ‘car’, ‘pair?’, etc. are open-coded. Thanks! Ludo’. --=-=-=--