https://github.com/liquidev/datarray
Struct-of-arrays style data structure that emulates array-of-structs access
https://github.com/liquidev/datarray
Last synced: about 2 months ago
JSON representation
Struct-of-arrays style data structure that emulates array-of-structs access
- Host: GitHub
- URL: https://github.com/liquidev/datarray
- Owner: liquidev
- License: mit
- Created: 2021-04-01T22:26:21.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-03T11:30:44.000Z (about 4 years ago)
- Last Synced: 2025-01-20T14:39:50.965Z (3 months ago)
- Language: Nim
- Size: 15.6 KB
- Stars: 39
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Datarray
> Data oriented design made easy.
Inspired by [this article](https://blog.royalsloth.eu/posts/the-compiler-will-optimize-that-away/).
Datarray is a struct of arrays data structure that can be used just like an
object oriented-style array of structs.```nim
import datarraytype
Ant = object
name: string
color: string
isWarrior: bool
age: int32var antColony: Datarray[1000, Ant]
# multiple styles of data access:
var numberOfWarriors = 0# a) use the fields directly
for i in 0.. 1 and color == "red":
inc numChosen
echo numChosen```
In all of these examples, datarray does some memory magic to turn `Ant` into
the following:```nim
type
AntColony = object
names: array[1000, string]
colors: array[1000, string]
isWarrior: array[1000, bool]
age: array[1000, int32]
```while still retaining easy, OOP-like field access using the dot operator.
## Benchmarks
`nim r --passC:-march=native --passC:-flto -d:danger --exceptions:goto tests/benchmark.nim`
Running on an AMD Ryzen 5 1600.
```
name ............................... min time avg time std dv runs
array of ref Ant ................... 2.450 ms 2.530 ms ±0.044 x1000
array of Ant ....................... 1.292 ms 1.312 ms ±0.009 x1000
AntColony .......................... 0.594 ms 0.596 ms ±0.002 x1000
Datarray ith() ..................... 0.362 ms 0.396 ms ±0.004 x1000
Datarray Element[T] ................ 0.594 ms 0.595 ms ±0.001 x1000
```For some reason, not using LTO and goto-based exceptions tanks the performance
of `Element[T]`, but I'm yet to discover the reason behind this (or potentially
a better solution that doesn't have such high performance overhead).
Thus, try to avoid `Element[T]` in performance-critical code, and use `ith` and
`select` instead of it.