guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 01/01: Optimize peek-char


From: Andy Wingo
Subject: [Guile-commits] 01/01: Optimize peek-char
Date: Thu, 28 Apr 2016 05:58:16 +0000

wingo pushed a commit to branch wip-port-refactor
in repository guile.

commit f24802b77055c27514a8c2d2766c6734d323b708
Author: Andy Wingo <address@hidden>
Date:   Thu Apr 28 07:54:07 2016 +0200

    Optimize peek-char
    
    * libguile/ports.c (scm_peek_char): Optimize.  A loop calling peek-char
      on a buffered string port 10e6 times goes down from 50ns/iteration to
      32ns/iteration.
---
 libguile/ports.c |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/libguile/ports.c b/libguile/ports.c
index cc33bd9..394d632 100644
--- a/libguile/ports.c
+++ b/libguile/ports.c
@@ -2124,17 +2124,29 @@ SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
            "sequence when the error is raised.\n")
 #define FUNC_NAME s_scm_peek_char
 {
-  int err;
+  int first_byte, err;
   SCM result;
   scm_t_wchar c;
   char bytes[SCM_MBCHAR_BUF_SIZE];
   long column, line;
   size_t len = 0;
+  scm_t_port_internal *pti;
 
   if (SCM_UNBNDP (port))
     port = scm_current_input_port ();
   SCM_VALIDATE_OPINPORT (1, port);
+  pti = SCM_PORT_GET_INTERNAL (port);
+
+  /* First, a couple fast paths.  */
+  first_byte = peek_byte_or_eof (port);
+  if (first_byte == EOF)
+    return SCM_EOF_VAL;
+  if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1)
+    return SCM_MAKE_CHAR (first_byte);
+  if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8 && first_byte < 0x80)
+    return SCM_MAKE_CHAR (first_byte);
 
+  /* Now the slow paths.  */
   column = SCM_COL (port);
   line = SCM_LINUM (port);
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]