]> git.eshelyaron.com Git - emacs.git/commitdiff
Speed up insert-file-contents reads
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 13 Jul 2025 20:33:27 +0000 (13:33 -0700)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 08:49:08 +0000 (10:49 +0200)
This change makes insert-file-contents-literally 30% faster on my
platform, when inserting xdisp.c.
* src/fileio.c (READ_BUF_SIZE): Remove, replacing with ...
(IO_BUFSIZE): ... this new constant from Coreutils.
All uses of READ_BUF_SIZE changed to either MAX_ALLOCA or
IO_BUFSIZE.

(cherry picked from commit 5bce6753e24d04ea3e4715fab53cc65c1b4ee8d7)

src/fileio.c

index 075b9c590c8fbb03fa6bc6ec2faf1f9c384f731e..8e1ac42723d2977cefab33bf77f8051af8b63c9b 100644 (file)
@@ -3836,8 +3836,6 @@ For existing files, this compares their last-modified times.  */)
          ? Qt : Qnil);
 }
 \f
-enum { READ_BUF_SIZE = MAX_ALLOCA };
-
 /* This function is called after Lisp functions to decide a coding
    system are called, or when they cause an error.  Before they are
    called, the current buffer is set unibyte and it contains only a
@@ -4028,6 +4026,10 @@ maybe_move_gap (struct buffer *b)
     }
 }
 
+/* A good blocksize to minimize system call overhead across most systems.
+   Taken from coreutils/src/ioblksize.h as of July 2025.  */
+enum { IO_BUFSIZE = 256 * 1024 };
+
 /* FIXME: insert-file-contents should be split with the top-level moved to
    Elisp and only the core kept in C.  */
 
@@ -4082,7 +4084,7 @@ by calling `format-decode', which see.  */)
   off_t total = 0;
   bool regular;
   int save_errno = 0;
-  char read_buf[READ_BUF_SIZE];
+  char read_buf[MAX_ALLOCA];
   struct coding_system coding;
   bool replace_handled = false;
   bool set_coding_system = false;
@@ -4861,7 +4863,7 @@ by calling `format-decode', which see.  */)
        else
          {
            buf = (char *) BEG_ADDR + PT_BYTE - BEG_BYTE + inserted;
-           bufsize = min (min (gap_size, total - inserted), READ_BUF_SIZE);
+           bufsize = min (min (gap_size, total - inserted), IO_BUFSIZE);
          }
 
        if (!seekable && end_offset == TYPE_MAXIMUM (off_t))