Skip to content

GH-40316: [Python] only allocate the ScalarMemoTable when used - #40565

Merged
pitrou merged 2 commits into
apache:mainfrom
anjakefala:kef/40316
Mar 20, 2024
Merged

pitrou merged 2 commits into
apache:mainfrom
anjakefala:kef/40316

Conversation

@anjakefala

@anjakefala anjakefala commented Mar 14, 2024 •

Copy link
Copy Markdown
Contributor

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=False

After 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_table and unique_values have been moved underneath an if (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_objects has extensive existing tests:

_assert_nunique(arr.to_pandas(deduplicate_objects=False), len(arr))
and
def time_to_pandas_no_dedup(self, *args):

Are there any user-facing changes?

Nope.

@jorisvandenbossche jorisvandenbossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No C++ expert, but this looks good to me, thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::function will add a layer of indirection. Did you try to measure performance to see if that matters?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, better, just keep the existing WrapMemoized and WrapUnmemoized.

@anjakefala anjakefala Mar 19, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I can try to do something.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it should be better now!

@pitrou

pitrou commented Mar 19, 2024

Copy link
Copy Markdown
Member

@anjakefala Can you check this still solves the memory consumption problem?

@anjakefala

Copy link
Copy Markdown
Contributor Author

@pitrou Yes, indeed it does!

I looked at my original code that was resulting in freezing at the RETURN_NOT_OK(memo_table.GetOrInsert(value, &memo_index)); line.

18 inline Status ConvertAsPyObjects(const PandasOptions& options, const ChunkedArray& data,
  619                                  WrapFunction&& wrap_func, PyObject** out_values) {
  620   using ArrayType = typename TypeTraits<Type>::ArrayType;
  621   using Scalar = typename MemoizationTraits<Type>::Scalar;
  622   std::function<Status(const typename MemoizationTraits<Type>::Scalar&, PyObject**)> WrapFunc;
  623
  624   if (options.deduplicate_objects) {
  626     ::arrow::internal::ScalarMemoTable<Scalar> memo_table(options.pool);
  627     std::vector<PyObject*> unique_values;
  628     int32_t memo_size = 0;
  630
  631     WrapFunc = [&](const Scalar& value, PyObject** out_values) {
  633       int32_t memo_index;
  634       RETURN_NOT_OK(memo_table.GetOrInsert(value, &memo_index));
  636       if (memo_index == memo_size) {
  637         // New entry
  639         RETURN_NOT_OK(wrap_func(value, out_values));
  640         unique_values.push_back(*out_values);
  641         ++memo_size;
  642       } else {
  644         // Duplicate entry
  645         Py_INCREF(unique_values[memo_index]);
  646         *out_values = unique_values[memo_index];
  647       }
  648       return Status::OK();
  649     };
  650   } else {
  651     WrapFunc = [&](const Scalar& value, PyObject** out_values) {
  652       return wrap_func(value, out_values);
  653     };
  654   }
  655
  656   for (int c = 0; c < data.num_chunks(); c++) {
  658     const auto& arr = arrow::internal::checked_cast<const ArrayType&>(*data.chunk(c));
  660     RETURN_NOT_OK(internal::WriteArrayObjects(arr, WrapFunc, out_values));
  662     out_values += arr.length();
  663   }
  664   return Status::OK();
  665 }

It seems that the main difference is your creation of convert_chunks, and moving the for loop within it. Do you know why that helped fix the freezing?

Anyway, this works great for my needs! Thank you!

@pitrou

pitrou commented Mar 20, 2024

Copy link
Copy Markdown
Member

In your case, the outer variables referenced by WrapFunc (memo_table, unique_values...) are destroyed just after WrapFunc is defined and before it is actually called. So calling WrapFunc references destroyed objects, which leads to misbehaviour.

@anjakefala

Copy link
Copy Markdown
Contributor Author

Got it, and they are not destroyed in your case, because convert_chunks is called within the if/else statement. That was the better approach to fixing my code.

Thanks @pitrou! Testing looks good on my end.

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@conbench-apache-arrow

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants