<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7036.0">
<TITLE>RE: [racket] case form implemented with a hash</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">I tried the binary search. Speed up in my case about 10%. Not very spectacular.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">The macro is a dragon.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">Jos </FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">-----Original Message-----</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">From: Matthew Flatt [</FONT></SPAN><A HREF="mailto:mflatt@cs.utah.edu"><SPAN LANG="es"><U><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">mailto:mflatt@cs.utah.edu</FONT></U></SPAN></A><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">] </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">Sent: jueves, 29 de septiembre de 2011 15:08</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">To: Jos Koot</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">Cc: 'Racket-users'</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">Subject: RE: [racket] case form implemented with a hash</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">I don't see Clinger's technique as useful only for a small number of</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">clauses. Although I'm not sure that 86 clauses is enough to tell the</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">difference, just to double-check: did you try a binary search instead</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">of a hash table?</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">That is, instead of</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (= x 0)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e0</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (= x 1)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e1</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (= x 2)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e2</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e3)))</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">using</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (x . < . 2)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (x . < . 1)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e0</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e1)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> (if (x . < . 3)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e2</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New"> e3))</FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">? </FONT></SPAN>
</P>
<P><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">At Thu, 29 Sep 2011 12:25:16 +0200, "Jos Koot" wrote:</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Thanks Matthew.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> I read the paper.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> In my case I have a case form which dispatches on a character, 86 different</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> ones. Therefore I can dispatch by means of a vector. However, almost every</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> character has its own clause and therefore dispatching on the index of the</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> character would give no speed up. I already tried a hash-table and a vector</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (making sure it is computed once only) The values in the table/vector are</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> procedures that run the body of a clause. The vector is used as follows:</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (define (parse-fmt-instr parser-state) ; Parses one single instruction.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (skip-white-fmt-and-commas parser-state)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (let ((char (pop-fmt-char parser-state #f)))</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (and char</FONT></SPAN>
<BR><SPAN LANG="el"><FONT SIZE=2 FACE="Courier New Greek">> (if (char=? char #\¦Ë) call-proc-instr</FONT></SPAN><SPAN LANG="es"></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (let ((i (char->integer char)))</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (if (< i 128)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> ((vector-ref parser-table i) parser-state char)</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> (fmt-error parser-state)))))))</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> However, as you predicted, I found no speed up. With a hash table in some</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> cases even a slow down.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> The gain in speed of dispatching seems to be destroyed by the extra</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> procedure call.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> I think Clinger's paper is particularly useful for case forms wih many keys</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> but few clauses.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Thanks for your help.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Jos </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> -----Original Message-----</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> From: Matthew Flatt [</FONT></SPAN><A HREF="mailto:mflatt@cs.utah.edu"><SPAN LANG="es"><U><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">mailto:mflatt@cs.utah.edu</FONT></U></SPAN></A><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">] </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Sent: mi¨¦rcoles, 28 de septiembre de 2011 22:59</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> To: Jos Koot</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Cc: 'Racket-users'</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Subject: RE: [racket] case form implemented with a hash</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> I had in mind the comment on page 64, bottom right, but I see that it's</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> a smaller comment than in my memory.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Overall, I think it will work better to use a hash table to go from a</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> value to an index, and then use a binary search on the index as in</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> Clinger's paper. Indirect dispatch via thunks in a hash table will most</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> likely be slower.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> At Wed, 28 Sep 2011 22:55:08 +0200, "Jos Koot" wrote:</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Hi Hatthew,</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > I am still trying to implement 'case' by means of a hash.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Can you give me a more specific pointer. I followed your pointer but did</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> not</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > see anything relevant to my question.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Jos </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > -----Original Message-----</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > From: Matthew Flatt [</FONT></SPAN><A HREF="mailto:mflatt@cs.utah.edu"><SPAN LANG="es"><U><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">mailto:mflatt@cs.utah.edu</FONT></U></SPAN></A><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">] </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Sent: mi¨¦rcoles, 28 de septiembre de 2011 22:20</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > To: Jos Koot</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Cc: 'Racket-users'</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > Subject: Re: [racket] case form implemented with a hash</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > At Wed, 28 Sep 2011 22:08:44 +0200, "Jos Koot" wrote:</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > By inspection with the macro stepper I found that case forms are</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> expanded</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > to</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > nested if-forms. For case forms with many clauses, this may be</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > inefficient.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > I have tried to prepare a hash-case form using a hash table in order to</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > select the desired clause more efficiently. So far my attempts failed. I</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > am</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > still trying. However, has anybody else on this list thought of using a</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > hash</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > for case forms with many clauses? Does it exist? If not, would anyone be</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > interested in a hash-case form that has the same shape as a case-form,</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> but</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > uses a hash-table to select the appropriate clause? The hash table</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> should</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > be</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > a map onto thunks and macro hash-case simply would have to retrieve the</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > > thunk and call it.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > See "Rapid Case Dispatch in Scheme", Clinger, Scheme'06.</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > </FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > If you implement a revised `case' that consistently works better, then</FONT></SPAN>
<BR><SPAN LANG="es"><FONT SIZE=2 FACE="Courier New">> > I'd be happy to use it as a replacement for the current `case'.</FONT></SPAN>
</P>
</BODY>
</HTML>