Skip to content

Commit a5f0e3d

Browse files
committed
refactor: optimize attribute loading with improved byte swap handling
1 parent df66fc3 commit a5f0e3d

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/loading/attribute.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Load all attribute entries for a given attribute from its AEDRs.
99
@inline function load_attribute_entries(buffer::Vector{UInt8}, adr, RecordSizeType, cdf_encoding)
1010
head = max(adr.AgrEDRhead, adr.AzEDRhead)
1111
offsets = get_offsets(buffer, head, RecordSizeType)
12+
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
1213
return map(offsets) do offset
13-
load_aedr_data(buffer, offset, RecordSizeType, cdf_encoding)
14+
load_aedr_data(buffer, offset, RecordSizeType, needs_byte_swap)
1415
end
1516
end
1617

@@ -19,7 +20,7 @@ end
1920
2021
Load all attributes from the CDF file.
2122
"""
22-
function attrib(cdf::CDFDataset; predicate=is_global)
23+
function attrib(cdf::CDFDataset; predicate = is_global)
2324
RecordSizeType = recordsize_type(cdf)
2425
buffer = cdf.buffer
2526
cdf_encoding = cdf.cdr.encoding
@@ -59,14 +60,15 @@ function vattrib(cdf::CDFDataset, varnum::Integer)
5960
RecordSizeType = recordsize_type(cdf)
6061
buffer = cdf.buffer
6162
cdf_encoding = cdf.cdr.encoding
62-
attributes = Dict{String,Union{String,Vector}}()
63+
attributes = Dict{String, Union{String, Vector}}()
6364
offsets = get_offsets_lazy(buffer, cdf.gdr.ADRhead, RecordSizeType)
65+
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
6466
for offset in offsets
6567
is_global(buffer, offset, RecordSizeType) && continue
6668
adr = ADR(buffer, offset, RecordSizeType)
6769
for head in (adr.AgrEDRhead, adr.AzEDRhead)
6870
head == 0 && continue
69-
found = _search_aedr_entries(buffer, head, RecordSizeType, cdf_encoding, varnum)
71+
found = _search_aedr_entries(buffer, head, RecordSizeType, needs_byte_swap, varnum)
7072
isnothing(found) && continue
7173
name = String(adr.Name)
7274
attributes[name] = _get_attributes(name, found, cdf)
@@ -99,13 +101,14 @@ function vattrib(cdf, varnum, name)
99101
# Search for the specific attribute by name first
100102
offsets = get_offsets_lazy(buffer, cdf.gdr.ADRhead, RecordSizeType)
101103
name_bytes = codeunits(name)
104+
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
102105
for offset in offsets
103106
is_global(buffer, offset, RecordSizeType) && continue
104107
adr = ADR(buffer, offset, RecordSizeType)
105108
adr.Name != name_bytes && continue
106109
for head in (adr.AgrEDRhead, adr.AzEDRhead)
107110
head == 0 && continue
108-
found = _search_aedr_entries(buffer, head, RecordSizeType, cdf_encoding, varnum)
111+
found = _search_aedr_entries(buffer, head, RecordSizeType, needs_byte_swap, varnum)
109112
isnothing(found) && continue
110113
return _get_attributes(name, found, cdf)
111114
end
@@ -114,15 +117,15 @@ function vattrib(cdf, varnum, name)
114117
return nothing
115118
end
116119

117-
@inline function _search_aedr_entries(source, aedr_head, RecordSizeType, cdf_encoding::Int32, target_varnum)
120+
@inline function _search_aedr_entries(source, aedr_head, RecordSizeType, needs_byte_swap, target_varnum)
118121
aedr_head == 0 && return nothing
119122
offset = Int(aedr_head)
120123
_num_offset = 13 + 2 * sizeof(RecordSizeType)
121124
_next_offset = 5 + sizeof(RecordSizeType)
122125
while offset != 0
123126
num = read_be(source, offset + _num_offset, Int32)
124127
if num == target_varnum
125-
return load_aedr_data(source, offset, RecordSizeType, cdf_encoding)
128+
return load_aedr_data(source, offset, RecordSizeType, needs_byte_swap)
126129
end
127130
offset = Int(read_be(source, offset + _next_offset, RecordSizeType))
128131
end
@@ -134,7 +137,7 @@ end
134137
135138
Return a list of attribute names in the CDF file.
136139
"""
137-
function attribnames(cdf::CDFDataset; predicate=is_global)
140+
function attribnames(cdf::CDFDataset; predicate = is_global)
138141
names = String[]
139142
buffer = cdf.buffer
140143
RecordSizeType = recordsize_type(cdf)

src/records/aedr.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct AEDR{FST, A}
2121
Value::A # This consists of the number of elements (specified by the NumElems field) of the data type (specified by the DataType field). This can be thought of as a 1-dimensional array of values (stored contiguously). The size of this field is the product of the number of elements and the size in bytes of each element.
2222
end
2323

24-
function load_aedr_data(buffer::Vector{UInt8}, offset, RecordSizeType, cdf_encoding)
24+
function load_aedr_data(buffer::Vector{UInt8}, offset, RecordSizeType, needs_byte_swap)
2525
_datatype_offset = 9 + 2 * sizeof(RecordSizeType)
2626
_numelems_offset = 17 + 2 * sizeof(RecordSizeType)
2727
_data_offset = 41 + 2 * sizeof(RecordSizeType)
@@ -31,7 +31,6 @@ function load_aedr_data(buffer::Vector{UInt8}, offset, RecordSizeType, cdf_encod
3131
return if datatype in (CDF_CHAR, CDF_UCHAR)
3232
load_char_data(buffer, offset + _data_offset, NumElems)
3333
else
34-
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
3534
load_attribute_data(T, buffer, offset + _data_offset, NumElems, needs_byte_swap)
3635
end
3736
end

0 commit comments

Comments
 (0)