Skip to content
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

Remove Clone and copy source structs internally #1449

Merged
merged 9 commits into from
Mar 19, 2022

Conversation

viirya
Copy link
Member

@viirya viirya commented Mar 16, 2022

Which issue does this PR close?

Closes #1425.

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 16, 2022
let array_mut = array as *mut FFI_ArrowArray;
let schema_mut = schema as *mut FFI_ArrowSchema;

let array_data = std::ptr::replace(array_mut, FFI_ArrowArray::empty());
Copy link
Member

Choose a reason for hiding this comment

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

Actually after thinking more on this, it seems this won't address the original problem neither. It basically just calls drop on FFI_ArrowArray (which is empty), but doesn't free the memory pointed by array and schema.

  +-------+
  | array |
  +-------+             +----------------------------+
     |                  |                            |
     +----------------->|      FFI_ArrowArray        | <- memory leaked
                        |                            |
                        +----------------------------+

For instance, if array and schema are from Arc::into_raw, then the memory allocated for the Arc will become dangling after this, and thus memory leak.

I'm thinking whether we'll need two APIs, one where we are able to take the ownership of the memory allocated for the array and schema (e.g., exported by Arc::into_raw from Rust itself), and one where we cannot take the ownership (e.g., memory was allocated by other languages such as Java), and thus requires the exporter to free the memory by itself later.

For the latter, we can clone the content for FFI_ArrowArray and FFI_ArrowSchema, and set the content of the original array and schema to be FFI_ArrowArray::empty() and FFI_ArrowSchema::empty() so that the exporter can just safely free the memory later.

Copy link
Contributor

Choose a reason for hiding this comment

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

For instance, if array and schema are from Arc::into_raw, then the memory allocated for the Arc will become dangling after this, and thus memory leak.

Currently if user try to export using into_raw and and don't import using from_raw (we can assume it's a normal case? as they export data to be used by others they don't need to import again), they might have memory leak.

After check the CPP-import implementation, I think this change is fine. We even can remove the two drop_in_place call as it seems unnecessary.

What we need is to redesign the ArrowArray::into_raw(), we can't use Arc::into_raw in the implementation

Copy link
Member

Choose a reason for hiding this comment

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

Yea the drop_in_place here seems unnecessary.

I'm not sure if it's possible to redesign ArrowArray::into_raw though, since after exporting the array, we need to free up the memory allocated for FFI_ArrowArray. However this can only be done after the exported array is imported via FFI_ArrowArray::try_from_raw, which we don't know when.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is correct that with one single API, we cannot deal with both cases: raw pointers from Arc and not from Arc.

I'm not sure two separate APIs is good. With a single API, we can ask users to take care of releasing the raw pointers (either Arc or not) by themselves.

Copy link
Contributor

Choose a reason for hiding this comment

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

However this can only be done after the exported array is imported via FFI_ArrowArray::try_from_raw, which we don't know when.

  1. It may not be imported in rust via FFI_ArrowArray::try_from_raw, it can be imported by other language sdk
  2. We don't need to know, the user should import it somewhere or free them if needed, that's why we can't use Arc::into_raw because we don't know how user might use them. This API should be fired and done, shouldn't expect user always do something like try_from_raw

Copy link
Member Author

Choose a reason for hiding this comment

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

another one allowing importer to allocate memory for exporter

If Rust side is importer, we already have it as we can do it now by creating empty structs, passing raw pointers to exporter.

If Java side is importer, we may need an export API which takes raw pointers from Java and replaces its content.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, agreed. Do you plan to add the export API in this PR, or separately?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should be straightforward to add, let me add it here. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a new API export_into_raw. Please check it. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to catch up with these discussions since I'll soon have a need to create Buffers from foreign memory. The cast to *mut followed by std::ptr::replace here doesn't look safe to me. When the pointer is coming from an Arc that seems to violate rust's unique ownership rules.

@codecov-commenter
Copy link

codecov-commenter commented Mar 16, 2022

Codecov Report

Merging #1449 (7863a6c) into master (717216f) will increase coverage by 0.03%.
The diff coverage is 86.33%.

❗ Current head 7863a6c differs from pull request most recent head 888cbe0. Consider uploading reports for the commit 888cbe0 to get more accurate results

