From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Andreas =?UTF-8?Q?R=C3=B6hler?= Newsgroups: gmane.emacs.bugs Subject: bug#35708: [27.0.50]: thingatpt.el, thing-at-point-looking-at redundant Date: Wed, 15 May 2019 12:11:05 +0200 Message-ID: References: <8a73a0c9-5b38-4089-2780-28e62a1e162b@easy-emacs.de> <87ftpih6hs.fsf@gmail.com> <1bbebe25-9c4e-2563-701b-9bed85fcabb0@easy-emacs.de> <8536lixikt.fsf@gmail.com> <364b372e-7896-d92c-f848-0e7abc152a20@easy-emacs.de> <2d6578ec-8d7c-d3d0-7d1c-08eab22cae79@easy-emacs.de> <85tvdxw1dc.fsf@gmail.com> <6bf2ad95-5a41-cbf1-c5ea-07dc06d0f9a1@easy-emacs.de> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="------------96E31B7BEC272D5D29D7970D" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="17202"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux i686; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 Cc: 35708@debbugs.gnu.org To: npostavs@gmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed May 15 12:12:12 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQqtP-0004NB-A1 for geb-bug-gnu-emacs@m.gmane.org; Wed, 15 May 2019 12:12:11 +0200 Original-Received: from localhost ([127.0.0.1]:34599 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQqtO-0000CA-AQ for geb-bug-gnu-emacs@m.gmane.org; Wed, 15 May 2019 06:12:10 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:35810) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQqtH-0000C1-Pr for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 06:12:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hQqtG-00070V-AL for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 06:12:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37535) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hQqtG-00070E-6S for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 06:12:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hQqtF-000651-WF for bug-gnu-emacs@gnu.org; Wed, 15 May 2019 06:12:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Andreas =?UTF-8?Q?R=C3=B6hler?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 15 May 2019 10:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 35708 X-GNU-PR-Package: emacs Original-Received: via spool by 35708-submit@debbugs.gnu.org id=B35708.155791507623320 (code B ref 35708); Wed, 15 May 2019 10:12:01 +0000 Original-Received: (at 35708) by debbugs.gnu.org; 15 May 2019 10:11:16 +0000 Original-Received: from localhost ([127.0.0.1]:51079 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hQqsV-000643-KQ for submit@debbugs.gnu.org; Wed, 15 May 2019 06:11:15 -0400 Original-Received: from mout.kundenserver.de ([212.227.126.131]:42897) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hQqsT-00063q-1f for 35708@debbugs.gnu.org; Wed, 15 May 2019 06:11:14 -0400 Original-Received: from [192.168.178.31] ([88.130.49.114]) by mrelayeu.kundenserver.de (mreue009 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MI5YN-1hVVQj1GGV-00FAu7; Wed, 15 May 2019 12:11:06 +0200 In-Reply-To: <6bf2ad95-5a41-cbf1-c5ea-07dc06d0f9a1@easy-emacs.de> Content-Language: en-US X-Provags-ID: V03:K1:Zw+HlcF8W4KgLlFl5ihiiofNgAnINGlPwT06LOhFGZhnTUnuC++ +MpgoXD0r+jX5ULIERfvr1gWHdubwk1Xbbx7gxcxyS9M/zExtBrzne9CJ8hHHOrdpd1eVzw bL3B7Ba6vdwUiw+Y93sd+g/3NnZUTRT4ZuW9vRWvQAdFNMn8hKJ9lgo2ZQmv7Po/LsqQ9Dt ETnW+LvWHzEo9WZo/8fjA== X-UI-Out-Filterresults: notjunk:1;V03:K0:KGJgb6CvSlA=:nBQUuwX9XUcb7wZ2JhV3ZC VVqTf8+bqBcJShTTgPpgmEtUefXZgPOPFQ2Q59i09fcRc3fa8Yda1Enan6wS8d1skKtCHoHoi kW1guz/wjibIJHNUpYh/O3M0d01By0zfhQhSgzOINYnn0pAqOk1YfPytYIG0N5kTawDT7Q71u s9pvQplEaouNBrYyUXadOlqP5TBI1Fb4PxT4+WE6Jk+u7UHWlpomEtRM3WpQ0jfCzqwNp9Z1v jLA4dbUsX+zeakMD7LKLvaibrl+2dSE2qYU4nHeQt2WjRQUVSoVDGXku/bgPr4GRWJYKX4VN/ zpBQycSmj/2HBbJTaGovpdmV/fLUwZJn1N+QXxNerIAJf7GOzV65OjhHi4DbX4m+VFlQS/RZr 0CwXITEoKww30972pdwP8gBX8kO+wUcKrjT7YmxS1jMkTV32hTzokJTtw9ShWVV2ZroAiSWMg dt7yYD6Nu+GvZEcyG1JFvDtEhlwQYHRi9/7MRUvSGnJuOhoYQqh4efZRbEK92rSWyENdlnUHZ 58+qa4VEhiku/iokGW8VE3hBd3R6GPe+Dk2fPZmy88xVEfqjHtEFf3kwsGO0MhCZfswefTuvc J1TXNyK+7nUkMiibMP2PPO/WjQItgGxxsVGwDKy6G7kYRqyQ630P5hrKlF5m0vF48zllbtbHp ar1K1/yFl8eHwSHW+AzU+ULVacqFUK1i7yyahRk1fzswrquLjksZQ/I3ohnklOUjHB5Gx9mpa QGmCQ8OA1JxrJ1CFssO+8yDrvk3oernrmwPvnsJW9OnUCvyGx+cikD5oC9JrD4lfdLXumDdH X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:159333 Archived-At: This is a multi-part message in MIME format. --------------96E31B7BEC272D5D29D7970D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Am 15.05.19 um 08:59 schrieb Andreas Röhler: > > Am 14.05.19 um 16:34 schrieb npostavs@gmail.com: >> Andreas Röhler writes: >> >>>> Hmm, current thing-at-point-looking-at might be slow with large >>>> buffers. The slightly modified test should reveal it: >>>> >>>> (ert-deftest thing-at-point-looking-at-2 () >>>>    (with-temp-buffer >>>>      (insert "1abcd 222abcd") >>>>      (dotimes (_ 99999) (insert " asdf ")) >>>>      (goto-char (point-min)) >>>>        (search-forward "2ab") >>>>        (should (thing-at-point-looking-at "2abcd")) >> Yes, in this case, since the loop over looking-at only needs to iterate >> twice, so it will be faster.  But what about when there is no match? >> E.g., >> >> (with-temp-buffer >>    (dotimes (_ 99999) (insert " asdf ")) >>    (goto-char (point-max)) >>    (list :ar-regexp-atpt (benchmark-run (ar-regexp-atpt "foo")) >>          :thing-at-point-looking-at (benchmark-run >> (thing-at-point-looking-at "foo")))) >> >>> Another fix, as a bug showed up when testing (ar-regexp-atpt "[a-z]+"): >>> >>> (defun ar-regexp-atpt (regexp) >>>    "Return t if REGEXP matches at or before point, nil otherwise. >>> >>> Changes match-data" >>>    (save-excursion >>>      (if (looking-at regexp) >>>      (while >>>          (and (not (bobp)) >>>           (or (progn (backward-char) (looking-at regexp)) >>>               (forward-char 1)))) >>>        (while (not (or (bobp) (backward-char) (looking-at regexp)))) >>>        (ar-regexp-atpt regexp)) >> What's this recursive call for?  It triggers (error "Lisp nesting >> exceeds ‘max-lisp-eval-depth’") in the benchmark above. >> >>>      (looking-at regexp))) > > > The recursive call needed a guard:     (unless (bobp) > > It is called after function went backward while not looking-at matches, > > Now the result for the 99999 is > > (:ar-regexp-atpt (0.774574453 0 0.0) :thing-at-point-looking-at > (0.000798669 0 0.0)) > > > The fixed form: > > Make sure match pos includes cursor pos: (defun ar-regexp-atpt (regexp)   "Return t if REGEXP matches at or before point, nil otherwise. Changes match-data"   (save-excursion     (let ((orig (point)))       (if (looking-at regexp)       (while           (and (not (bobp))            (or (progn (backward-char) (looking-at regexp))                (forward-char 1))))     (while (not (or (bobp) (backward-char) (looking-at regexp))))     (unless (bobp) (ar-regexp-atpt regexp)))       (and        (looking-at regexp)        (<= (match-beginning 0) orig)        (>= (match-end 0) orig))))) --------------96E31B7BEC272D5D29D7970D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
Am 15.05.19 um 08:59 schrieb Andreas Röhler:

