Skip to content

GH-41771: [C++] Iterator releases its resource immediately when it reads all values - #41824

Merged
bkietz merged 1 commit into
apache:mainfrom
kou:cpp-dataset-scanner-reader
May 28, 2024
Merged

bkietz merged 1 commit into
apache:mainfrom
kou:cpp-dataset-scanner-reader

Conversation

@kou

@kou kou commented May 25, 2024 •

Copy link
Copy Markdown
Member

Rationale for this change

Iterator keeps its resource (ptr_) until it's deleted but we can release its resource immediately when it reads all values. If Iterator keeps its resource until it's deleted, it may block closing a file. See GH-41771 for this case.

What changes are included in this PR?

Releases ptr_ when Next() returns the end.

Are these changes tested?

Yes.

Are there any user-facing changes?

Yes.

…l values

Iterator keeps its resource (ptr_) until it's deleted but we can
release its resource immediately when it reads all values. If Iterator
keeps its resource until it's deleted, it may block closing file. See
apacheGH-41771 for this case.
@kou
kou requested a review from bkietz May 25, 2024 03:50
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #41771 has been automatically assigned in GitHub to PR creator.

@bkietz bkietz 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

Result<T> Next() {
if (ptr_) {
auto next_result = next_(ptr_.get());
if (next_result.ok() && IsIterationEnd(next_result.ValueUnsafe())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@kou @bkietz This extra check and release should be the responsibility of the underlying iterator instead of forcing every abstract iterator to behave this way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Could you explain the reason? Performance?

For the #41771 case:

This is the underlying iterator:

Result<TaggedRecordBatchIterator> AsyncScanner::ScanBatches() {
return ::arrow::internal::IterateSynchronously<TaggedRecordBatch>(
[this](::arrow::internal::Executor* executor) {
return ScanBatchesAsync(executor);
},
scan_options_->use_threads);
}

We want to release the IPC reader created at:

auto open_reader = OpenReaderAsync(source);

It's referred by the underlying iterator indirectly via Future/std::function.

It seems that we can't remove a reference from Future/std::function without deleting Future/std::function and we can't delete Future/std::function without deleting the underlying iterator.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you explain the reason? Performance?

Performance, binary code size, and overall elegance of the iterator tree.

But I see that it's not possible because these iterators are like C++ in that the iterator itself is the value as well instead of being something that produces the value. So just ignore my comment.

@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 e6e00e7.

There were 6 benchmark results indicating a performance regression:

The full Conbench report has more details. It also includes information about 102 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