Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Parallel array
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Pros and cons== {{no references|section|date=January 2018}} {{Pro and con list|date=May 2021}} Parallel arrays have a number of practical advantages over the normal approach: * They can save a substantial amount of space in some cases by avoiding alignment issues. For example, some architectures work best if 4-byte integers are always stored beginning at memory locations that are multiple of 4. If the previous field was a single byte, 3 bytes might be wasted. Many modern compilers can automatically avoid such problems, though in the past some programmers would explicitly declare fields in order of decreasing alignment restrictions. * If the number of items is small, array indices can occupy significantly less space than full pointers, particularly on some architectures. * Sequentially examining a single field of each record in the array is very fast on modern machines, since this amounts to a linear traversal of a single array, exhibiting ideal [[locality of reference]] and cache behaviour. * They may allow efficient processing with [[SIMD instruction]]s in certain [[instruction set architecture]]s Several of these advantages depend strongly on the particular programming language and implementation in use. However, parallel arrays also have several strong disadvantages, which serves to explain why they are not generally preferred: * They have significantly worse [[locality of reference]] when visiting the records non-sequentially and examining multiple fields of each record, because the various arrays may be stored arbitrarily far apart. * They obscure the relationship between fields of a single record (e.g. no type information relates the index between them, an index may be used erroneously). * They have little direct language support (the language and its syntax typically express no relationship between the arrays in the parallel array, and cannot catch errors). * Since the bundle of fields is not a "thing", passing it around is tedious and error-prone. For example, rather than calling a function to do something to one record (or structure or object), the function must take the fields as separate arguments. When a new field is added or changed, many parameter lists must change, where passing objects as whole would avoid such changes entirely. * They are expensive to grow or shrink, since each of several arrays must be reallocated. Multi-level arrays can ameliorate this problem, but impacts performance due to the additional indirection needed to find the desired elements. * Perhaps worst of all, they greatly raise the possibility of errors. Any insertion, deletion, or move must always be applied consistently to all of the arrays, or the arrays will no longer be synchronized with each other, leading to bizarre outcomes. The bad locality of reference can be alleviated in some cases: if a structure can be divided into groups of fields that are generally accessed together, an array can be constructed for each group, and its elements are records containing only these subsets of the larger structure's fields. (see [[data-oriented design]]). This is a valuable way of speeding up access to very large structures with many members, while keeping the portions of the structure tied together. An alternative to tying them together using array indexes is to use [[Reference (computer science)|reference]]s to tie the portions together, but this can be less efficient in time and space. Another alternative is to use a single array, where each entry is a record structure. Many language provide a way to declare actual records, and arrays of them. In other languages it may be feasible to simulate this by declaring an array of n*m size, where m is the size of all the fields together, packing the fields into what is effectively a record, even though the particular language lacks direct support for records. Some [[compiler optimization]]s, particularly for [[vector processor]]s, are able to perform this transformation automatically when arrays of structures are created in the program.{{Citation needed|date=April 2012}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)