From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Are there any problems with lexical-let or other cl-macros??? Date: Tue, 01 Jun 2010 15:18:12 +0200 Organization: Informatimago Message-ID: References: <5ad73987-3540-44a3-b4b1-b83c92d92526@q23g2000vba.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291825774 12382 80.91.229.12 (8 Dec 2010 16:29:34 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 16:29:34 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 08 17:29:28 2010 Return-path: Envelope-to: geh-help-gnu-emacs@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 1PQMtU-0007AK-Hp for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 17:29:28 +0100 Original-Received: from localhost ([127.0.0.1]:53247 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQMqB-00051j-Jy for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 11:26:03 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 107 Original-X-Trace: individual.net mKz4VXpeO+7L5yeG4ImB1A7jE6DDuLYJweIMo3JL/8yxh/P0Yl Cancel-Lock: sha1:MDhlMjAxZjRkODEyYWFmNGM4ZmI2YmVmNjFlOTY5ODIxNWU3Y2UxZg== sha1:yIrzpM8I7UjNXi75CPA7OM2vs2g= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.1 (darwin) Original-Xref: usenet.stanford.edu gnu.emacs.help:178527 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:75611 Archived-At: LanX writes: > Hi > > http://steve-yegge.blogspot.com/2008/11/ejacs-javascript-interpreter-for-emacs.html > steve yegge complains about missing lexical vars roughly giving this > example > > (require 'cl) > (defun foo () > (setq x 7)) > > (defun bar () > (let ((x 6)) > (foo) > x)) ; you would expect x to still be 6 here > > (message (number-to-string (bar))) > > What I don't understand is that simply replacing let with lexical-let > will solve the problem. Esthetically, but the implementation is not efficient, and cannot be since the emacs VM (which is a very high level lisp VM) doesn't provide a way to implement lexical binding at all AFAIK (well perhaps using a lisp vector and mapping variables to offsets, but this probably would not be more efficient either). Let's compare the emacs VM (I introduce a (setf x (+ x x)) form to have both compiler generate more comparable code, clisp compilers optimizes out the original let): (disassemble (byte-compile (defun* bar () (lexical-let ((x 3)) (setf x (+ x x)) (foo) x)))) byte code: args: nil 0 constant 3 1 varbind --cl-x-- 2 constant --cl-x-- 3 symbol-value 4 constant --cl-x-- 5 symbol-value 6 plus 7 varset --cl-x-- 8 constant foo 9 call 0 10 discard 11 constant --cl-x-- 12 symbol-value 13 unbind 1 14 return (Note that these occurences of --cl-x-- above are all the same symbol, but it's not interned, so in a different (let ((x ...)) ...) it would be a different --cl-x-- symbol; we could have used a symbol named x too). With the clisp VM: (disassemble (compile (defun bar () (let ((x 3)) (setf x (+ x x)) (foo) x)))) WARNING in BAR : Function FOO is not defined Disassembly of function BAR (CONST 0) = 3 (CONST 1) = FOO 0 required arguments 0 optional arguments No rest parameter No keyword parameters 7 byte-code instructions: 0 (CONST&PUSH 0) ; 3 1 (LOAD&PUSH 0) 2 (LOAD&PUSH 1) 3 (CALLSR&STORE 2 55 0) ; + 7 (CALL0 1) ; FOO 9 (POP) 10 (SKIP&RET 1) NIL In the case of the emacs VM, it has to establish a dynamic binding with varbind, which is rather costly. References to variables must go thru the symbol: you need to keep the symbols around (space), and it goes thru the symbol (time, one indirection). In the case of the clisp VM, for lexical references the compiler can drop the variable name (spare the symbol), and the references to the lexical variable is done by direct addressing (relative to the frame, or in the case of clisp, the stack). clisp byte code is of lower level than emacs byte code, but it is bound to be more efficient at run-time. On the other hand, the clisp compiler is more complex than the emacs byte-compiler. -- __Pascal Bourguignon__ http://www.informatimago.com