From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#33708: 26.1.90; nhexl-mode performance Date: Fri, 14 Dec 2018 13:40:36 -0500 Message-ID: References: <8d6352ac-4bca-fb76-8c2f-dd597e481e8e@bgc-jena.mpg.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1544812750 17060 195.159.176.226 (14 Dec 2018 18:39:10 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 14 Dec 2018 18:39:10 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 33708@debbugs.gnu.org To: Guido Kraemer Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Dec 14 19:39:06 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gXsMb-0004Jb-69 for geb-bug-gnu-emacs@m.gmane.org; Fri, 14 Dec 2018 19:39:05 +0100 Original-Received: from localhost ([::1]:34979 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXsOh-0000MX-FU for geb-bug-gnu-emacs@m.gmane.org; Fri, 14 Dec 2018 13:41:15 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gXsOX-0000M3-K6 for bug-gnu-emacs@gnu.org; Fri, 14 Dec 2018 13:41:06 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gXsOU-0008GW-9y for bug-gnu-emacs@gnu.org; Fri, 14 Dec 2018 13:41:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:44283) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gXsOT-0008GM-US for bug-gnu-emacs@gnu.org; Fri, 14 Dec 2018 13:41:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gXsOT-0006Tw-RG for bug-gnu-emacs@gnu.org; Fri, 14 Dec 2018 13:41:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 14 Dec 2018 18:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33708 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 33708-submit@debbugs.gnu.org id=B33708.154481284624887 (code B ref 33708); Fri, 14 Dec 2018 18:41:01 +0000 Original-Received: (at 33708) by debbugs.gnu.org; 14 Dec 2018 18:40:46 +0000 Original-Received: from localhost ([127.0.0.1]:48541 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gXsOE-0006TK-Da for submit@debbugs.gnu.org; Fri, 14 Dec 2018 13:40:46 -0500 Original-Received: from alt42.smtp-out.videotron.ca ([23.233.128.29]:4865) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gXsOC-0006T5-NA for 33708@debbugs.gnu.org; Fri, 14 Dec 2018 13:40:45 -0500 Original-Received: from fmsmemgm.homelinux.net ([23.233.195.134]) by Videotron with SMTP id XsO4g34hOM6QEXsO6g4cKE; Fri, 14 Dec 2018 13:40:39 -0500 X-Authority-Analysis: v=2.3 cv=bq5i+nSi c=1 sm=1 tr=0 a=xXJ578j8WyTliCxld3/pTA==:117 a=xXJ578j8WyTliCxld3/pTA==:17 a=2ur7OfE09M0A:10 a=g5WmnKvGXLTxagMPJxAA:9 a=9Hpp9JvEUdwtoNKF:21 a=JfC2CH3QbcSsgSG5:21 Original-Received: by fmsmemgm.homelinux.net (Postfix, from userid 20848) id D81FFAE97A; Fri, 14 Dec 2018 13:40:36 -0500 (EST) In-Reply-To: <8d6352ac-4bca-fb76-8c2f-dd597e481e8e@bgc-jena.mpg.de> (Guido Kraemer's message of "Tue, 11 Dec 2018 18:06:40 +0100") X-CMAE-Envelope: MS4wfMmtEVwRql6nSqktzxhUlPeDAF9sfUHH4hJmVBAi2WIvcA408fRsbqp1O4Vy/lVaH2EKcR453KD9jYeTe74xFeUpXbgMy9Gr5aaJlWbgtuPYffGN5+nG chUPKhyXEyvOy66urqTs9GSbNG1+T387My0ldzSee7Fwv22ELeOgVCFCk7tTe9GiQr1ezwoxXvwIazIQD8fMzJtUp7X3MV27ZCvR/X1tvpUn2z5Q6cQNjMkF QE8WtWN3M92JsPubXlQAlOyGtwtFXE8g6NCPaPdKFds= 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: 208.118.235.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:153457 Archived-At: > Occurred in the files of the Bitcoin blockchain. The beginning of every > block is marked with the byte sequence `f9beb4d9`. Searching for this > byte sequence in nhexl-mode is really slow. Duh, indeed it's painful, and it's a plain performance bug in nhexl-mode. I just installed the patch below which seems to fix it (along with another less severe bug). The corresponding 1.2 package should appear soon on GNU ELPA. Thanks for the test case (tho it was painful to get due to having to go through my browser and coerce it to run non-free Javascript code). Stefan diff --git a/packages/nhexl-mode/nhexl-mode.el b/packages/nhexl-mode/nhexl-mode.el index 89d91182f..a52a90081 100644 --- a/packages/nhexl-mode/nhexl-mode.el +++ b/packages/nhexl-mode/nhexl-mode.el @@ -807,22 +807,31 @@ Return the corresponding nibble, if applicable." (push (string-to-number (substring string i (+ i 2)) 16) chars) (setq i (+ i 2))) - (let* ((base (regexp-quote (apply #'string (nreverse chars)))) - (newstr - (if (>= i (length string)) - base - (cl-assert (= (1+ i) (length string))) - (let ((nibble (string-to-number (substring string i) 16))) - ;; FIXME: if one of the two bounds is a special char - ;; like `]` or `^' we can get into trouble! - (format "%s[%c-%c]" base - (* 16 nibble) - (+ 15 (* 16 nibble))))))) + (let* ((base (regexp-quote (apply #'unibyte-string (nreverse chars)))) + (re + (concat (if (>= i (length string)) + base + (cl-assert (= (1+ i) (length string))) + (let ((nibble (string-to-number (substring string i) 16))) + ;; FIXME: if one of the two bounds is a special char + ;; like `]` or `^' we can get into trouble! + (concat base + (unibyte-string ?\[ (* 16 nibble) ?- + (+ 15 (* 16 nibble)) ?\])))) + ;; We also search for the literal hex string here, so the + ;; search stops as soon as one is found, otherwise we too + ;; easily fall into the trap of bug#33708 where at every + ;; cycle we first search unsuccessfully through the whole + ;; buffer with one kind of search before trying the + ;; other search. + ;; Don't bother regexp-quoting the string since we know + ;; it's only made of hex chars! + "\\|" string))) (let ((case-fold-search nil)) (funcall (if isearch-forward #'re-search-forward #'re-search-backward) - newstr bound noerror))))) + re bound noerror))))) (defun nhexl--isearch-search-fun (orig-fun) (let ((def-fun (funcall orig-fun))) @@ -830,9 +839,18 @@ Return the corresponding nibble, if applicable." (unless bound (setq bound (if isearch-forward (point-max) (point-min)))) (let ((startpos (point)) - (def (funcall def-fun string bound noerror))) - ;; Don't search further than what `def-fun' found. - (if def (setq bound (match-beginning 0))) + def) + ;; Hex address search. + (when (and nhexl-isearch-hex-addresses + (> (length string) 1) + (string-match-p "\\`[[:xdigit:]]+:?\\'" string)) + ;; Could be a hexadecimal address. + (goto-char startpos) + (let ((newdef (nhexl--isearch-match-hex-address string bound noerror))) + (when newdef + (setq def newdef) + (setq bound (match-beginning 0))))) + ;; Hex bytes search (when (and nhexl-isearch-hex-bytes (> (length string) 1) (string-match-p "\\`[[:xdigit:]]+\\'" string)) @@ -842,12 +860,10 @@ Return the corresponding nibble, if applicable." (when newdef (setq def newdef) (setq bound (match-beginning 0))))) - (when (and nhexl-isearch-hex-addresses - (> (length string) 1) - (string-match-p "\\`[[:xdigit:]]+:?\\'" string)) - ;; Could be a hexadecimal address. + ;; Normal search. + (progn (goto-char startpos) - (let ((newdef (nhexl--isearch-match-hex-address string bound noerror))) + (let ((newdef (funcall def-fun string bound noerror))) (when newdef (setq def newdef) (setq bound (match-beginning 0))))) @@ -909,17 +925,19 @@ Return the corresponding nibble, if applicable." #'nhexl--isearch-highlight-cleanup) (defun nhexl--isearch-highlight-cleanup (&rest _) (when (and nhexl-mode nhexl-isearch-hex-highlight) - (dolist (ol isearch-lazy-highlight-overlays) - (when (and (overlayp ol) (eq (overlay-buffer ol) (current-buffer))) - (put-text-property (overlay-start ol) (overlay-end ol) - 'fontified nil))))) + (with-silent-modifications + (dolist (ol isearch-lazy-highlight-overlays) + (when (and (overlayp ol) (eq (overlay-buffer ol) (current-buffer))) + (put-text-property (overlay-start ol) (overlay-end ol) + 'fontified nil)))))) (advice-add 'isearch-lazy-highlight-match :after #'nhexl--isearch-highlight-match) (defun nhexl--isearch-highlight-match (&optional mb me) (when (and nhexl-mode nhexl-isearch-hex-highlight (integerp mb) (integerp me)) - (put-text-property mb me 'fontified nil))) + (with-silent-modifications + (put-text-property mb me 'fontified nil)))) (defun nhexl--line-width-watcher (_sym _newval op where) (when (eq op 'set)