Repository navigation
GH-40316: [Python] only allocate the ScalarMemoTable when used - #40565
Conversation
1e90930 to
9705e27
Compare
jorisvandenbossche
left a comment
There was a problem hiding this comment.
No C++ expert, but this looks good to me, thanks!
There was a problem hiding this comment.
std::function will add a layer of indirection. Did you try to measure performance to see if that matters?
There was a problem hiding this comment.
Or, better, just keep the existing WrapMemoized and WrapUnmemoized.
There was a problem hiding this comment.
I had a lot of challenges with building and/or running the program without the shared pointers. Without them, the lambda function at run-time or build-time declared things were out of scope or not declared. I can share with you what I had previously, and how it froze when I ran it. You are much more experienced with C++ than I am and you might have a better idea how to approach it.
If I keep the WrapMemoized and WrapUnmemoized, then I would need an additional if statement later on, but maybe that is fine.
There was a problem hiding this comment.
Well, I can try to do something.
|
@anjakefala Can you check this still solves the memory consumption problem? |
|
@pitrou Yes, indeed it does! I looked at my original code that was resulting in freezing at the It seems that the main difference is your creation of Anyway, this works great for my needs! Thank you! |
|
In your case, the outer variables referenced by |
|
Got it, and they are not destroyed in your case, because Thanks @pitrou! Testing looks good on my end. |
|
After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit b6c4fbc. There was 1 benchmark result indicating a performance regression:
The full Conbench report has more details. It also includes information about 2 possible false positives for unstable benchmarks that are known to sometimes produce them. |
Rationale for this change
Mimalloc and jemalloc can allocate a relatively large amount of memory for the ScalarMemoTable. For this reason, the ScalarMemoTable should only be allocated when it is used (when
options.deduplicate_objects=True).I tested this change, and for small tables it does improve memory allocation.
options.deduplicate_objects=FalseAfter this change:
📦 Total memory allocated:
174.422MB
📊 Histogram of allocation size:
min: 1.000B
--------------------------------------------
< 6.000B : 3064 ▇▇
< 36.000B : 7533 ▇▇▇▇
< 222.000B : 9974 ▇▇▇▇▇
< 1.319KB : 53264 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
< 7.999KB : 5188 ▇▇▇
< 48.503KB : 742 ▇
< 294.066KB: 102 ▇
< 1.741MB : 22 ▇
< 10.556MB : 1 ▇
<=64.000MB : 1 ▇
--------------------------------------------
max: 64.000MB
Before this change:
📦 Total memory allocated:
1.295GB
📊 Histogram of allocation size:
min: 1.000B
--------------------------------------------
< 6.000B : 3064 ▇▇
< 36.000B : 7543 ▇▇▇▇
< 222.000B : 10009 ▇▇▇▇▇
< 1.319KB : 53269 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
< 7.999KB : 5192 ▇▇▇
< 48.503KB : 761 ▇
< 294.066KB: 102 ▇
< 1.741MB : 22 ▇
< 10.556MB : 1 ▇
<=64.000MB : 19 ▇
--------------------------------------------
max: 64.000MB
What changes are included in this PR?
The allocation of
memo_tableandunique_valueshave been moved underneath anif (options.deduplicate_objects)block. Since they are used within a lambda, they have been changed to shared pointers, so that their values exist for the lifetime needed.Are these changes tested?
deduplicate_objectshas extensive existing tests:arrow/python/pyarrow/tests/test_pandas.py
Line 3211 in b235f83
arrow/python/benchmarks/convert_pandas.py
Line 71 in b235f83
Are there any user-facing changes?
Nope.