Skip to content

Commit 609e53d

Browse files
perf: Do not recreate and fill the environment in .savvy_wrap_PlRExpr() (#1444)
Co-authored-by: eitsupi <[email protected]>
1 parent f05d610 commit 609e53d

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

.lintr.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ linters <- linters_with_defaults(
99
exclusions <- as.list(
1010
c(
1111
"R/000-wrappers.R",
12+
"R/001-wrappers.R",
1213
fs::dir_ls("R", glob = "**/import-standalone-*.R") |>
1314
rlang::set_names(NULL)
1415
)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method("!",polars_expr)
44
S3method("!=",polars_expr)
55
S3method("!=",polars_series)
6+
S3method("$",PlRExpr)
67
S3method("$",polars_data_frame)
78
S3method("$",polars_dtype)
89
S3method("$",polars_dtype_enum)

R/001-wrappers.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Override the default `.savvy_wrap_*` functions for avoiding assignment many members to the env.
2+
# See <https://github.com/pola-rs/r-polars/issues/1439> for details.
3+
# TODO: generate this file automatically
4+
5+
PlRExprMethods <- new.env(parent = emptyenv())
6+
7+
`.savvy_wrap_PlRExpr` <- function(ptr) {
8+
e <- new.env(parent = emptyenv())
9+
e$.ptr <- ptr
10+
11+
class(e) <- c("PlRExpr", "savvy_polars__sealed")
12+
e
13+
}
14+
15+
#' @export
16+
`$.PlRExpr` <- function(x, name) {
17+
method_names <- names(PlRExprMethods)
18+
ptr <- env_get(x, ".ptr")
19+
20+
if (identical(name, ".ptr")) {
21+
ptr
22+
} else if (name %in% method_names) {
23+
PlRExprMethods[[name]](ptr)
24+
} else {
25+
NextMethod()
26+
}
27+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ assign_objects_to_env <- function(env, obj_name_pattern, ..., search_env = paren
3636
}
3737

3838
POLARS_STORE_ENVS <- list(
39+
"PlRExpr_" = PlRExprMethods,
3940
"pl__" = pl,
4041
"cs__" = cs,
4142
"pl_api_" = pl__api,

0 commit comments

Comments
 (0)