]> git.eshelyaron.com Git - emacs.git/commitdiff
insert-file-contents read size increase
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 20 Jul 2025 20:40:20 +0000 (13:40 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 25 Jul 2025 08:11:44 +0000 (10:11 +0200)
This increases the max read size from 0.25 to 1 MiB, which is a
bit better for Emacs in my measurements.  The old value was taken
from coreutils, but was for a different purpose.
* src/fileio.c (INSERT_READ_SIZE_MAX): New constant, replacing
IO_BUFSIZE.  All uses changed.
(Finsert_file_contents): Do not use a read buffer that is larger
than INSERT_READ_SIZE_MAX.

(cherry picked from commit a8e46f11a89145390ee681e2dd14fbe6ab79b7f0)

src/fileio.c

index 76e5e56190e8105e82714fdcf5a52ae5d89cb9cc..93ca8508ced91700c089a7e649450b936960d9f0 100644 (file)
@@ -4048,10 +4048,6 @@ xlseek (emacs_fd fd, off_t pos, Lisp_Object filename)
   return pos;
 }
 
-/* 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.  */
 
@@ -4096,6 +4092,14 @@ by calling `format-decode', which see.  */)
   (Lisp_Object filename, Lisp_Object visit, Lisp_Object beg, Lisp_Object end,
    Lisp_Object replace)
 {
+  /* A good read blocksize for insert-file-contents.
+     It is for reading a big chunk of a file into memory,
+     as opposed to coreutils IO_BUFSIZE which is for 'cat'-like stream reads.
+     If too small, insert-file-contents has more syscall overhead.
+     If too large, insert-file-contents might take too long respond to a quit.
+     1 MiB should be reasonable even for older, slower devices circa 2025.  */
+  enum { INSERT_READ_SIZE_MAX = min (1024 * 1024, MAX_RW_COUNT) };
+
   struct timespec mtime;
   emacs_fd fd;
   ptrdiff_t inserted = 0;
@@ -4106,7 +4110,7 @@ by calling `format-decode', which see.  */)
   off_t total = 0;
   bool regular;
   int save_errno = 0;
-  char read_buf[MAX_ALLOCA];
+  char read_buf[min (+MAX_ALLOCA, +INSERT_READ_SIZE_MAX)];
   struct coding_system coding;
   bool replace_handled = false;
   bool set_coding_system = false;
@@ -4886,7 +4890,7 @@ by calling `format-decode', which see.  */)
        else
          {
            buf = (char *) BEG_ADDR + PT_BYTE - BEG_BYTE + inserted;
-           bufsize = min (gap_size, IO_BUFSIZE);
+           bufsize = min (gap_size, INSERT_READ_SIZE_MAX);
          }
        bufsize = min (bufsize, total - inserted);