[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: `looking-back' strange warning
From: |
Tassilo Horn |
Subject: |
Re: `looking-back' strange warning |
Date: |
Fri, 02 Oct 2015 22:59:41 +0200 |
User-agent: |
Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) |
Drew Adams <drew.adams@oracle.com> writes:
>> > My two cents: Even if you specify an optimal limit, `looking-back' can
>> > still be unnecessarily slow. For example,
>> > (looking-back "xy" 2)
>> > is much slower than
>> > (and (> (point) 2)
>> > (save-excursion (goto-char (- (point) 2))
>> > (looking-at "xy")))
>> > where "xy" stands for any plain string.
>>
>> Definitely. And this is in fact a typical case of the
>> misuse: the string to check is often a literal, so its
>> length is known.
>>
>> This is the first thing to mention about `looking-back',
>> in terms of performance: avoid it altogether, if you can.
>
> BTW/FWIW -
> I use this function in such cases, which can be pretty common:
>
> (defun chars-before (chars)
> "Return non-nil if the literal string CHARS is right before point.
> This is more efficient that `looking-back' for this use case."
> (let* ((len (length chars))
> (idx (1- len))
> (pt (point)))
> (catch 'chars-before
> (dolist (char (append chars ()))
> (unless (condition-case nil
> (eq char (char-before (- pt idx)))
> (error nil)) ; e.g. `bobp'
> (throw 'chars-before nil))
> (setq idx (1- idx)))
> t)))
This version is about 6 times faster in the t case and a still a bit
faster in the nil case where the char before point is already different
(which is the best case for your function).
(defun chars-before (chars)
"Return non-nil if the literal string CHARS is right before point.
This is more efficient that `looking-back' for this use case."
(let ((beg (- (point) (length chars))))
(unless (< beg 0)
(save-excursion
(goto-char beg)
(looking-at (regexp-quote chars))))))
Bye,
Tassilo
- RE: `looking-back' strange warning, (continued)
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- Re: `looking-back' strange warning, Dmitry Gutov, 2015/10/08
- Re: `looking-back' strange warning, Andreas Röhler, 2015/10/08
- Re: `looking-back' strange warning, Dmitry Gutov, 2015/10/08
- Re: `looking-back' strange warning, Andreas Röhler, 2015/10/08
- Re: `looking-back' strange warning, Stefan Monnier, 2015/10/08
- Re: `looking-back' strange warning, Michael Heerdegen, 2015/10/08
- Re: `looking-back' strange warning, Michael Heerdegen, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- Re: `looking-back' strange warning,
Tassilo Horn <=
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- Re: `looking-back' strange warning, Tassilo Horn, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- Re: `looking-back' strange warning, Andreas Röhler, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08
- Re: `looking-back' strange warning, Andreas Röhler, 2015/10/08
- Re: `looking-back' strange warning, Michael Heerdegen, 2015/10/08
- Re: `looking-back' strange warning, Stefan Monnier, 2015/10/08
- RE: `looking-back' strange warning, Drew Adams, 2015/10/08