Skip to content

Growable Array Performance

ADMIN | 05-05-2011

Here’s a subject to which everyone can relate in one way or another: growable arrays.

Hexagon pattern 1

Here’s a subject to which everyone can relate in one way or another: growable arrays. An array is a contiguous memory block that provides O(1) random access via indexing. “Growable” means that the array has the ability to expand, accommodating more elements as needed. I’ve seen numerous homegrown implementations for “generic arrays”, “dynamic arrays”, “generic block arrays” – all written by myself and fellow teammates over the years. We rolled our own because the Standard Template Library has taken quite some time to reach an equal footing on all platforms (SGI, AIX, Solaris, Linux, Windows, etc.).

The growable array is an interesting container to study. There are two ways to grow, both fundamentally similar:

• Supposing you’re using simple data types (non-objects), your best bet is to use the heap function realloc for allocating and growing your array. At best, realloc simply expands the memory block you’re using, returning the same pointer. At worst, realloc is unable to expand and must find a new block to meet your needs – and copy your data across.

• If you’re using objects (that have meaningful construction/destruction behavior), you’re stuck using new/delete, because the C-runtime heap functions will not call your ctor/dtor. Also, there is no equivalent “realloc” for C++ objects – so you always have to allocate the larger space, copy the objects across, and release the old memory.

The takeaway: when the growable array expands, growing is expensive. Assuming the latter case (not able to use realloc) – the array elements (objects) must be copied from the old array to the new array – every object element must constructed in the new array and destructed in the old, possibly thousands or millions of times.

The solution? To cut down on number of times you’ll need to grow, grow the array in large chunks. Taken to the extreme, you could simply allocate the maximum number of elements you’d ever expect, avoiding the growing step in all but extreme outlier cases… In the vast majority of cases, however, this would lead to extreme memory waste.

It is trivial to conduct a performance study on growth rates versus memory waste. Consider my findings based on small to very-large memory target sizes (~100 elements to 75 million elements):

Growth rate

Time  (s)

Memory waste (MB)

100% (double every time)

0.34

175

45%

0.52

54

25%

0.85

35

10%

1.93

19

The results speak for themselves – the larger the growth rate (chunk size), the better the performance – but you waste the most memory. On the flip-side, if you grow your array slowly, we see struggling performance, but more efficient memory usage.

Here’s what missing from the equation: context. In almost every situation, you as the developer have context that can help this situation immensely: you know the problem you’re trying to solve. The key knowledge is this: you have some sense of the target size of the array. With this knowledge, you can construct your growable array with an initial size that meets the vast majority of your algorithm’s needs, both maximizing performance and minimizing waste.

It should be no surprise then, that STL’s vector class has a reserve(size_type) method, allowing you to specify an initial array size.

Take a look around your code. Is there a homegrown growable array? Can you reserve an initial size? Do you use STL vector – are you using the reserve() method? Depending on your algorithms and the pressure placed on your growable arrays, this small change can make a measurable performance difference.

We did this a couple of years ago in ACIS: after a fair amount of study, my buddy Jeff discovered that the vast majority of our LIST_HEADER usages contained 16 elements or less. After modifying LIST_HEADER to always start with 16 elements, Jeff’s simple change improved ACIS’ overall performance around 5% – a simple change with a huge result.

You might also like...

c138_2_cropped
3 Min read
3D ACIS
We often focus the success of new partners, showcasing how Spatial helped with bringing a new product to market. But...
Large Tolerant  Vertex
3 Min read
3D Software Development Kits
boolean operation Boolean operations on individual bodies are common functions in 3D modeling. While simple in concept,...
3 Min read
3D Software Development Kits
In much the same way as physical design has moved from paper 2D drawings to 3D models in software, so has analysis....
4 Min read
3D InterOp
Part and parcel with model-based engineering is model translation. Because the model is now the specification, accurate...
4 Min read
3D ACIS
We are moving things around in the office here at Spatial to accommodate some new people. As a result, our marketing...
2 Min read
3D ACIS
This article shares a simple architecture which can be used to capture meta-data about the use of ACIS APIs in your...
1 Min read
3D ACIS
How To Create an Ellipsoid using 3D ACIS The 3D analytics supported directly in 3D ACIS include: sphere, block,...
2 Min read
3D ACIS
Basically, there are two priorities when using a software component, particularly a 3D modeling kernel: Does it do what...
1 Min read
3D ACIS
A geometry kernel is a big thing. It’s a huge thing. Maybe even big enough to see from space. By most accounts, even...
1 Min read
3D ACIS
My comrades and I did a performance analysis of Intel’s Hyper Threading Technology (HTT), using thread-safe ACIS and...
3 Min read
3D ACIS
In earlier posts I’ve written a lot about the various approaches to multiprocessing and the potential benefits. What I...
2 Min read
3D ACIS
The challenges of a major software release are not unique to Spatial. And like other organizations, the launch process...
5 Min read
3D ACIS
We’ve known for a long time that the integrity of B-rep data plays a major role in the success of downstream modeling...
3 Min read
3D ACIS
Answer: when it’s a 'HappyPathPoint'.
1 Min read
3D ACIS
To finish up this series of posts; what Gregg's post described happened a few years ago. Since that first team room,...
4 Min read
3D ACIS
This post will discuss two aspects of my favorite programming language, C++:
2 Min read
3D ACIS
Way back in the Dark Ages of the mid-90s, I used to read a newsgroup called comp.lang.c++. You can tell this was the...
7 Min read
3D ACIS
Here’s a subject to which everyone can relate in one way or another: growable arrays. An array is a contiguous memory...