-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat(python): Reduce scan_csv() (and friends') memory usage when using BytesIO #20649
base: main
Are you sure you want to change the base?
feat(python): Reduce scan_csv() (and friends') memory usage when using BytesIO #20649
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #20649 +/- ##
==========================================
+ Coverage 79.02% 79.86% +0.84%
==========================================
Files 1557 1557
Lines 220551 220752 +201
Branches 2514 2527 +13
==========================================
+ Hits 174283 176312 +2029
+ Misses 45695 43862 -1833
- Partials 573 578 +5 ☔ View full report in Codecov by Sentry. |
I was a bit worried that this would break if you changed the backing buffer afterwards. But it seems to not do that, so very nice work 😄 |
Pretty sure previously, if StringIO had reached this point (which it wouldn't in practice) you'd end up trying to load a _path_ matching the StringIO's contents...
let Ok(bytes) = py_f.call_method0("getvalue") else { | ||
return py_f; | ||
}; | ||
if bytes.downcast::<PyBytes>().is_ok() || bytes.downcast::<PyString>().is_ok() { |
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.
I am fairly certain the StringIO path wasn't hit in existing usage of this function, and it's wrong because the caller expects strings to be paths... and doesn't match the function name, either.
memory_usage = memory_usage_without_pyarrow | ||
|
||
# Create CSV that is ~70-85 MB in size: | ||
f = io.BytesIO() |
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 we test this with a little bit less data. I assume it is noticeable with less.
When using
scan_csv()
on aBytesIO()
, the hope is that it won't make a whole copy of the data if possible, e.g. by doing filtering as it reads. In fact, it currently does make a copy, making memory usage that much higher.This PR fixes that, by using
BytesIO.getvalue()
instead ofBytesIO.read()
. I assume this will also help otherscan_*
functions since it's just generic infrastructure code.To demonstrate, below is a standalone version of the test in the PR. When run with released 1.19:
When run with this branch:
Here's the script: