all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Dmitry Antipov <antipov@dev.rtsoft.ru>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 16830@debbugs.gnu.org
Subject: bug#16830: Re: bug#16830: [Bug] 24.3.50; massive slow down in forward-line
Date: Mon, 17 Mar 2014 19:05:27 +0400	[thread overview]
Message-ID: <53270F37.1010904@dev.rtsoft.ru> (raw)
In-Reply-To: <83txb5op81.fsf@gnu.org>

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

On 03/10/2014 10:58 PM, Eli Zaretskii wrote:

> It would be nice to be able to turn the cache on and off dynamically,
> depending on the actual line length of the buffer.  I tried to
> implement this, but my naive implementation didn't work well, because
> sampling of the lines tends to be extremely un-representative.  If
> someone can come up with a smarter implementation, please show it.

What if we just maintain the '\n' counter per each buffer text?
With that, finding an average line length is straightforward,
and the very basic implementation looks fairly simple; this
should be helpful in maintaining newline_cache as well.

Dmitry


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: nl.patch --]
[-- Type: text/x-patch; name="nl.patch", Size: 4917 bytes --]

=== modified file 'src/buffer.c'
--- src/buffer.c	2014-03-15 11:16:12 +0000
+++ src/buffer.c	2014-03-17 14:42:01 +0000
@@ -573,6 +573,7 @@
   BUF_END_UNCHANGED (b) = 0;
   BUF_BEG_UNCHANGED (b) = 0;
   *(BUF_GPT_ADDR (b)) = *(BUF_Z_ADDR (b)) = 0; /* Put an anchor '\0'.  */
+  BUF_NL (b) = 0;
   b->text->inhibit_shrinking = false;
   b->text->redisplay = false;
 

=== modified file 'src/buffer.h'
--- src/buffer.h	2014-01-01 07:43:34 +0000
+++ src/buffer.h	2014-03-17 14:42:01 +0000
@@ -91,6 +91,9 @@
 /* Modification count as of last visit or save.  */
 #define SAVE_MODIFF (current_buffer->text->save_modiff)
 
+/* How many '\n' in the buffer.  */
+#define NL (current_buffer->text->nl)
+
 /* BUFFER_CEILING_OF (resp. BUFFER_FLOOR_OF), when applied to n, return
    the max (resp. min) p such that
 
@@ -183,6 +186,9 @@
 /* FIXME: should we move this into ->text->auto_save_modiff?  */
 #define BUF_AUTOSAVE_MODIFF(buf) ((buf)->auto_save_modified)
 
+/* How many '\n' in the buffer.  */
+#define BUF_NL(buf) ((buf)->text->nl)
+
 /* Compaction count.  */
 #define BUF_COMPACT(buf) ((buf)->text->compact)
 
@@ -463,6 +469,9 @@
     /* Properties of this buffer's text.  */
     INTERVAL intervals;
 
+    /* How many '\n' in this buffer's text.  */
+    ptrdiff_t nl;
+
     /* The markers that refer to this buffer.
        This is actually a single marker ---
        successive elements in its marker `chain'

=== modified file 'src/insdel.c'
--- src/insdel.c	2014-01-01 17:44:48 +0000
+++ src/insdel.c	2014-03-17 14:55:57 +0000
@@ -560,7 +560,20 @@
       return to_addr - initial_to_addr;
     }
 }
-\f
+
+/* Count '\n' in [PTR..PTR + NBYTES).  */
+
+static ptrdiff_t
+count_newlines (const char *ptr, ptrdiff_t nbytes)
+{
+  const char *p = ptr;
+  ptrdiff_t count = 0, restbytes = nbytes;
+
+  while (restbytes > 0 && (p = memchr (p, '\n', restbytes)))
+    p++, count++, restbytes = nbytes - (p - ptr);
+  return count;
+}
+
 /* Insert a string of specified length before point.
    This function judges multibyteness based on
    enable_multibyte_characters in the current buffer;
@@ -814,6 +827,9 @@
   MODIFF++;
   CHARS_MODIFF = MODIFF;
 
+  /* Count newlines in new text.  */
+  NL += count_newlines (string, nbytes);
+
   memcpy (GPT_ADDR, string, nbytes);
 
   GAP_SIZE -= nbytes;
@@ -924,6 +940,9 @@
     make_gap (outgoing_nbytes - GAP_SIZE);
   UNGCPRO;
 
+  /* Count newlines in string text.  */
+  NL += count_newlines (SSDATA (string) + pos_byte, nbytes);
+
   /* Copy the string text into the buffer, perhaps converting
      between single-byte and multibyte.  */
   copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
@@ -1001,6 +1020,11 @@
   record_insert (GPT, nchars);
   MODIFF++;
 
+  /* Add newlines from new text.  */
+  NL += count_newlines (text_at_gap_tail
+			? (char *) GAP_END_ADDR - nbytes
+			: (char *) GPT_ADDR, nbytes);
+
   GAP_SIZE -= nbytes;
   if (! text_at_gap_tail)
     {
@@ -1138,6 +1162,9 @@
     emacs_abort ();
 #endif
 
+  /* Add newlines from gap.  */
+  NL += count_newlines ((char *) GPT_ADDR, outgoing_nbytes);
+
   record_insert (PT, nchars);
   MODIFF++;
   CHARS_MODIFF = MODIFF;
@@ -1340,6 +1367,9 @@
      combining.  */
   if (! EQ (BVAR (current_buffer, undo_list), Qt))
     deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
+  
+  /* Do not count newlines from deleted text any more.  */
+  NL -= count_newlines ((char *) BYTE_POS_ADDR (from_byte), nbytes_del);
 
   GAP_SIZE += nbytes_del;
   ZV -= nchars_del;
@@ -1360,6 +1390,9 @@
   if (GAP_SIZE < outgoing_insbytes)
     make_gap (outgoing_insbytes - GAP_SIZE);
 
+  /* Add newlines from string text.  */
+  NL += count_newlines (SSDATA (new), insbytes);
+
   /* Copy the string text into the buffer, perhaps converting
      between single-byte and multibyte.  */
   copy_text (SDATA (new), GPT_ADDR, insbytes,
@@ -1470,6 +1503,9 @@
   if (to < GPT)
     gap_left (to, to_byte, 0);
 
+  /* Do not count newlines from deleted text any more.  */
+  NL -= count_newlines ((char *) BYTE_POS_ADDR (from_byte), nbytes_del);
+
   GAP_SIZE += nbytes_del;
   ZV -= nchars_del;
   Z -= nchars_del;
@@ -1489,6 +1525,9 @@
   if (GAP_SIZE < insbytes)
     make_gap (insbytes - GAP_SIZE);
 
+  /* Add newlines from replacement text.  */
+  NL += count_newlines (ins, insbytes);
+
   /* Copy the replacement text into the buffer.  */
   memcpy (GPT_ADDR, ins, insbytes);
 
@@ -1737,6 +1776,9 @@
      adjusting the markers that bound the overlays.  */
   adjust_overlays_for_delete (from, nchars_del);
 
+  /* Do not count newlines from deleted text any more.  */
+  NL -= count_newlines ((char *) BYTE_POS_ADDR (from_byte), nbytes_del);
+
   GAP_SIZE += nbytes_del;
   ZV_BYTE -= nbytes_del;
   Z_BYTE -= nbytes_del;


  parent reply	other threads:[~2014-03-17 15:05 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-21 12:16 bug#16830: [Bug] 24.3.50; massive slow down in forward-line Stefan-W. Hahn
2014-02-21 12:32 ` Eli Zaretskii
2014-02-21 15:51   ` Stefan-W. Hahn
2014-02-21 17:50     ` Stefan Monnier
2014-02-22  8:38       ` Stefan-W. Hahn
     [not found]       ` <20140222083926.GC27381@pille.home>
2014-02-22  9:18         ` Eli Zaretskii
2014-02-22 11:08           ` Stefan-W. Hahn
2014-02-22 11:29             ` Eli Zaretskii
2014-02-22 11:36               ` Eli Zaretskii
2014-02-22 12:33                 ` Stefan-W. Hahn
2014-02-22 12:55                   ` Juanma Barranquero
2014-02-22 15:06                     ` Stefan-W. Hahn
2014-02-22 13:05                   ` Eli Zaretskii
2014-02-21  7:43                     ` Stefan-W. Hahn
2014-02-23 18:04                       ` Eli Zaretskii
2014-02-22 12:27               ` Stefan-W. Hahn
2014-03-10 18:58                 ` Eli Zaretskii
2014-03-11  8:08                   ` martin rudalics
2014-03-11 17:03                     ` Eli Zaretskii
2014-03-12 14:12                     ` Stefan Monnier
2014-03-16 16:32                       ` Eli Zaretskii
2014-03-17 15:05                   ` Dmitry Antipov [this message]
2014-03-17 16:39                     ` Eli Zaretskii
2014-06-22 16:50                       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53270F37.1010904@dev.rtsoft.ru \
    --to=antipov@dev.rtsoft.ru \
    --cc=16830@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.