Re: specialized array slower than unspecialized?



In article <1175349057.554480.255380@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"andrew.baine@xxxxxxxxx" <andrew.baine@xxxxxxxxx> wrote:

On Mar 30, 9:51 pm, Barry Margolin <bar...@xxxxxxxxxxxx> wrote:
In article <1175301662.043024.201...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,

turing.incompl...@xxxxxxxxx wrote:
Hello,

I'm working on my first non-toy lisp program (a Monte Carlo simulation
code). I was experimenting with different ways of writing a function
when I accidentally noticed the following: it seems that declaring the
element-type of an array makes the code slower (and also cons more),
but I was under the impression that the opposite should be the case.
Can somebody please enlighten me? (I hope this question is not too
stupid)

If the array is specialized, then every time you extract an element from
it the system has to allocate an object to hold it -- this is
traditionally called "boxing".

On the other hand, if the array is general then it just contains
pointers to the objects, and extracting from the array just involves
returning that pointer.

Boxing can be avoided if you provide enough declarations that the
compiler can generate code that operates directly on the specialized
representation. Your code doesn't have any type declarations, so
everything ends up being done with generic code.

Very interesting -- so if you use an :element-type argument to make-
array you're not likely to get any benefit unless you also use type
declarations when you use that array?

There's still benefit in some cases. If the array is large, specialized
arrays of small integer types can use much less memory. E.g. :TYPE
(UNSIGNED-BYTE 8) will just use one byte per element, rather than 4 (or
8 if you have a 64-bit Lisp).

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: specialized array slower than unspecialized?
    ... If the array is specialized, then every time you extract an element from ... pointers to the objects, and extracting from the array just involves ... Boxing can be avoided if you provide enough declarations that the ...
    (comp.lang.lisp)
  • Re: how to use 1D array as a multidimensional array
    ... Do you mean those declarations literally? ... If the array size is actually fixed, as it is in your example, ... of the details of pointers and dummy arguments are quite tricky, ... you don't need the complications and side issues of this method. ...
    (comp.lang.fortran)
  • Re: Design orientated C question
    ... F1(HDC hdc,struct tagIdRoomIndex RIndex) ... when I am passing the array of structures by the calling function: ... The only things that should go in header files are structure definitions and function declarations. ... Every .cpp file that uses structure/class definitions or function declarations from a header must include that header. ...
    (microsoft.public.vc.language)
  • Re: garbage collector pauses, help?
    ... Very heavy on floating point computations. ... Once I add declarations, I feel like ... > about no consing at all once the population of genomes is initialized ... processing step generate a new copy of the array. ...
    (comp.lang.lisp)
  • Re: Wired binary behavior
    ... The order of declarations affects, on most implementations, the ... if you happen to be overwriting an array or before the end ... somewhere in the OP's code he scribbles past the end of ... Writing past the end of an array is evil, ...
    (comp.lang.c)