unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#13182: [PATCH] long delays in python-mode buffer parsing
@ 2012-12-14 11:40 Daniel Colascione
  2012-12-15  6:44 ` Daniel Colascione
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Daniel Colascione @ 2012-12-14 11:40 UTC (permalink / raw)
  To: 13182

[-- Attachment #1: Type: text/plain, Size: 2005 bytes --]

Some python-mode operations slow down noticeably when the region being edited
contains an unclosed bracket or string: these constructs lead to python-mode
scanning the entire remainder of the buffer, and this scan appears to take time
O(nr_lines^2). When which-func mode is enabled, this slowness renders Emacs
unusable, since we'll call python-info-current-defun frequently in order to
update the modeline, and this function will take several seconds to complete.

The following patch appears to remedy the problem without breaking anythig.

=== modified file 'lisp/progmodes/python.el'
--- lisp/progmodes/python.el	2012-11-27 03:10:32 +0000
+++ lisp/progmodes/python.el	2012-12-14 11:28:58 +0000
@@ -1184,13 +1184,21 @@
 (defun python-nav-end-of-statement ()
   "Move to end of current statement."
   (interactive "^")
-  (while (and (goto-char (line-end-position))
-              (not (eobp))
-              (when (or
-                     (python-info-line-ends-backslash-p)
-                     (python-syntax-context 'string)
-                     (python-syntax-context 'paren))
-                (forward-line 1))))
+
+  (let (string-start bs-pos)
+    (while (and (goto-char (line-end-position))
+                (not (eobp))
+                (cond ((setq string-start (python-syntax-context 'string))
+                       (goto-char string-start)
+                       (forward-sexp))
+                      ((python-syntax-context 'paren)
+                       ;; The statement won't end before we've escaped
+                       ;; at least one level of parenthesis.
+                       (condition-case err
+                           (goto-char (scan-lists (point) 1 -1))
+                         (scan-error (goto-char (nth 3 err)))))
+                      ((setq bs-pos (python-info-line-ends-backslash-p))
+                       (goto-char bs-pos))))))
   (point-marker))

 (defun python-nav-backward-statement (&optional arg)



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 258 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#13182: [PATCH] long delays in python-mode buffer parsing
  2012-12-14 11:40 bug#13182: [PATCH] long delays in python-mode buffer parsing Daniel Colascione
@ 2012-12-15  6:44 ` Daniel Colascione
  2012-12-15 14:50   ` Stefan Monnier
  2012-12-28 15:29 ` Fabián Ezequiel Gallina
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Daniel Colascione @ 2012-12-15  6:44 UTC (permalink / raw)
  To: 13182

