From: ············@gmail.com
Subject: with-slots on instances of structure classes and performance
Date: 
Message-ID: <1146257964.429015.203070@e56g2000cwe.googlegroups.com>
Hi,

most current Common Lisp implementations allow the use of SLOT-VALUE on
structure classes, thus making it possible to use WITH-SLOTS with
structures. It is a very convenient feature allowing to combine faster
acces with concise syntax.

LispWorks has another valuable feature: it optimizes (SLOT-VALUE (THE
structure-type variable) 'symbol) into an inline structure accessor by
offset. It makes the syntax equivalent to calling slot-accessors (which
is a fast inline operation). Moreover, it does the same for variables
with declared types, that is:

(declare (type structure-type variable))
...
(slot-value variable 'symbol)

will be compiled into a couple of instructions retrieving a value by
fixed offset.

Unfortunately, other compilers don't do that, and that sacrifices
performance (or forces to write verbose slot accessors). It can be
partially remedied by redefined WITH-SLOTS (as with-slits) which
substitutes structure slot accessors in cases when the type is known
(that is, via explicit THE) -- I had to do that to get comparable
performance from Allegro.

But I can't find a way to reproduce the functionality completely inside
Common Lisp, I see know way to retrieve the type information from
declarations in macros. That is, when there is no (THE ...) but an
earlier declaration. Is there a way to do that?

David

From: jayessay
Subject: Re: with-slots on instances of structure classes and performance
Date: 
Message-ID: <m31wvh48w6.fsf@rigel.goldenthreadtech.com>
·············@gmail.com" <············@gmail.com> writes:

> But I can't find a way to reproduce the functionality completely
> inside Common Lisp, I see know way to retrieve the type information
> from declarations in macros. That is, when there is no (THE ...) but
> an earlier declaration. Is there a way to do that?

That's what environment access can buy you.  If (as you mentioned) you
are still using Allegro, I believe you should be able to get at this
in macros via the environment access stuff in ACL 7+.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: ············@gmail.com
Subject: Re: with-slots on instances of structure classes and performance
Date: 
Message-ID: <1146328826.235819.229480@e56g2000cwe.googlegroups.com>
> That's what environment access can buy you.  If (as you mentioned) you
> are still using Allegro, I believe you should be able to get at this
> in macros via the environment access stuff in ACL 7+.

Yes, thanks a lot, that was it. I've implemented it for Allegro (don't
see a way for CMU, but its structure accessors are fast anyway, and the
code is almost as fast), and now my code is only 15-20% slower with
Allegro than with LispWorks.

On the other hand, it is a shame that Lisp forces to do such things
manually (and in implementation-specific way), and a case when
Hindley-Milner type system and type derivation would shine. 

David
From: jayessay
Subject: Re: with-slots on instances of structure classes and performance
Date: 
Message-ID: <m3bquk2nkb.fsf@rigel.goldenthreadtech.com>
·············@gmail.com" <············@gmail.com> writes:

> > That's what environment access can buy you.  If (as you mentioned) you
> > are still using Allegro, I believe you should be able to get at this
> > in macros via the environment access stuff in ACL 7+.
> 
> Yes, thanks a lot, that was it. I've implemented it for Allegro (don't
> see a way for CMU, but its structure accessors are fast anyway, and the
> code is almost as fast), and now my code is only 15-20% slower with
> Allegro than with LispWorks.

That's good.


> On the other hand, it is a shame that Lisp forces to do such things
> manually

It's not statically typed.  And furthermore, type inference like this
is often domain oriented/specific.  I have a "type" inferencer
(actually it does more than just type inference, in particular various
code motion) in a major system I built (on Allegro) even before the
environment stuff was available and it takes major advantage of
various domain specific information (operation types, their behavior,
side effecting aspects, et.al.)


> (and in implementation-specific way),

Well, the code for Allegro's environment access is a public lib which
other implementations can take and incorporate to fulfill this in a
(defacto)standard(!!!) way.


> Hindley-Milner type system and type derivation would shine. 

Maybe your application would benefit more from using Haskell/*ML???


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Joerg Hoehle
Subject: Re: with-slots on instances of structure classes and performance
Date: 
Message-ID: <ufyjf2ric.fsf@users.sourceforge.net>
·············@gmail.com" <············@gmail.com> writes:
> most current Common Lisp implementations allow the use of SLOT-VALUE on
> structure classes, thus making it possible to use WITH-SLOTS with
> structures. It is a very convenient feature allowing to combine faster
> acces with concise syntax.

As convenient as it may be, it's not portable.  If you appreciate
portable code, why don't you create a SPEED-SLOTS macro that DTRT?  It
would expand to WITH-SLOTS on Lispworks, and do other things on other
platforms (when possible).

I remember creating a VAR-TYPECASE for use in CMUCL years ago to optimize
 (TYPECASE (<variable>)
   (<type> <forms...>) ...)
to
   (<type> (locally (declare <type> <variable>) forms))

Of course, it would have been nicer if cmucl had done this inference
itself (maybe it does now). But writing the macro was good enough for
our purposes -- and trivial to do.

Generally, as writing code that optimizes well on several CL
implementations is not immediate, using tiny wrapper macros goes a
long way IMHO.

For example, CLISP has a macro FCASE to use CASE with another test
than EQL.  Quite handy, and using it leads to better performance in
CLISP than the naive (COND ((<test> foo bar) ...)).  I've seen exactly
this pattern in some piece of CL code whose author did not use CLISP.

> But I can't find a way to reproduce the functionality completely inside
> Common Lisp, I see know way to retrieve the type information from
> declarations in macros. That is, when there is no (THE ...) but an
> earlier declaration. Is there a way to do that?
No, as others said.  Environment Access so far is Allegro only and I
cannot comment for now on its general suitability for other
implementations.

Regards,
	Jorg Hohle
Telekom/T-Systems Technology Center