all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using pread
@ 2013-02-01  8:05 Dmitry Antipov
  2013-02-01  8:18 ` Paul Eggert
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2013-02-01  8:05 UTC (permalink / raw)
  To: Emacs development discussions; +Cc: Paul Eggert

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

Can we assume that all supported systems has pread(2)? If yes, we can optimize
read + lseek in some cases (and even if not, it's easy to emulate it in emacs_pread).

Dmitry

[-- Attachment #2: pread.patch --]
[-- Type: text/plain, Size: 2451 bytes --]

=== modified file 'src/fileio.c'
--- src/fileio.c	2013-02-01 06:30:51 +0000
+++ src/fileio.c	2013-02-01 07:40:44 +0000
@@ -3703,17 +3703,14 @@
 	      int nread;
 
 	      if (st.st_size <= (1024 * 4))
-		nread = emacs_read (fd, read_buf, 1024 * 4);
+		nread = emacs_pread (fd, read_buf, 1024 * 4, 0);
 	      else
 		{
-		  nread = emacs_read (fd, read_buf, 1024);
+		  nread = emacs_pread (fd, read_buf, 1024, 0);
 		  if (nread == 1024)
 		    {
-		      int ntail;
-		      if (lseek (fd, - (1024 * 3), SEEK_END) < 0)
-			report_file_error ("Setting file position",
-					   Fcons (orig_filename, Qnil));
-		      ntail = emacs_read (fd, read_buf + nread, 1024 * 3);
+		      int ntail = emacs_pread (fd, read_buf + nread, 1024 * 3,
+					       st.st_size - 1024 * 3);
 		      nread = ntail < 0 ? ntail : nread + ntail;
 		    }
 		}
@@ -3753,11 +3750,6 @@
 		  /* Discard the unwind protect for recovering the
                      current buffer.  */
 		  specpdl_ptr--;
-
-		  /* Rewind the file for the actual read done later.  */
-		  if (lseek (fd, 0, SEEK_SET) < 0)
-		    report_file_error ("Setting file position",
-				       Fcons (orig_filename, Qnil));
 		}
 	    }
 

=== modified file 'src/lisp.h'
--- src/lisp.h	2013-02-01 06:30:51 +0000
+++ src/lisp.h	2013-02-01 07:33:29 +0000
@@ -3566,6 +3566,7 @@
 extern int emacs_open (const char *, int, int);
 extern int emacs_close (int);
 extern ptrdiff_t emacs_read (int, char *, ptrdiff_t);
+extern ptrdiff_t emacs_pread (int, char *, ptrdiff_t, ptrdiff_t);
 extern ptrdiff_t emacs_write (int, const char *, ptrdiff_t);
 
 extern void unlock_all_files (void);

=== modified file 'src/sysdep.c'
--- src/sysdep.c	2013-02-01 06:30:51 +0000
+++ src/sysdep.c	2013-02-01 07:35:36 +0000
@@ -2209,6 +2209,21 @@
   return (rtnval);
 }
 
+/* Read from FILEDESC to a buffer BUF with size NBYTES at offset OFFSET,
+   retrying if interrupted.  Return the number of bytes read, which might
+   be less than NBYTES.  On error, set errno and return -1.  */
+
+ptrdiff_t
+emacs_pread (int fildes, char *buf, ptrdiff_t nbytes, ptrdiff_t offset)
+{
+  register ssize_t rtnval;
+
+  while ((rtnval = pread (fildes, buf, nbytes, offset)) == -1
+	 && (errno == EINTR))
+    QUIT;
+  return rtnval;
+}
+
 /* Write to FILEDES from a buffer BUF with size NBYTE, retrying if interrupted
    or if a partial write occurs.  Return the number of bytes written, setting
    errno if this is less than NBYTE.  */


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

* Re: Using pread
  2013-02-01  8:05 Using pread Dmitry Antipov
@ 2013-02-01  8:18 ` Paul Eggert
  2013-02-01  8:34   ` Dmitry Antipov
  0 siblings, 1 reply; 6+ messages in thread
From: Paul Eggert @ 2013-02-01  8:18 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Emacs development discussions

On 02/01/2013 12:05 AM, Dmitry Antipov wrote:
> Can we assume that all supported systems has pread(2)?

A few systems lack pread (HP-UX 10, mingw, MSVC 9, BeOS)
but there's a gnulib module for it so the mainline Emacs
code should be able to assume pread.



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

* Re: Using pread
  2013-02-01  8:18 ` Paul Eggert
