Skip to content

move helper functions outside of [ - #7935

Open
ben-schwen wants to merge 1 commit into
bracket_modularizationfrom
brk/a4_move_helpers
Open

ben-schwen wants to merge 1 commit into
bracket_modularizationfrom
brk/a4_move_helpers

Conversation

@ben-schwen

Copy link
Copy Markdown
Member

Towards #852

Since this change consists of a lot code moving I would recommend reviewing it with
git diff brk/a4_move_helpers^! --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change
which displays the real changes nicely

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (bracket_modularization@5c990f3). Learn more about missing BASE report.

Additional details and impacted files
@@                    Coverage Diff                    @@
##             bracket_modularization    #7935   +/-   ##
=========================================================
  Coverage                          ?   99.09%           
=========================================================
  Files                             ?       88           
  Lines                             ?    17430           
  Branches                          ?        0           
=========================================================
  Hits                              ?    17272           
  Misses                            ?      158           
  Partials                          ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
  • HEAD=brk/a4_move_helpers much slower for transform improved in #5493
  • HEAD=brk/a4_move_helpers much slower for as.data.table.array improved in #7019

(Plot image upload to picrd.com failed; download the artifact below to view plots)

Generated via commit ca6af09

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 1 minutes and 25 seconds
Installing different package versions 49 seconds
Running and plotting the test cases 6 minutes and 10 seconds

Comment thread R/data.table.R Outdated
jvnames[head(key_idx, missing_keys[1L] - 1L)]
}
shared_keys = get_shared_keys(jsub, jvnames, sdvars = sdvars, key(x))
shared_keys = .dt_get_shared_keys(jsub, jvnames, sdvars = sdvars, key(x))

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.

Suggested change
shared_keys = .dt_get_shared_keys(jsub, jvnames, sdvars = sdvars, key(x))
shared_keys = .dt_get_shared_keys(jsub, jvnames, sdvars, key(x))

Comment thread R/translation.R Outdated
raise_condition = function(signal, message, classes, immediate=FALSE, appendLF=FALSE) {
obj = list(message=message, call=sys.call(sys.nframe()-2L))
n = sys.nframe() - 2L # the caller of stopf() etc.
# helpers extracted from [.data.table are named .dt_*; their conditions report the call of [.data.table (or whoever called them)

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.

Maybe they should be named .dtq_ (query) to distinguish them from other internal functions?

Another alternative to consider (especially w.r.t. potential performance drawbacks) would be to S3 them:

.zip_items = function(...)
class(.zip_items) = "dt_query_helper"

And here:

while (inherits(sys.call(n), "dt_query_helper")) ...

(I haven't tested if sys.call() retains the attribute).


At a higher level, is it really that much more beneficial to see "Error in [.data.table" than "Error in .zip_items"? The latter would appear to be more specific about which of the many steps in [ went awry; and beginner users can be "scared" by "[.data.table" too (they've never written [.data.table before -- where did it come from?). Would just using call.=FALSE suffice?

Also, be sure to evaluate the robustness of the proposed solution. I have memories of endless headaches trying to inspect the call stack and provide better errors in {bit64} where I wanted to know "Did a user call this exported S3 method directly, or did they get here from the generic?". I ultimately decided it was impossible to resolve in full generality. Edge cases to consider: DT() where [ gets masked by another name; tryCatch() / withCallingHandlers(); substitute().

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.

library(bench)

by_name  = function(n) { call = sys.call(n); is.call(call) && is.name(f <- call[[1L]]) && startsWith(as.character(f), ".dt_") }
by_class = function(n) inherits(sys.function(n), "dt_query_helper")

walk = function(check) {
  n = sys.nframe() - 1L
  parents = sys.parents()
  while (n > 0L && parents[n] > 0L && check(n)) n = parents[n]
  n
}

.dt_inner = function(check) walk(check)
.dt_outer = function(check) .dt_inner(check)
class(.dt_inner) = class(.dt_outer) = "dt_query_helper"
`[.fake` = function(x, check) .dt_outer(check)
x = structure(list(), class="fake")

stopifnot(identical(x[by_name], x[by_class]))
mark(by_name = x[by_name], by_class = x[by_class], iterations=1e5)
#   A tibble: 2 × 13
#   expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result    memory             time                 gc                    
#   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>    <list>             <list>               <list>                
# 1 by_name      3.25µs   4.01µs   233464.        0B     133. 99943    57      428ms <int [1]> <Rprofmem [0 × 3]> <bench_tm [100,000]> <tibble [100,000 × 3]>
# 2 by_class     2.29µs   2.83µs   324089.        0B     149. 99954    46      308ms <int [1]> <Rprofmem [0 × 3]> <bench_tm [100,000]> <tibble [100,000 × 3]>

so should be same in terms of speed :)

@ben-schwen
ben-schwen removed this pull request from stack #7933 November 3, 2026 09:15
@ben-schwen
ben-schwen added this pull request to stack #7953 November 3, 2026 09:16
Base automatically changed from brk/a3_callenv to bracket_modularization November 3, 2026 10:30
Functions defined inside [ capture its frame, so they block moving the code that uses them.
Move them to internal top-level functions with class "dt_query_helper", so that conditions they raise still report the [.data.table call.

No behaviour change.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants