From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: Passing buffers to function in elisp Date: Wed, 22 Feb 2023 06:30:18 +0100 Message-ID: References: <87mt56hg4e.fsf@iki.fi> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="Lj0GnClpOJv/ygDn" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25861"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Feb 22 06:30:58 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pUhiT-0006YJ-Ri for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 22 Feb 2023 06:30:57 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUhhy-0007WH-TI; Wed, 22 Feb 2023 00:30:26 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUhhx-0007Vu-5R for help-gnu-emacs@gnu.org; Wed, 22 Feb 2023 00:30:25 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUhhv-0001Lq-1K for help-gnu-emacs@gnu.org; Wed, 22 Feb 2023 00:30:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=crC7rE6QJsDuLyN+Tb98hWaB4A5eWBELoUdPzZj/rPw=; b=P5vVGLFy6dTdORcGhQvlMlXfmQ XAcdX9UDKKR5RJAnWHOGoeM36n8mOMJr4HNBO9WMLI1i4/+UTZotEsIo910AWM2hchG4iAFeTAUbq HvGhYovfITorZ9qUo9BExwb0JGYKiGR3h8R80yrX3lOW//1u4aXK2AKxDagaLsxXcKIPdpXfDWLhb CWK8hKt8x6VxwRlXM81euz8gyLfmDLWO/ZSNWME00E8eYVurUE3puV+oRIjlunIuVR8RdpU0Cb9tA 2IBkrgYARFdDgc3LiktOxP8mdJeB297hzMhJa9e2AEIzxVASNvym9s8JswXBemyqKkyMUkZMhDPfs E6sLsmqQ==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.94.2) (envelope-from ) id 1pUhhq-0006cm-OG for help-gnu-emacs@gnu.org; Wed, 22 Feb 2023 06:30:18 +0100 Content-Disposition: inline In-Reply-To: <87mt56hg4e.fsf@iki.fi> Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:142807 Archived-At: --Lj0GnClpOJv/ygDn Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Feb 21, 2023 at 11:18:25PM +0200, Petteri Hintsanen wrote: > Hello list, >=20 >=20 > Alan J. Perlis said "A LISP programmer knows the value of everything, > but the cost of nothing." >=20 >=20 > I'm reading some bytes into a temp buffer, like so: >=20 > (with-temp-buffer > (set-buffer-multibyte nil) > (insert-file-contents-literally filename nil 0 64000)) >=20 > then I pass these bytes to functions for processing, like this >=20 > (func1 (buffer-string)) >=20 > or sometimes just part of them >=20 > (func2 (substring (buffer-string) 100 200)) >=20 > Now: >=20 > . does this generate garbage? (I believe it does.) It most probably does, but that will depend on the future history of buffer and whatever func1 does with its arg. If both are needed later, it isn't garbage (yet). They become garbage once they aren't needed. > . if there are many funcalls like that, will there be lots of garbage? > (I guess there will be.) See above. See, the documentation of `buffer string' hints that it is doing a copy. If you modify the string, the buffer will stay the same and vice-versa. If that is what you want, then go for it :-) > . is this bad style? (I'm afraid it is, hence asking.) See above: it depends. If you want func1 to operate on the buffer content, then you better pass it the buffer itself (actually a reference to the buffer, but that's "details" ;-) If you'd be surprised that func1 is able to change the buffer, then better pass it a copy: `buffer-string' seems a good way to do that. > Is it better just to assume in functions that the current buffer is the > data buffer and work on that, instead of passing data as function > arguments? That depends on your style and on the "contracts" you make with yourself (and ultimately, of course, on what you are trying to do: for each different purpose, some style will be clearer/more efficient -- ideally both, but life and things). > [Why am I doing like this? It is /slightly/ easier to write tests when > functions get their data in their arguments.] Then go for it. To accompany your nice Perlis quote above I offer "Premature optimization is the root of all evil", which is attributed to Donald Knuth (some say it was Tony Hoare). Keep an eye on things and be ready to notice whether it is creating performance problems. > Also: is it good idea to try to limit the number temp buffers > (with-temp-buffer expressions)? Or are they somehow recycled within the > elisp interpreter? Once the interpreter (well, it's a hybrid these days. Let's call it the "run time") can prove they aren't needed, it will get recycled, yes. If you are curious, just invoke (garbage-collect) after you have accumulated some. It will tell you what it found. Cheers --=20 t --Lj0GnClpOJv/ygDn Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCY/WoZAAKCRAFyCz1etHa RgBAAJ9Manj1mTRs6JzISekIqONcriA2MgCeIezZBgVMJeeNslSt+bhs6U6RgSY= =gkNF -----END PGP SIGNATURE----- --Lj0GnClpOJv/ygDn--