Skip to content

GH-48057: [R] Slow reading performance caused by apply_arrow_r_metadata() looping through all columns, including NULL ones - #48104

Merged
thisisnic merged 3 commits into
apache:mainfrom
thisisnic:GH-48057_metdata
Nov 13, 2025
Merged

thisisnic merged 3 commits into
apache:mainfrom
thisisnic:GH-48057_metdata

Conversation

@thisisnic

@thisisnic thisisnic commented Nov 12, 2025 •

Copy link
Copy Markdown
Member

Rationale for this change

Slow reading due to looping through metadata

What changes are included in this PR?

Don't loop through NULL metadata

Are these changes tested?

Not in unit tests, but see comment below with microbenchmarks.

Are there any user-facing changes?

No

@github-actions

Copy link
Copy Markdown

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

@thisisnic

Copy link
Copy Markdown
Member Author

Bit of a weird one to write tests for, so I just ran microbenchmarks using the code supplied by the user on the linked issue.

Running user-supplied reprex on main branch without this change:

> microbenchmark::microbenchmark(fn_arrow(), fn_nano(), times = 20)
Unit: milliseconds
       expr       min         lq     mean    median        uq       max neval
 fn_arrow() 7591.8715 7810.82744 7918.969 7883.9831 8092.0043 8238.5480    20
  fn_nano()   77.6787   90.42936  119.264  109.1114  126.8542  243.1027    20 

Then, with it:

> microbenchmark::microbenchmark(fn_arrow(), fn_nano(), times = 20)
Unit: milliseconds
       expr       min        lq     mean   median       uq      max neval
 fn_arrow() 254.86050 259.42603 301.5087 265.7393 285.8860 707.4794    20
  fn_nano()  73.24052  92.19223 147.6159 118.3257 218.1055 240.3102    20 

@thisisnic
thisisnic marked this pull request as ready for review November 12, 2025 10:51
@thisisnic
thisisnic requested a review from jonkeane as a code owner November 12, 2025 10:51
Comment thread r/R/metadata.R
if (is.data.frame(x)) {
# if columns metadata exists, apply it here
if (length(names(x)) && !is.null(columns_metadata)) {
if (length(names(x)) && !is.null(columns_metadata) && !all(map_lgl(columns_metadata, is.null))) {

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.

I know we don't want to benchmark this in our code, but this looks like something we want a test for, yeah?

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.

Oops, didn't think it was testable, but it totally is; added!

expect_identical(df_no_metadata, df_with_metadata)

# with more complex column metadata - it preserves it
spicy_df_arrow <- arrow_table(haven_data)

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.

🌶️

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

One small addition, but good otherwise

})

test_that("apply_arrow_r_metadata doesn't add in metadata from plain data.frame objects", {
# with just a plain df the (empty) column metadata is not preserved

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.

Mind adding the issue number to this comment?

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit 9ea91ca.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 1 possible false positive for unstable benchmarks that are known to sometimes produce them.

Mottl pushed a commit to Mottl/arrow that referenced this pull request May 26, 2026
…metadata() looping through all columns, including NULL ones (apache#48104)

### Rationale for this change

Slow reading due to looping through metadata

### What changes are included in this PR?

Don't loop through NULL metadata

### Are these changes tested?

Not in unit tests, but see comment below with microbenchmarks.

### Are there any user-facing changes?

No

* GitHub Issue: apache#48057

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
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.

2 participants