@@            Coverage Diff             @@
##           master    #1449      +/-   ##
==========================================
+ Coverage   82.67%   82.71%   +0.03%     
==========================================
  Files         185      187       +2     
  Lines       53866    54175     +309     
==========================================
+ Hits        44535    44811     +276     
- Misses       9331     9364      +33     
Impacted Files Coverage Δ
arrow/src/array/array_list.rs 95.52% <ø> (ø)
arrow/src/array/mod.rs 100.00% <ø> (ø)
integration-testing/src/lib.rs 0.00% <0.00%> (ø)
arrow/src/array/data.rs 82.95% <58.33%> (-0.21%) ⬇️
arrow/src/array/transform/fixed_size_list.rs 73.68% <73.68%> (ø)
arrow/src/array/transform/union.rs 75.00% <75.00%> (ø)
arrow/src/array/transform/mod.rs 86.46% <84.21%> (+0.03%) ⬆️
arrow/src/array/array.rs 86.92% <100.00%> (+1.51%) ⬆️
arrow/src/compute/kernels/cast.rs 95.59% <100.00%> (+0.03%) ⬆️
arrow/src/compute/kernels/filter.rs 88.00% <100.00%> (+2.59%) ⬆️
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 717216f...888cbe0. Read the comment docs.

arrow/src/ffi.rs Outdated
@@ -802,6 +810,41 @@ impl ArrowArray {
pub fn into_raw(this: ArrowArray) -> (*const FFI_ArrowArray, *const FFI_ArrowSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So should we delete this API, as this no longer a pair with the try_from_raw() method.

If we leave it here, the only way to avoid memory leak is user use Arc::from_raw() 2 times by themselves

Copy link
Member Author

Choose a reason for hiding this comment

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

I tend not to delete it as its usage is different. We actually use this API internally and we manage the Arc pointers manually. This save us a round trip from Java.

@wangfenjin
Copy link
Contributor

Good work, seems this is the minimum change we can make this right

@viirya
Copy link
Member Author

viirya commented Mar 18, 2022

@sunchao @alamb Do we need to include this into 11.0.0? Or wait for next release?

@sunchao
Copy link
Member

sunchao commented Mar 18, 2022

It'd be nice if we can include this in 11.0.0 and 10.0.1, although 11.0.0 RC1 is already under voting now.

arrow/src/ffi.rs Outdated
@@ -802,6 +810,35 @@ impl ArrowArray {
pub fn into_raw(this: ArrowArray) -> (*const FFI_ArrowArray, *const FFI_ArrowSchema) {
(Arc::into_raw(this.array), Arc::into_raw(this.schema))
}

/// exports [ArrowArray] to raw pointers of the C Data Interface provided by the consumer.
Copy link
Member

Choose a reason for hiding this comment

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

nit: "Exports"

arrow/src/ffi.rs Outdated
/// this [ArrowArray] to the location pointed by the raw pointers. Usually the raw pointers are
/// provided by the array data consumer.
pub unsafe fn export_into_raw(
this: ArrowArray,
Copy link
Member

Choose a reason for hiding this comment

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

Can we have this is src/array/array.rs and have it to be a top-level function, paired to make_array_from_raw?

The signature can be something:

pub unsafe fn export_array_into_raw(
  src: &ArrayRef,
  out_array: *mut FFI_ArrowArray,
  out_schema: *mut FFI_ArrowSchema)

It's better to also update the usage doc at the top of this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved. Please take another look. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, I changed src: &ArrayRef to src: ArrayRef. It is already a Arc, seems not necessary to take borrowed type on it.

arrow/src/array/array.rs Outdated Show resolved Hide resolved
Copy link
Member

@sunchao sunchao left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @viirya !

@viirya
Copy link
Member Author

viirya commented Mar 18, 2022

Thanks @sunchao @wangfenjin for the detailed review!

@sunchao sunchao merged commit d02425d into apache:master Mar 19, 2022
@sunchao
Copy link
Member

sunchao commented Mar 19, 2022

Merged, thanks!

@viirya
Copy link
Member Author

viirya commented Mar 19, 2022

Thanks @sunchao

@alamb
Copy link
Contributor

alamb commented Mar 22, 2022

Since we'll release a new arrow version in ~2 weeks, this will be included there. Hopefully we can avoid API changes and release it as 11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FFI: ArrowArray::try_from_raw shouldn't clone
6 participants