Extending a simple array dynamically

Discussion of Common Lisp

Extending a simple array dynamically

Postby ender2012 » Wed Jun 10, 2009 9:35 am

I have made a .obj loader and I am currently trying to work on increasing efficiency in my draw cycle. I have read that using a simple array and svref instead of elf would make for much quicker array lookups from my data structures where I store vertices and such. My question is there anyway to extend the size of a simple array dynamically? Or would I have to pre parse my file to determine what size array I would need for each data type and then set the simple array to be that size?
ender2012
 

Re: Extending a simple array dynamically

Postby Ramarren » Wed Jun 10, 2009 10:51 am

First, the next more specific array accessor than ELT is AREF, and at least on SBCL I think that there is no much point in using explicit SVREF, since usually type declarations/inference will automatically expand to specialized version.

Anyway, by definition of simple array it cannot be dynamically adjustable. But I am not sure I understand your use case... If you are gathering objects when parsing a file, you might just as well collect them in a list and when done convert it to an array for faster lookups. It is also of course always possible to copy the contents into a newly allocated, bigger array. Or use some kind of rope-like datastructure, ie. have a tree of array fragments, but I doubt this would be useful in this case.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Extending a simple array dynamically

Postby slobodan.blazeski » Wed Jun 10, 2009 3:42 pm

VECTOR-PUSH-EXTEND is what you want but array must be adjustable.
http://www.lispworks.com/documentation/ ... vec_ps.htm

Arrays are continuous blocks of memory I really doubt you will gain much if at all by using simple array especially in accesing elements.
Maybe it would be better if you make your code with adjustable array and then profile where the problem really is.
slobodan.blazeski
 
Posts: 9
Joined: Tue May 26, 2009 8:27 am

Re: Extending a simple array dynamically

Postby nuntius » Wed Jun 10, 2009 6:49 pm

ender2012 wrote:I have made a .obj loader and I am currently trying to work on increasing efficiency in my draw cycle. I have read that using a simple array and svref instead of elf would make for much quicker array lookups from my data structures where I store vertices and such. My question is there anyway to extend the size of a simple array dynamically? Or would I have to pre parse my file to determine what size array I would need for each data type and then set the simple array to be that size?


First off, are you planning on making the .obj loader public? I had started in on one, but got sidetracked. This would be a good thing to have alongside projects like cl-opengl and cl-glfw...

As for your question, I think the best answer is to construct a list while parsing the file and then copy it into a simple-vector for rendering.
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Extending a simple array dynamically

Postby simon » Thu Jun 11, 2009 2:41 pm

slobodan.blazeski wrote:VECTOR-PUSH-EXTEND is what you want but array must be adjustable.
http://www.lispworks.com/documentation/ ... vec_ps.htm

Arrays are continuous blocks of memory I really doubt you will gain much if at all by using simple array especially in accesing elements.
Maybe it would be better if you make your code with adjustable array and then profile where the problem really is.



Careful; this is both good advice and bad. With an optimizing compiler that can take advantage of it, it can make a huge difference to array access times to replace arrays with simple-arrays and the appropriate declarations. If the array isn't simple, there is an indirect on every single access, which adds up (and may be most of the cost per access). This can be an order of magnitude slower. Of course, in order for this to be the thing that is actually slowing you down, the code will obviously be pretty fast in other aspects.

However, you're quite right that an adjustable vector (or a list when it's being read int) may be exactly what the OP wants for loading, and there really isn't any point worrying about the array access unless it's known to be a factor.
simon
 
Posts: 16
Joined: Wed May 13, 2009 9:12 am


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron