Skip to content

GH-48977: [C++] Fix quadratic field name index construction on libc++ - #50970

Merged
pitrou merged 3 commits into
apache:mainfrom
advitrocks9:schema-name-index-quadratic
Aug 25, 2026
Merged

GH-48977: [C++] Fix quadratic field name index construction on libc++#50970
pitrou merged 3 commits into
apache:mainfrom
advitrocks9:schema-name-index-quadratic

Conversation

@advitrocks9

Copy link
Copy Markdown
Contributor

Rationale for this change

Building a schema with 500,000 identically named fields takes about 300 seconds.

CreateNameToIndexMap fills an unordered_multimap with one bare emplace per field. libc++ keeps equal keys contiguous, so each unhinted insert walks to the end of the equal range before splicing, making the loop quadratic. libstdc++ splices next to the first match and is already linear.

What changes are included in this PR?

emplace_hint(find(name), ...), the shape arvidjonasson worked out on the issue from the LLVM bug kou linked. reserve(fields.size()) goes in alongside to pay for the extra find. SchemaBuilder::Impl::AppendField had the same pattern, and Schema's copy constructor now rebuilds from fields_ instead of copying the multimap node by node.

Are these changes tested?

arrow-type-test passes 157/157. Nothing observable changes, so no new unit test. type_benchmark.cc gains a schema construction case: duplicate_names/10000 goes from 117 ms to 0.41 ms, distinct_names is level to 7% faster.

Are there any user-facing changes?

Fields sharing a name come back from GetAllFieldsByName in a different order. That order isn't a contract: GetAllFieldIndices sorts, both pyarrow callers reject more than one field, and the two standard libraries already disagree.

Copilot AI lite review requested due to automatic review settings August 24, 2026 15:13
@advitrocks9
advitrocks9 requested a review from pitrou as a code owner August 24, 2026 15:13
@github-actions github-actions Bot added the awaiting review Awaiting review label Aug 24, 2026
@github-actions

Copy link
Copy Markdown

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

Copilot AI left a comment

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.

Pull request overview

Optimizes schema field-name index construction on libc++ to avoid quadratic behavior with duplicate names.

Changes:

  • Uses reserved, hinted multimap insertion.
  • Optimizes schema copying and SchemaBuilder.
  • Adds distinct- and duplicate-name benchmarks.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
cpp/src/arrow/type.cc Optimizes name-index construction and schema copying.
cpp/src/arrow/type_benchmark.cc Adds schema construction benchmarks.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cpp/src/arrow/type.cc
Comment thread cpp/src/arrow/type.cc
Copilot AI review requested due to automatic review settings August 25, 2026 04:45

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 25, 2026
@advitrocks9

Copy link
Copy Markdown
Contributor Author

@pitrou good to merge? I believe the 2 failures are unrelated known issues

@pitrou

pitrou commented Aug 25, 2026

Copy link
Copy Markdown
Member

You're right, those are unrelated. Thank you @advitrocks9 !

@pitrou
pitrou merged commit 6b419a1 into apache:main Aug 25, 2026
64 of 66 checks passed
@pitrou pitrou removed the awaiting committer review Awaiting committer review label Aug 25, 2026
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