-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SAS7BDAT parser: Fast byteswap #47403
Conversation
pandas/io/sas/sas.pyx
Outdated
uint8_t, | ||
uint16_t, | ||
uint32_t, | ||
uint64_t, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, are these interchangeable with the versions of these we cimport from numpy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I didn't know those exist.
size_t
and friends:
uint64_t
and friends:
https://github.com/cython/cython/blob/f753deecd09e011a1bc276b78ccc0f1c0ad67f09/Cython/Includes/numpy/__init__.pxd#L746 -> https://github.com/cython/cython/blob/f753deecd09e011a1bc276b78ccc0f1c0ad67f09/Cython/Includes/numpy/__init__.pxd#L325
So this looks identical in both cases, but I'm happy to import from NumPy if that's preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't make a huge difference. on the one hand itd be nice to avoid dependency on numpy when possible, on the other im inevitably going to forget and ask again in 6 months if we dont use the numpy versions
pandas/io/sas/sas.pyx
Outdated
|
||
cdef inline float _byteswap_float(float num): | ||
cdef uint32_t answer = 0 | ||
memcpy(&answer, &num, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use sizeof instead of hard coding the size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a verbatim copy from ReadStat, do you still want me to make that modification?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok. I think fine to keep as is then
One thing I realized is that we could also use NumPy's byteswapping, at the cost of around 10% performance relative to this impl. |
How much of this could be replaced with that? |
Everything copied from readstat so ~ 50 lines of Cython code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asv's that cover this case?
can you add a whatsnew note
pandas/io/sas/sas.pyx
Outdated
@@ -433,3 +439,73 @@ cdef class Parser: | |||
self.current_row_on_page_index += 1 | |||
self.current_row_in_chunk_index += 1 | |||
self.current_row_in_file_index += 1 | |||
|
|||
|
|||
def read_float_with_byteswap(const uint8_t *data, bint byteswap): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add some comments here on what and why you are doing this.
ASV from #47405:
|
Test failure seems unrelated |
I'm trying an intrinsics-based version: Fewer SLOC and faster. |
Personally I think it should be useful. Probably just one parametrized to test the function for every type is enough. |
The release note in the other PR is not accurate now. The changes in this PR won't be available until pandas 1.6/2.0, while for what you mention the release note in the other PR mentions this issue/PR for 1.5. Probably no big deal, but if you want to open a small PR to update that note in the releases to only include the issues/PRs that have already been merged, that would leave things more accurate (ping me in that PR, so I backport it to 1.5). And then you can add another release note for 1.6 here. |
Or we merge the PRs noted in those release notes to 1.5? The PRs have been ready for review with no code changes for a long time. |
We already released pandas 1.5 release candidate, and we are only backporting regressions to it, not new features, performance improvements. It's also unclear to me this will be merged before the release. I know this has been forgotten for a long time, sorry about that. But that's unfortunately part of how open source development works in a project like pandas. |
I added tests using Hypothesis and moved the byteswapping code to a module. The byteswapping code also be moved somewhere else outside the SAS stuff. |
Updated release notes. I took the liberty of including #47656 already. |
Thanks @jonashaag |
* Fast byteswap * Add types * Review feedback * Slightly faster variant (1 less bytes obj construction) * Make MyPy happy? * Update sas7bdat.py * Use intrinsics * Lint * Add tests + move byteswap to module * Add float tests + refactoring * Undo unrelated changes * Undo unrelated changes * Lint * Update v1.6.0.rst * read_int -> read_uint * Lint * Update sas7bdat.py
Speed up SAS7BDAT int/float reading.
This is order of magnitude faster than using
struct.unpack(fmt, data)
orprecompiled_unpacker = struct.Struct(fmt).unpack; ...; precompiled_unpacker(data)
.Unfortunately Python does not expose a low-level interface to
struct
or a byteswapping interface. The byteswap implementation in this change is frompyreadstat
.Today this brings a modest 10-20% performance improvement. But together with the other changes I will be proposing it will be a major bottleneck.
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.