Newsgroups: comp.lang.lisp
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!barmar
From: barmar@think.com (Barry Margolin)
Subject: Re: Accessing a field of a Lisp structure
Message-ID: <1991Apr14.073241.23653@Think.COM>
Sender: news@Think.COM
Organization: Thinking Machines Corporation, Cambridge MA, USA
References: <1991Apr10.215214.2827@cbnewsl.att.com> <28047A1F.44CE@ibma0.cs.uiuc.edu> <1991Apr11.215248.9075@cbnewsl.att.com>
Distribution: usa
Date: Sun, 14 Apr 91 07:32:41 GMT

In article <1991Apr11.215248.9075@cbnewsl.att.com> davel@cbnewsl.att.com (David Loewenstern) writes:
>In article <28047A1F.44CE@ibma0.cs.uiuc.edu> epstein@sunc4.cs.uiuc.edu (Milt Epstein) writes:
>>I sent a message to Barry suggesting the INTERN-FORMAT version, and he
>>pointed out a couple of things:
>>
>>1. It's better to use FIND-SYMBOL than INTERN, since the symbol (the
>>structure accessor function) should already exist.
>
>Maybe.  The cost of using find-symbol is that the macro writer
>must add some error handling code, or else (access-field instance oops)
>will generate some unreadable nonsense about test-NIL being undefined.

Both FIND-SYMBOL and INTERN will return the appropriate symbol if a valid
slot name was specified.  If a nonexistent slot name was specified, INTERN
will result in an undefined function error regarding TEST-<bad-slot-name>,
while FIND-SYMBOL will result in an attempt to funcall NIL.

>>2. The CASE version appeared to be much faster (he did some timing
>>test on a Symbolics).
>
>This, however, I do not believe.  After all, the macro 
>
>(access-field instance a)
>
>expands during compile time directly into
>(test-a instance)
>
>The function access-field *calls* test-a and so cannot possibly
>be faster than test-a alone.

Ahh, now I see your confusion.  I didn't write a macro, I wrote a function.
A macro can't be used for what the original poster wanted.  He wanted a
function that takes the slot name as an argument, so that the slot can be
specified at runtime.  His example was something like:

(defun prt (person slot)
  (print (user-slot person slot)))

Your macro version doesn't seem to be very useful to me.  If the slot name
has to be known at compile time then it is just as easy to write the call
to the accessor directly.  What's the difference between

(access-field instance a)

and

(test-a instance)

?  The original post asked for something that would be

(access-field instance 'a)

That little quote makes a BIG difference.

>The only way the function could be faster than the macro is if you
>are including the macroexpansion time with the execution time.

Now that I've explained, I think you see why the CASE version is faster.
The FORMAT-INTERN version calls FORMAT and conses a string each time it is
called.

--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar
