chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] read-byte, etc.


From: Alex Shinn
Subject: Re: [Chicken-users] read-byte, etc.
Date: Thu, 06 Apr 2006 08:41:55 -0500
User-agent: Wanderlust/2.10.1 (Watching The Wheels) SEMI/1.14.6 (Maruoka) FLIM/1.14.6 (Marutamachi) APEL/10.6 Emacs/21.3 (i386-pc-linux-gnu) MULE/5.0 (SAKAKI)

At Thu, 6 Apr 2006 14:14:26 +0200, Hans Bulfone wrote:
> 
> On Tue, Apr 04, 2006 at 02:34:16AM -0500, Alex Shinn wrote:
> > At Mon, 3 Apr 2006 13:11:05 +0200, Hans Bulfone wrote:
> > > 
> > > i would have also expected this version to be slightly
> > > faster because read-char doesn't need to be looked up
> > > on every invokation... but it got slightly slower;
> > > can anybody explain why?

Ah, OK, I tried and see there is in fact a tiny slow-down.

It looks like what the compiled C code ends up doing is just the
opposite of what you expected.  Binding read-char in a LET creates a
closure, and the code for accessing a closure reference seems to take
one extra level of indirection compared to a global reference.  Or
something :) But the difference is so small I wouldn't worry about it.

I did, however, oversimplify when I said it would work for all
situations.  It still fails if they both require and _import_ utf8
before requiring your module, because then by the time it gets loaded
READ-CHAR has already been redefined.  In general, if you want to
import a syntax-case module into the top-level, you should require
everything first then do any imports.  Otherwise you may get
unpredictable behavior - it was wrapped in a module for a reason!

For this particular case if you're really paranoid you can
bullet-proof it against user error:

(define (read-byte . args)
  (let ((p (if (eq? args '()) ##sys#standard-input (car args))))
    (##sys#check-port-mode p #t 'read-char)
    (let ((ch (##sys#read-char-0 p)))
      (if (eof-object? ch)
          ch
          (char->integer ch)))))

-- 
Alex




reply via email to

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