From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Brandon Craig Rhodes Newsgroups: gmane.emacs.bugs Subject: bug#7343: Making flyspell incredibly fast when checking whole files Date: Sat, 06 Nov 2010 10:03:52 -0400 Message-ID: <874obu5z1z.fsf@asaph.rhodesmill.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1289058244 18011 80.91.229.12 (6 Nov 2010 15:44:04 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 6 Nov 2010 15:44:04 +0000 (UTC) To: 7343@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Nov 06 16:43:50 2010 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PEkvl-0003Co-Th for geb-bug-gnu-emacs@m.gmane.org; Sat, 06 Nov 2010 16:43:50 +0100 Original-Received: from localhost ([127.0.0.1]:50305 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PEkvl-0002hq-3E for geb-bug-gnu-emacs@m.gmane.org; Sat, 06 Nov 2010 11:43:49 -0400 Original-Received: from [140.186.70.92] (port=42728 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PEkve-0002gi-GV for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 11:43:43 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PEkvc-0003h5-WE for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 11:43:42 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:52562) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PEkvc-0003h0-TW for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 11:43:40 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1PEkWo-0000PQ-Bt; Sat, 06 Nov 2010 11:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Brandon Craig Rhodes Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 06 Nov 2010 15:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 7343 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.12890566271556 (code B ref -1); Sat, 06 Nov 2010 15:18:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 6 Nov 2010 15:17:07 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1PEkVu-0000P2-02 for submit@debbugs.gnu.org; Sat, 06 Nov 2010 11:17:06 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1PEjM0-0008Ke-7p for submit@debbugs.gnu.org; Sat, 06 Nov 2010 10:02:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PEjQO-000585-8U for submit@debbugs.gnu.org; Sat, 06 Nov 2010 10:07:21 -0400 Original-Received: from lists.gnu.org ([199.232.76.165]:46088) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PEjQ1-00054f-3Y for submit@debbugs.gnu.org; Sat, 06 Nov 2010 10:07:20 -0400 Original-Received: from [140.186.70.92] (port=49910 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PEjNj-00020z-Ox for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 10:05:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PEjN4-0004fs-BC for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 10:03:56 -0400 Original-Received: from asaph.rhodesmill.org ([74.207.234.78]:35858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PEjN4-0004fX-7i for bug-gnu-emacs@gnu.org; Sat, 06 Nov 2010 10:03:54 -0400 Original-Received: by asaph.rhodesmill.org (Postfix, from userid 1000) id 957BE2E103; Sat, 6 Nov 2010 10:03:52 -0400 (EDT) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 1) X-detected-operating-system: by eggs.gnu.org: Error: This connection is not (no longer?) in the cache. X-Mailman-Approved-At: Sat, 06 Nov 2010 11:17:04 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Sat, 06 Nov 2010 11:18:02 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:41409 Archived-At: --=-=-= Spell-checking programs like "aspell" and "hunspell" are blazingly fast when simply asked to check words with their "-l" option, but become much slower (the difference is often one or more orders of magnitude, depending on the dictionary size) when asked about words interactively because in that case they generate helpful near-misses. (Actually, "aspell" can turn this off even in interactive mode, which might become the basis of a further patch; but right now I will confine myself to submitting this one, since it has gotten my Emacs running fast enough again that I am happy.) Anyway, flyspell does try to take advantage of the above behavior by checking whether a region is larger than flyspell-large-region characters, and if so then it runs the spell checker as a separate process with "-l". But then it does something that, in many cases, is rather ruinous: it takes every misspelling so identified, and passes it *back* through the normal interactive spell-checking logic! This is because all of the real logic of what to do with a misspelling - how to highlight it, how to search for nearby instances of the same word, how to cache spellings, and so forth - is bound up in flyspell-word, so the flyspell-external-point-words function, which processes the actual misspellings discovered by flyspell-large-region, really has no other choice but to call flyspell-word for each misspelling. So to let flyspell-large-region enjoy the speed that it really should, we need to tell it never to re-check its words against the live *spell process attached to Emacs, because that is (a) redundant and (b) very expensive since, this second time, the spell checker will pause to generate near-misses. A patch is attached that fixes this problem, and - here on my laptop, at least - makes flyspell blazing fast at even large files. The mechanism is simple: I have added a second optional argument to flyspell-word, named "known-misspelling", that tells flyspell-word that the word has already been checked and is a misspelling and does not need to be checked again. Then, down in the function, I simply placed the entire interactive session with ispell/aspell/hunspell inside of an "if". I apologize in advance that this diff is constructed against the Ubuntu version of flyspell, which has who-knows-how-many differences with the official Emacs one. If the patch is too difficult to apply, let me know, and I will find the time to check out Emacs from trunk myself and reapply the patch there. Thanks! --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=flyspell-large-fast.patch diff -r 0de00de3360c -r 06af33083844 site-lisp/flyspell.el --- a/site-lisp/flyspell.el Sat Nov 06 08:10:07 2010 -0400 +++ b/site-lisp/flyspell.el Sat Nov 06 08:23:58 2010 -0400 @@ -1009,7 +1009,7 @@ ;;*---------------------------------------------------------------------*/ ;;* flyspell-word ... */ ;;*---------------------------------------------------------------------*/ -(defun flyspell-word (&optional following) +(defun flyspell-word (&optional following known-misspelling) "Spell check a word." (interactive (list ispell-following-word)) (ispell-set-spellchecker-params) ; Initialize variables and dicts alists @@ -1071,29 +1071,35 @@ (setq flyspell-word-cache-end end) (setq flyspell-word-cache-word word) ;; now check spelling of word. - (ispell-send-string "%\n") - ;; put in verbose mode - (ispell-send-string (concat "^" word "\n")) - ;; we mark the ispell process so it can be killed - ;; when emacs is exited without query - (set-process-query-on-exit-flag ispell-process nil) - ;; Wait until ispell has processed word. Since this code is often - ;; executed from post-command-hook but the ispell process may not - ;; be responsive, it's important to make sure we re-enable C-g. - (with-local-quit - (while (progn - (accept-process-output ispell-process) - (not (string= "" (car ispell-filter)))))) - ;; (ispell-send-string "!\n") - ;; back to terse mode. - ;; Remove leading empty element - (setq ispell-filter (cdr ispell-filter)) - ;; ispell process should return something after word is sent. - ;; Tag word as valid (i.e., skip) otherwise - (or ispell-filter - (setq ispell-filter '(*))) - (if (consp ispell-filter) - (setq poss (ispell-parse-output (car ispell-filter)))) + (if (not known-misspelling) + (progn + (ispell-send-string "%\n") + ;; put in verbose mode + (ispell-send-string (concat "^" word "\n")) + ;; we mark the ispell process so it can be killed + ;; when emacs is exited without query + (set-process-query-on-exit-flag ispell-process nil) + ;; Wait until ispell has processed word. Since this + ;; code is often executed from post-command-hook but + ;; the ispell process may not be responsive, it's + ;; important to make sure we re-enable C-g. + (with-local-quit + (while (progn + (accept-process-output ispell-process) + (not (string= "" (car ispell-filter)))))) + ;; (ispell-send-string "!\n") + ;; back to terse mode. + ;; Remove leading empty element + (setq ispell-filter (cdr ispell-filter)) + ;; ispell process should return something after word is sent. + ;; Tag word as valid (i.e., skip) otherwise + (or ispell-filter + (setq ispell-filter '(*))) + (if (consp ispell-filter) + (setq poss (ispell-parse-output (car ispell-filter))))) + ;; Else, this was a known misspelling to begin with, and + ;; we should forge an ispell return value. + (setq poss (list word 0 '() '()))) (let ((res (cond ((eq poss t) ;; correct (setq flyspell-word-cache-result t) @@ -1424,7 +1430,7 @@ t nil)))) (setq keep nil) - (flyspell-word) + (flyspell-word nil t) ;; Search for next misspelled word will begin from ;; end of last validated match. (setq buffer-scan-pos (point)))) --=-=-= -- Brandon Craig Rhodes brandon@rhodesmill.org http://rhodesmill.org/brandon --=-=-=--