Am 14.05.19 um 16:34 schrieb npostavs@gmail.com:
Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

Hmm, current thing-at-point-looking-at might be slow with large
buffers. The slightly modified test should reveal it:

(ert-deftest thing-at-point-looking-at-2 ()
   (with-temp-buffer
     (insert "1abcd 222abcd")
     (dotimes (_ 99999) (insert " asdf "))
     (goto-char (point-min))
       (search-forward "2ab")
       (should (thing-at-point-looking-at "2abcd"))
Yes, in this case, since the loop over looking-at only needs to iterate
twice, so it will be faster.  But what about when there is no match?
E.g.,

(with-temp-buffer
   (dotimes (_ 99999) (insert " asdf "))
   (goto-char (point-max))
   (list :ar-regexp-atpt (benchmark-run (ar-regexp-atpt "foo"))
         :thing-at-point-looking-at (benchmark-run (thing-at-point-looking-at "foo"))))

Another fix, as a bug showed up when testing (ar-regexp-atpt "[a-z]+"):

(defun ar-regexp-atpt (regexp)
   "Return t if REGEXP matches at or before point, nil otherwise.

Changes match-data"
   (save-excursion
     (if (looking-at regexp)
     (while
         (and (not (bobp))
          (or (progn (backward-char) (looking-at regexp))
              (forward-char 1))))
       (while (not (or (bobp) (backward-char) (looking-at regexp))))
       (ar-regexp-atpt regexp))
What's this recursive call for?  It triggers (error "Lisp nesting
exceeds ‘max-lisp-eval-depth’") in the benchmark above.

     (looking-at regexp)))


The recursive call needed a guard:     (unless (bobp)

It is called after function went backward while not looking-at matches,

Now the result for the 99999 is

(:ar-regexp-atpt (0.774574453 0 0.0) :thing-at-point-looking-at (0.000798669 0 0.0))


The fixed form:


Make sure match pos includes cursor pos:


(defun ar-regexp-atpt (regexp)

  "Return t if REGEXP matches at or before point, nil otherwise.
Changes match-data"
  (save-excursion
    (let ((orig (point)))
      (if (looking-at regexp)
      (while
          (and (not (bobp))
           (or (progn (backward-char) (looking-at regexp))
               (forward-char 1))))
    (while (not (or (bobp) (backward-char) (looking-at regexp))))
    (unless (bobp) (ar-regexp-atpt regexp)))
      (and
       (looking-at regexp)
       (<= (match-beginning 0) orig)
       (>= (match-end 0) orig)))))


--------------96E31B7BEC272D5D29D7970D--