Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!princeton!news.princeton.edu!blume
From: blume@dynamic.cs.princeton.edu (Matthias Blume)
Subject: Re: More than one way to
In-Reply-To: shivers@clark.lcs.mit.EDU's message of 15 Feb 1995 11:08:03 -0500
Message-ID: <BLUME.95Feb16004739@dynamic.cs.princeton.edu>
Originator: news@hedgehog.Princeton.EDU
Sender: news@Princeton.EDU (USENET News System)
Nntp-Posting-Host: dynamic.cs.princeton.edu
Organization: Princeton University
References: <dig-Scheme-7.45@mc.lcs.mit.edu> <9502151605.AA01108@clark.lcs.mit.edu>
Date: Thu, 16 Feb 1995 05:47:39 GMT
Lines: 46

In article <9502151605.AA01108@clark.lcs.mit.edu> shivers@clark.lcs.mit.EDU (Olin Shivers) writes:

       2. CAT-LOOP has a loop variable PORT which is not a loop variable at all.
	  It's a loop constant. So pull it out of the loop. Loop variables should
	  capture per-iteration loop state. (I beat my students up about this
	  all the time. It's a weird blind spot programmers have until they
	  make an effort to stamp it out.) Removing loop constants from the
	  loop vars will make the loop easier to reason about -- both for you
	  and for the compiler.

First of all, you should stop beating up your student, no matter what
programs they write. :)

Furthermore, your example is not good.  I agree with you that for
clarity it is probably better to pull the port argument out of the
loop.  For convenience (being able to feed the loop body directly to
CALL-WITH-INPUT-FILE) the port argument is better.

The performance argument is rather irrelevant.  One should go for
clarity first, not for performance. (If we are lucky, then these two
goals coincide, but in many cases they don't.) Programmer
optimizations are usually a bad idea. One should first write the
program in a straight-forward way, find the hot-spots, and then
optimize those.

Since the example is heavily I/O-bound it will hardly make any
difference whether you pass PORT as an argument, or whether it is a
free variable of the loop.

Moreover, even in less I/O-bound situations we wouldn't see much of a
difference, because every decent compiler would produce approximately
the same code for both versions.  Arguments are usually passed in
registers, and free variables should be kept in registers as well (if
possible).  In order to express the latter case many compilers will
actually put free variables back into argument lists in the
intermediate representations of the program.  If this is the case then
it is likely that both versions produce *precisely* the same object
code.

Dumb compilers (like VSCM's, for example :) will actually produce
faster code for the version where you pass the value as an explicit
argument, because - stupid as they are - they create a closure for
loop bodies which have free variables in them...

--
-Matthias