@ 2013-02-01  8:34   ` Dmitry Antipov
  2013-02-01 10:33     ` Eli Zaretskii
  2013-02-02  1:47     ` Paul Eggert
  0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Antipov @ 2013-02-01  8:34 UTC (permalink / raw)
  To: Paul Eggert; +Cc: Emacs development discussions

On 02/01/2013 12:18 PM, Paul Eggert wrote:

> A few systems lack pread (HP-UX 10, mingw, MSVC 9, BeOS)
> but there's a gnulib module for it so the mainline Emacs
> code should be able to assume pread.

IMHO it's worth using at least for some bits in fileio.c.
So if you add gnulib module, I can do the rest :-).

Dmitry




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

* Re: Using pread
  2013-02-01  8:34   ` Dmitry Antipov
@ 2013-02-01 10:33     ` Eli Zaretskii
  2013-02-02  1:47     ` Paul Eggert
  1 sibling, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2013-02-01 10:33 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: eggert, emacs-devel

> Date: Fri, 01 Feb 2013 12:34:27 +0400
> From: Dmitry Antipov <dmantipov@yandex.ru>
> Cc: Emacs development discussions <emacs-devel@gnu.org>
> 
> On 02/01/2013 12:18 PM, Paul Eggert wrote:
> 
> > A few systems lack pread (HP-UX 10, mingw, MSVC 9, BeOS)
> > but there's a gnulib module for it so the mainline Emacs
> > code should be able to assume pread.
> 
> IMHO it's worth using at least for some bits in fileio.c.

Any numbers to back that up?  IOW, in what sense is this an
"optimization"?



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

* Re: Using pread
  2013-02-01  8:34   ` Dmitry Antipov
  2013-02-01 10:33     ` Eli Zaretskii
@ 2013-02-02  1:47     ` Paul Eggert
  2013-02-02  8:09       ` Eli Zaretskii
  1 sibling, 1 reply; 6+ messages in thread
From: Paul Eggert @ 2013-02-02  1:47 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Emacs development discussions

On 02/01/2013 12:34 AM, Dmitry Antipov wrote:
> IMHO it's worth using at least for some bits in fileio.c.

Is using pread significantly faster or safer, when pread is native?
If this is merely a style issue, it's probably not worth the bother.



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

* Re: Using pread
  2013-02-02  1:47     ` Paul Eggert
@ 2013-02-02  8:09       ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2013-02-02  8:09 UTC (permalink / raw)
  To: Paul Eggert; +Cc: dmantipov, emacs-devel

> Date: Fri, 01 Feb 2013 17:47:35 -0800
> From: Paul Eggert <eggert@cs.ucla.edu>
> Cc: Emacs development discussions <emacs-devel@gnu.org>
> 
> On 02/01/2013 12:34 AM, Dmitry Antipov wrote:
> > IMHO it's worth using at least for some bits in fileio.c.
> 
> Is using pread significantly faster or safer, when pread is native?

AFAIU, its only advantage is when several threads might share a file
descriptor.  But if we consider this possibility for some future
extension, such as Tom's concurrency branch, then perhaps we should
modify emacs_read to use pread as well.

> If this is merely a style issue, it's probably not worth the bother.

I agree.



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

end of thread, other threads:[~2013-02-02  8:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-01  8:05 Using pread Dmitry Antipov
2013-02-01  8:18 ` Paul Eggert
2013-02-01  8:34   ` Dmitry Antipov
2013-02-01 10:33     ` Eli Zaretskii
2013-02-02  1:47     ` Paul Eggert
2013-02-02  8:09       ` Eli Zaretskii

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.