all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nicolas Richard <nrichard@ulb.ac.be>
To: Marcin Borkowski <mbork@mbork.pl>
Cc: help-gnu-emacs@gnu.org, Barry Margolin <barmar@alum.mit.edu>
Subject: Re: looking-at-p slower than looking-at
Date: Thu, 26 Nov 2015 14:37:32 +0100	[thread overview]
Message-ID: <86ziy0khk3.fsf@members.fsf.org> (raw)
In-Reply-To: <87io4pzul5.fsf@mbork.pl>

Hi Marcin,

Marcin Borkowski writes:
> On 2015-11-25, at 14:58, Nicolas Richard <nrichard@ulb.ac.be> wrote:
>
>> Marcin Borkowski <mbork@mbork.pl> writes:
>>
>>> You might also (depending on your use-case) want to use looking-at-p,
>>> which is marginally slower than looking-at, but does not modify match
>>> data.
>>
>> Why is it slower and how much slower is it ? I don't see how it can
>> happen from its implementation:
>>
>> (defsubst looking-at-p (regexp)
>>   "\
>> Same as `looking-at' except this function does not change the match data."
>>   (let ((inhibit-changing-match-data t))
>>     (looking-at regexp)))
>
> One more function call and one more variable binding.  IMHO it /must/ be
> slower, though I think the effect is negligible.

I think there's not more function calls (in compiled code) since
looking-at-p is a defsubst (i.e. gets inlined by the byte compiler). My
own gut feeling is that setting inhibit-changing-match-data allows
`looking-at' to take shortcuts, thus making it actually faster.

> (setq foo "foo")
> (benchmark 10000000 (looking-at foo))
> (benchmark 10000000 (looking-at-p foo))

benchmark is a function, so (looking-at(-p) foo) is evaluated first,
returns nil, and that is what is given to benchmark as argument. IOW
it's not benchmarking anything. You need to quote the form or use
benchmark-run (or use M-x benchmark interactively).

You could also use benchmark-run or benchmark-run-compiled instead (they
are macros). I admit that it's not 100% clear from the docstring that
the situation is different, since they both name call their argument
FORM(S). Perhaps the macros have an argument named "BODY" instead of
"FORMS".

> (AFAIU, it is of utmost importance that you don't use literal stringso
> in such a test, since then it is much more probable that GC will kick
> in. Am I right?)

I think the string litterals are read and "consed" only once. I'll try
to convince you :

(benchmark 3 '(message "%c" (incf (aref "a" 0))))
Running this with C-x C-e, I get :
b
c
d
in my *Message* buffer.

Explanation : when you hit C-x C-e, emacs reads the sexp,
thus translating it in a list of symbols, numbers and strings. At this
point, "a" is thus a string in emacs memory. When the code is run, it
modifies "a" inplace : (incf (aref "a" 0)) returns ?b (which is what we
get in *Messages*) but modifies the string by side effect. So on the
second run, the code gets the same string, which is now "b". Similarly
for the third run, where the string is now "c".

It would have been different had I done :
    (benchmark 3 '(message "%c" (incf (aref (string ?a) 0))))
because now the string "a" is re-constructed at run time everytime.

Conclusion : I don't think you need to avoid string litterals to have
less GC.

Nicolas.



  parent reply	other threads:[~2015-11-26 13:37 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-14 16:11 How to test if the current line contains only white-spache? Rolf Ade
2015-11-14 18:42 ` Barry Margolin
2015-11-15 13:29   ` Marcin Borkowski
2015-11-16 21:15     ` Philipp Stephani
2015-11-25 13:58     ` looking-at-p slower than looking-at Nicolas Richard
2015-11-25 20:34       ` Marcin Borkowski
2015-11-25 21:28         ` Michael Heerdegen
2015-11-25 21:32           ` Marcin Borkowski
2015-11-26 13:37         ` Nicolas Richard [this message]
2015-11-25 22:39       ` Emanuel Berg
2015-11-26 13:58         ` Nicolas Richard
     [not found]     ` <mailman.675.1448463102.31583.help-gnu-emacs@gnu.org>
2015-11-25 15:48       ` Barry Margolin
2015-11-25 20:39         ` Marcin Borkowski
2015-11-15 19:18   ` How to test if the current line contains only white-spache? Rolf Ade
2015-11-17  0:26     ` Emanuel Berg
2015-11-17 20:38       ` Marcin Borkowski
     [not found]     ` <mailman.23.1447720376.31583.help-gnu-emacs@gnu.org>
2015-11-19  1:56       ` Rolf Ade
2015-11-19  2:05         ` Stefan Monnier
2015-11-19  2:18           ` Emanuel Berg
2015-11-19  2:23         ` Emanuel Berg
     [not found]         ` <mailman.202.1447899839.31583.help-gnu-emacs@gnu.org>
2015-11-19 13:54           ` Rolf Ade
2015-11-19 15:20             ` Marcin Borkowski
2015-11-19 23:58               ` Emanuel Berg
2015-11-20  6:14                 ` Marcin Borkowski
2015-11-21  2:26                   ` Emanuel Berg
     [not found]               ` <mailman.279.1447976946.31583.help-gnu-emacs@gnu.org>
2015-11-20  0:48                 ` How to test if the current line contains only white-space? Rolf Ade
2015-11-20  0:48                 ` Rolf Ade
2015-11-21  2:02                   ` Emanuel Berg
     [not found]                   ` <mailman.375.1448070734.31583.help-gnu-emacs@gnu.org>
2015-11-21  2:28                     ` Rolf Ade
2015-11-24  3:01                       ` Emanuel Berg
2015-11-26 17:51         ` How to test if the current line contains only white-spache? Alex Bennée
2015-11-26 23:59           ` predicates (was: Re: How to test if the current line contains only white-spache?) Emanuel Berg
     [not found]           ` <mailman.787.1448581762.31583.help-gnu-emacs@gnu.org>
2015-11-27  4:28             ` predicates Pascal J. Bourguignon
2015-11-27 19:52               ` predicates Emanuel Berg
2015-11-15  5:56 ` How to test if the current line contains only white-spache? Yuri Khan
2015-11-15  9:18 ` John Mastro
2015-11-25 14:04 ` Nicolas Richard
2015-11-25 20:36   ` Marcin Borkowski
2015-11-25 22:41   ` Emanuel Berg
     [not found] ` <mailman.676.1448463105.31583.help-gnu-emacs@gnu.org>
2015-11-26 15:24   ` Rolf Ade

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86ziy0khk3.fsf@members.fsf.org \
    --to=nrichard@ulb.ac.be \
    --cc=barmar@alum.mit.edu \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@mbork.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.