From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: Any disadvantages of using put/get instead of defvar? Date: Fri, 21 Feb 2014 10:39:30 +0100 Message-ID: <87bny0x0rx.fsf@thinkpad-t61.fritz.box> References: <87ob218lsu.fsf@thinkpad-t61.fritz.box> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1392975605 32737 80.91.229.3 (21 Feb 2014 09:40:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 21 Feb 2014 09:40:05 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Oleh Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 21 10:40:14 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WGmab-0004Ei-KF for geh-help-gnu-emacs@m.gmane.org; Fri, 21 Feb 2014 10:40:13 +0100 Original-Received: from localhost ([::1]:43325 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGmab-0006fn-7x for geh-help-gnu-emacs@m.gmane.org; Fri, 21 Feb 2014 04:40:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34335) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGmaI-0006dO-L6 for help-gnu-emacs@gnu.org; Fri, 21 Feb 2014 04:40:00 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGma7-0001OH-Vh for help-gnu-emacs@gnu.org; Fri, 21 Feb 2014 04:39:54 -0500 Original-Received: from out5-smtp.messagingengine.com ([66.111.4.29]:42868) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGma7-0001O6-RD for help-gnu-emacs@gnu.org; Fri, 21 Feb 2014 04:39:43 -0500 Original-Received: from compute1.internal (compute1.nyi.mail.srv.osa [10.202.2.41]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id 2CB4E20A45; Fri, 21 Feb 2014 04:39:43 -0500 (EST) Original-Received: from frontend2 ([10.202.2.161]) by compute1.internal (MEProxy); Fri, 21 Feb 2014 04:39:43 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=from:to:cc:subject:references:date :in-reply-to:message-id:mime-version:content-type; s=smtpout; bh=lnXI6iEocNgWhrHMbvwdsK33PnM=; b=r93OQDsXPcnGu8y5NV8PcTCTDEqo DqYfLm9OtDMf8BX+fx2u9SILBQTHY3cJvQjf8oTL73Pr4UiQkbl0Eh2GrPQ1lgcT ZGhMaw8EnvqjeGw4+sECwnrb3Cv3NvZbYe+vgjGoPmK3YRDDXLA4fHCuitPbI96g Y0bToNSlZJ1mnKw= X-Sasl-enc: mg2cpe5MmWgxHrNasPiwr6awwoQew2qpruM+VV5aBEMb 1392975582 Original-Received: from thinkpad-t61.fritz.box (unknown [95.88.165.230]) by mail.messagingengine.com (Postfix) with ESMTPA id 99C8F68021A; Fri, 21 Feb 2014 04:39:42 -0500 (EST) In-Reply-To: (Oleh's message of "Fri, 21 Feb 2014 10:12:44 +0100") User-Agent: Gnus/5.13001 (Ma Gnus v0.10) Emacs/24.3.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 66.111.4.29 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:96148 Archived-At: Oleh writes: >>> The situation is that I have a function that uses one global variable. >>> It's for sure that no other function will want this variable. In an >>> effort to have all code in one place I want to move from: >>> >>> (defvar bar-foo 1) >>> (defun bar () >>> ;; use bar-foo here >>> ) >>> >>> to: >>> >>> (defun bar () >>> (let ((foo (or (get 'bar 'foo) 1))) >>> ;; use foo here >>> )) >>> >>> So the advantage is that I can move and rename the function without >>> worry that the function/variable coupling will break, because now >>> everything is inside one function. >> >> You could also define the variable inside the function, i.e., that's a >> buffer-local counter: >> >> (defun counter () >> (defvar counter-var 1) >> (setq-local counter-var (1+ counter-var))) >> > > Thanks, Tassilo, > > But doesn't `defvar` introduce overhead this way? Well, I've measured my counter above versus a version using symbol properties as you suggest: (defun bar () (let ((foo (or (get 'bar 'foo) 1))) (put 'bar 'foo (1+ foo)))) My counter is way faster although it uses defvar and setq-local, so that overhead is still small compared to looking up/putting a symbol property. Bye, Tassilo