https://github.com/tkf/recordarrays.jl
https://github.com/tkf/recordarrays.jl
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tkf/recordarrays.jl
- Owner: tkf
- License: mit
- Created: 2021-05-19T18:20:07.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-04-06T05:52:07.000Z (about 4 years ago)
- Last Synced: 2025-06-24T20:41:26.723Z (11 months ago)
- Language: Julia
- Size: 15.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RecordArrays.jl: flexible Array-of-Structures representation for Julia
**NOTE**: Packages such as
[StructArrays.jl](https://github.com/JuliaArrays/StructArrays.jl),
[TupleVectors.jl](https://github.com/cscherrer/TupleVectors.jl), and
[TypedTables.jl](https://github.com/JuliaData/TypedTables.jl) provide the
abstract array interface for Structure-of-Arrays representation which is much
more appropriate for many performance-oriented programs.
RecordArrays.jl is a package for using Array-of-Structures representation with
more control than `Array{T}`. For example, it can be used for creating
task-local state aligned to cache line. Updating a single field at single
index does not mutate other fields.
## Usage
```julia
julia> using RecordArrays
julia> xs = RecordArrays.fill((a = 1, b = 2), 5; align = 64)
5-element RecordArray{NamedTuple{(:a, :b), Tuple{Int64, Int64}},1,…}:
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
julia> all(i -> mod(UInt(pointer(xs, i)), 64) == 0, eachindex(xs))
true
julia> xs[1] = (a = 111, b = 222);
julia> xs
5-element RecordArray{NamedTuple{(:a, :b), Tuple{Int64, Int64}},1,…}:
(a = 111, b = 222)
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
julia> xs.a
5-element FieldArray{:a,Int64,1,…}:
111
1
1
1
1
julia> xs.a[2] = 11111;
julia> xs
5-element RecordArray{NamedTuple{(:a, :b), Tuple{Int64, Int64}},1,…}:
(a = 111, b = 222)
(a = 11111, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
julia> x3 = view(xs, 3) # acts like a `NamedTuple` of `Ref`s
1-element RecordArray{NamedTuple{(:a, :b), Tuple{Int64, Int64}},0,…}:
(a = 1, b = 2)
julia> x3.a[]
1
julia> x3.a[] = 333;
julia> xs
5-element RecordArray{NamedTuple{(:a, :b), Tuple{Int64, Int64}},1,…}:
(a = 111, b = 222)
(a = 11111, b = 2)
(a = 333, b = 2)
(a = 1, b = 2)
(a = 1, b = 2)
```
Use `RecordArray{T}(undef, dims)` to allocate a new uninitialized array:
```julia
julia> using RecordArrays
julia> xs = RecordArray{Some{Union{Nothing,Int}}}(undef, 3);
julia> xs .= Some.(1:3)
3-element RecordArray{Some{Union{Nothing, Int64}},1,…}:
1
2
3
```
Another way to allocate a new array is to use `RecordArrays.unsafe_zeros`:
```julia
julia> using RecordArrays
julia> xs = RecordArrays.unsafe_zeros(NTuple{5, UInt8}, 3)
3-element RecordArray{NTuple{5, UInt8},1,…}:
(0x00, 0x00, 0x00, 0x00, 0x00)
(0x00, 0x00, 0x00, 0x00, 0x00)
(0x00, 0x00, 0x00, 0x00, 0x00)
```
## See also
* https://github.com/Vitaliy-Yakovchuk/StructViews.jl