Skip to content

Commit 9aba948

Browse files
remove chunkbuffer as variable
1 parent 7c9a3f7 commit 9aba948

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

Sources/OmFileFormatSwift/OmFileFormat.swift

+2-13
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,11 @@ public final class OmFileReader<Backend: OmFileReaderBackend> {
575575
/// Future implementations could use async io via lib uring
576576
///
577577
/// `into` is a 2d flat array with `arrayDim1Length` count elements in the fast dimension
578-
/// `chunkBuffer` is used to temporary decompress chunks of data
579578
/// `arrayDim1Range` defines the offset in dimension 1 what is applied to the read into array
580579
/// `arrayDim1Length` if dim0Slow.count is greater than 1, the arrayDim1Length will be used as a stride. Like `nTime` in a 2d fast time array
581580
/// `dim0Slow` the slow dimension to read. Typically a location range
582581
/// `dim1Read` the fast dimension to read. Typical a time range
583-
public func read(into: UnsafeMutablePointer<Float>, arrayDim1Range: Range<Int>, arrayDim1Length: Int, chunkBuffer: UnsafeMutableRawPointer, dim0Slow dim0Read: Range<Int>, dim1 dim1Read: Range<Int>) throws {
582+
public func read(into: UnsafeMutablePointer<Float>, arrayDim1Range: Range<Int>, arrayDim1Length: Int, dim0Slow dim0Read: Range<Int>, dim1 dim1Read: Range<Int>) throws {
584583

585584
//assert(arrayDim1Range.count == dim1Read.count)
586585

@@ -622,17 +621,7 @@ public final class OmFileReader<Backend: OmFileReaderBackend> {
622621
guard error == ERROR_OK else {
623622
throw OmFileFormatSwiftError.omDecoder(error: String(cString: om_error_string(error)))
624623
}
625-
try reader.fn.decode(decoder: &decoder, into: into, chunkBuffer: chunkBuffer)
626-
}
627-
}
628-
629-
/// Read data into existing output float buffer. Allocate buffer on demand
630-
public func read(into: UnsafeMutablePointer<Float>, arrayDim1Range: Range<Int>, arrayDim1Length: Int, dim0Slow dim0Read: Range<Int>, dim1 dim1Read: Range<Int>) throws {
631-
//assert(arrayDim1Range.count == dim1Read.count)
632-
let bufferSize = P4NDEC256_BOUND(n: chunk0*chunk1, bytesPerElement: compression.bytesPerElement)
633-
try withUnsafeTemporaryAllocation(byteCount: bufferSize, alignment: 4) { buffer in
634-
try read(into: into, arrayDim1Range: arrayDim1Range, arrayDim1Length: arrayDim1Length, chunkBuffer: buffer.baseAddress!, dim0Slow: dim0Read, dim1: dim1Read)
635-
624+
try reader.fn.decode(decoder: &decoder, into: into)
636625
}
637626
}
638627

Sources/OmFileFormatSwift/OmFileReader2.swift

+23-22
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ public struct OmFileReader2Array<Backend: OmFileReaderBackend, OmType: OmFileArr
219219
guard error == ERROR_OK else {
220220
throw OmFileFormatSwiftError.omDecoder(error: String(cString: om_error_string(error)))
221221
}
222-
let chunkBufferSize = om_decoder_read_buffer_size(&decoder)
223-
let chunkBuffer = UnsafeMutableRawBufferPointer.allocate(byteCount: Int(chunkBufferSize), alignment: 1)
224-
try fn.decode(decoder: &decoder, into: into, chunkBuffer: chunkBuffer.baseAddress!)
225-
chunkBuffer.deallocate()
222+
try fn.decode(decoder: &decoder, into: into)
226223
})
227224
})
228225
})
@@ -267,30 +264,34 @@ public struct OmFileReader2Array<Backend: OmFileReaderBackend, OmType: OmFileArr
267264

268265
extension OmFileReaderBackend {
269266
/// Read and decode
270-
func decode(decoder: UnsafePointer<OmDecoder_t>, into: UnsafeMutableRawPointer, chunkBuffer: UnsafeMutableRawPointer) throws {
267+
func decode(decoder: UnsafePointer<OmDecoder_t>, into: UnsafeMutableRawPointer) throws {
271268
var indexRead = OmDecoder_indexRead_t()
272269
om_decoder_init_index_read(decoder, &indexRead)
273270

274-
/// Loop over index blocks and read index data
275-
while om_decoder_next_index_read(decoder, &indexRead) {
276-
//print("Read index \(indexRead)")
277-
let indexData = self.getData(offset: Int(indexRead.offset), count: Int(indexRead.count))
278-
279-
var dataRead = OmDecoder_dataRead_t()
280-
om_decoder_init_data_read(&dataRead, &indexRead)
281-
282-
var error: OmError_t = ERROR_OK
283-
/// Loop over data blocks and read compressed data chunks
284-
while om_decoder_next_data_read(decoder, &dataRead, indexData, indexRead.count, &error) {
285-
//print("Read data \(dataRead) for chunk index \(dataRead.chunkIndex)")
286-
let dataData = self.getData(offset: Int(dataRead.offset), count: Int(dataRead.count))
287-
guard om_decoder_decode_chunks(decoder, dataRead.chunkIndex, dataData, dataRead.count, into, chunkBuffer, &error) else {
271+
/// The size to decode a single chunk
272+
let bufferSize = om_decoder_read_buffer_size(decoder)
273+
try withUnsafeTemporaryAllocation(byteCount: Int(bufferSize), alignment: 1) { buffer in
274+
/// Loop over index blocks and read index data
275+
while om_decoder_next_index_read(decoder, &indexRead) {
276+
//print("Read index \(indexRead)")
277+
let indexData = self.getData(offset: Int(indexRead.offset), count: Int(indexRead.count))
278+
279+
var dataRead = OmDecoder_dataRead_t()
280+
om_decoder_init_data_read(&dataRead, &indexRead)
281+
282+
var error: OmError_t = ERROR_OK
283+
/// Loop over data blocks and read compressed data chunks
284+
while om_decoder_next_data_read(decoder, &dataRead, indexData, indexRead.count, &error) {
285+
//print("Read data \(dataRead) for chunk index \(dataRead.chunkIndex)")
286+
let dataData = self.getData(offset: Int(dataRead.offset), count: Int(dataRead.count))
287+
guard om_decoder_decode_chunks(decoder, dataRead.chunkIndex, dataData, dataRead.count, into, buffer.baseAddress, &error) else {
288+
throw OmFileFormatSwiftError.omDecoder(error: String(cString: om_error_string(error)))
289+
}
290+
}
291+
guard error == ERROR_OK else {
288292
throw OmFileFormatSwiftError.omDecoder(error: String(cString: om_error_string(error)))
289293
}
290294
}
291-
guard error == ERROR_OK else {
292-
throw OmFileFormatSwiftError.omDecoder(error: String(cString: om_error_string(error)))
293-
}
294295
}
295296
}
296297

0 commit comments

Comments
 (0)