Precompute little-endian sample dtype once per stream#170
Merged
Conversation
The little-endian dtype view used by np.frombuffer in _read_chunk3 was
recomputed once per sample via s.dtype.newbyteorder('<'). Compute it once
in StreamData.__init__ (s.dtype_le) and reuse it. XDF sample data is
little-endian, so the result is byte-identical; loading a 553k-sample,
99-channel EEG recording is ~8% faster.
Contributor
|
Very nice, thanks @sappelhoff! |
Contributor
Author
|
Sure! Could we perhaps get a release with this? 🙏 the speedup is modest but noticeable :) |
Contributor
|
Yes, I will try to do it today or tomorrow at the latest. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE I generated this PR with the help of Claude as part of a general memory/speed profiling I am doing for a private package of mine.
What
_read_chunk3reinterprets each numeric sample buffer as little-endian vianp.frombuffer(raw, dtype=s.dtype.newbyteorder("<"), count=s.nchns).s.dtype.newbyteorder("<")returns the same dtype object every time but was being recomputed once per sample. This moves it toStreamData.__init__(s.dtype_le, alongside the existingdtype/samplebytespre-calcs) and reuses it.Why it's behaviour-preserving
"<"regardless of platform —s.dtype_le = s.dtype.newbyteorder("<")is the identical value, so output is byte-identical (including on big-endian hosts).dtype_leis set under the sameif self.fmt != "string":guard that already gatesself.dtype, and its only use is the numeric branch of_read_chunk3. String streams never set or read it. Empty streams never call_read_chunk3.newbyteorderis dtype-generic (no type-specific branch), so all numeric formats behave identically.The
.pxdCython declaration is updated to keepStreamDatain sync.Verification
example-files/cloned), covering int16/int32/float32/double64/string/empty streams — every sample-reading path.time_series/time_stampsmd5 on a real 224 MB recording (99-ch EEG @ 500 Hz + marker/logging/notes/misc streams), baseline vs this branch.load_xdfon that recording (median 1.014 s → 0.935 s, 6 reps), as the per-samplenewbyteorder(553k calls) is eliminated.