Skip to content

Commit d8fb6c3

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

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/loading/attribute.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919
2020
Load all attributes from the CDF file.
2121
"""
22-
function attrib(cdf::CDFDataset; predicate=is_global)
22+
function attrib(cdf::CDFDataset; predicate = is_global)
2323
RecordSizeType = recordsize_type(cdf)
2424
buffer = cdf.buffer
2525
cdf_encoding = cdf.cdr.encoding
@@ -59,14 +59,15 @@ function vattrib(cdf::CDFDataset, varnum::Integer)
5959
RecordSizeType = recordsize_type(cdf)
6060
buffer = cdf.buffer
6161
cdf_encoding = cdf.cdr.encoding
62-
attributes = Dict{String,Union{String,Vector}}()
62+
attributes = Dict{String, Union{String, Vector}}()
6363
offsets = get_offsets_lazy(buffer, cdf.gdr.ADRhead, RecordSizeType)
64+
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
6465
for offset in offsets
6566
is_global(buffer, offset, RecordSizeType) && continue
6667
adr = ADR(buffer, offset, RecordSizeType)
6768
for head in (adr.AgrEDRhead, adr.AzEDRhead)
6869
head == 0 && continue
69-
found = _search_aedr_entries(buffer, head, RecordSizeType, cdf_encoding, varnum)
70+
found = _search_aedr_entries(buffer, head, RecordSizeType, needs_byte_swap, varnum)
7071
isnothing(found) && continue
7172
name = String(adr.Name)
7273
attributes[name] = _get_attributes(name, found, cdf)
@@ -99,13 +100,14 @@ function vattrib(cdf, varnum, name)
99100
# Search for the specific attribute by name first
100101
offsets = get_offsets_lazy(buffer, cdf.gdr.ADRhead, RecordSizeType)
101102
name_bytes = codeunits(name)
103+
needs_byte_swap = is_big_endian_encoding(cdf_encoding)
102104
for offset in offsets
103105
is_global(buffer, offset, RecordSizeType) && continue
104106
adr = ADR(buffer, offset, RecordSizeType)
105107
adr.Name != name_bytes && continue
106108
for head in (adr.AgrEDRhead, adr.AzEDRhead)
107109
head == 0 && continue
108-
found = _search_aedr_entries(buffer, head, RecordSizeType, cdf_encoding, varnum)
110+
found = _search_aedr_entries(buffer, head, RecordSizeType, needs_byte_swap, varnum)
109111
isnothing(found) && continue
110112
return _get_attributes(name, found, cdf)
111113
end
@@ -114,15 +116,15 @@ function vattrib(cdf, varnum, name)
114116
return nothing
115117
end
116118

117-
@inline function _search_aedr_entries(source, aedr_head, RecordSizeType, cdf_encoding::Int32, target_varnum)
119+
@inline function _search_aedr_entries(source, aedr_head, RecordSizeType, needs_byte_swap, target_varnum)
118120
aedr_head == 0 && return nothing
119121
offset = Int(aedr_head)
120122
_num_offset = 13 + 2 * sizeof(RecordSizeType)
121123
_next_offset = 5 + sizeof(RecordSizeType)
122124
while offset != 0
123125
num = read_be(source, offset + _num_offset, Int32)
124126
if num == target_varnum
125-
return load_aedr_data(source, offset, RecordSizeType, cdf_encoding)
127+
return load_aedr_data(source, offset, RecordSizeType, needs_byte_swap)
126128
end
127129
offset = Int(read_be(source, offset + _next_offset, RecordSizeType))
128130
end
@@ -134,7 +136,7 @@ end
134136
135137
Return a list of attribute names in the CDF file.
136138
"""
137-
function attribnames(cdf::CDFDataset; predicate=is_global)
139+
function attribnames(cdf::CDFDataset; predicate = is_global)
138140
names = String[]
139141
buffer = cdf.buffer
140142
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)