[-- Attachment #1: Type: text/plain, Size: 2779 bytes --]

On 12/14/2012 3:40 AM, Daniel Colascione wrote:
> Some python-mode operations slow down noticeably when the region being edited
> contains an unclosed bracket or string: these constructs lead to python-mode
> scanning the entire remainder of the buffer, and this scan appears to take time
> O(nr_lines^2). When which-func mode is enabled, this slowness renders Emacs
> unusable, since we'll call python-info-current-defun frequently in order to
> update the modeline, and this function will take several seconds to complete.
> 
> The following patch appears to remedy the problem without breaking anythig.

Here's a bugfixed patch. Review would be appreciated.

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-12-10 18:33:59 +0000
+++ lisp/ChangeLog	2012-12-15 06:38:59 +0000
@@ -1,3 +1,12 @@
+2012-12-15  Daniel Colascione  <dancol@dancol.org>
+
+	* progmodes/python.el (python-nav-end-of-statement): Don't loop forever.
+
+2012-12-14  Daniel Colascione  <dancol@dancol.org>
+
+	* progmodes/python.el (python-nav-end-of-statement): Rewrite in
+	order to improve efficiency.
+
 2012-12-10  Jambunathan K  <kjambunathan@gmail.com>

 	* hi-lock.el: Refine the choice of default face.

=== modified file 'lisp/progmodes/python.el'
--- lisp/progmodes/python.el	2012-11-27 03:10:32 +0000
+++ lisp/progmodes/python.el	2012-12-15 06:28:38 +0000
@@ -1184,13 +1184,23 @@
 (defun python-nav-end-of-statement ()
   "Move to end of current statement."
   (interactive "^")
-  (while (and (goto-char (line-end-position))
-              (not (eobp))
-              (when (or
-                     (python-info-line-ends-backslash-p)
-                     (python-syntax-context 'string)
-                     (python-syntax-context 'paren))
-                (forward-line 1))))
+
+  (let (string-start bs-pos)
+    (while (and (goto-char (line-end-position))
+                (not (eobp))
+                (cond ((setq string-start (python-syntax-context 'string))
+                       (goto-char string-start)
+                       (let (forward-sexp-function)
+                         (forward-sexp)))
+                      ((python-syntax-context 'paren)
+                       ;; The statement won't end before we've escaped
+                       ;; at least one level of parenthesis.
+                       (condition-case err
+                           (goto-char (scan-lists (point) 1 -1))
+                         (scan-error (goto-char (nth 3 err)))))
+                      ((setq bs-pos (python-info-line-ends-backslash-p))
+                       (goto-char bs-pos)
+                       (forward-line 1))))))
   (point-marker))

 (defun python-nav-backward-statement (&optional arg)





[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 258 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#13182: [PATCH] long delays in python-mode buffer parsing
  2012-12-15  6:44 ` Daniel Colascione
@ 2012-12-15 14:50   ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2012-12-15 14:50 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: 13182

> Here's a bugfixed patch. Review would be appreciated.

It looks fine in general.  I prefer using forward-sexp over scan-lists,
but that's just a personal preference.  Also, a single changelog entry
will do, if you commit it as a single commit.


        Stefan





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#13182: [PATCH] long delays in python-mode buffer parsing
  2012-12-14 11:40 bug#13182: [PATCH] long delays in python-mode buffer parsing Daniel Colascione
  2012-12-15  6:44 ` Daniel Colascione
@ 2012-12-28 15:29 ` Fabián Ezequiel Gallina
  2012-12-31 21:02 ` Fabián Ezequiel Gallina
  2012-12-31 21:05 ` Fabián Ezequiel Gallina
  3 siblings, 0 replies; 6+ messages in thread
From: Fabián Ezequiel Gallina @ 2012-12-28 15:29 UTC (permalink / raw)
  To: 13182

The patch looks good and given this is a speed regression this should be 
committed to the emacs-24 branch, please do it and then we can close 
this bug.


Thanks,
Fabián.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#13182: [PATCH] long delays in python-mode buffer parsing
  2012-12-14 11:40 bug#13182: [PATCH] long delays in python-mode buffer parsing Daniel Colascione
  2012-12-15  6:44 ` Daniel Colascione
  2012-12-28 15:29 ` Fabián Ezequiel Gallina
@ 2012-12-31 21:02 ` Fabián Ezequiel Gallina
  2012-12-31 21:05 ` Fabián Ezequiel Gallina
  3 siblings, 0 replies; 6+ messages in thread
From: Fabián Ezequiel Gallina @ 2012-12-31 21:02 UTC (permalink / raw)
  To: 13182

I just installed a modified version of your patch in revno 111108.

This modified version would jump correctly to the end of defun in the 
following context:

     doing_something("""
     This is preformatted text and should not be indented to the level 
of the parentheses.
     """
     )






^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#13182: [PATCH] long delays in python-mode buffer parsing
  2012-12-14 11:40 bug#13182: [PATCH] long delays in python-mode buffer parsing Daniel Colascione
                   ` (2 preceding siblings ...)
  2012-12-31 21:02 ` Fabián Ezequiel Gallina
@ 2012-12-31 21:05 ` Fabián Ezequiel Gallina
  3 siblings, 0 replies; 6+ messages in thread
From: Fabián Ezequiel Gallina @ 2012-12-31 21:05 UTC (permalink / raw)
  To: 13182

s/defun/statement/





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-12-31 21:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-14 11:40 bug#13182: [PATCH] long delays in python-mode buffer parsing Daniel Colascione
2012-12-15  6:44 ` Daniel Colascione
2012-12-15 14:50   ` Stefan Monnier
2012-12-28 15:29 ` Fabián Ezequiel Gallina
2012-12-31 21:02 ` Fabián Ezequiel Gallina
2012-12-31 21:05 ` Fabián Ezequiel Gallina

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).