From 9d654590b05280ee5e70d1746096763a44f04dab Mon Sep 17 00:00:00 2001 From: sorhawell Date: Sun, 12 Nov 2023 21:24:15 +0100 Subject: [PATCH 1/3] drop_all_in_place --- R/extendr-wrappers.R | 2 ++ src/rust/src/rdataframe/mod.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index 209b63df3..4b5732f2e 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -129,6 +129,8 @@ DataFrame$default <- function() .Call(wrap__DataFrame__default) DataFrame$lazy <- function() .Call(wrap__DataFrame__lazy, self) +DataFrame$drop_all_in_place <- function() invisible(.Call(wrap__DataFrame__drop_all_in_place, self)) + DataFrame$new_with_capacity <- function(capacity) .Call(wrap__DataFrame__new_with_capacity, capacity) DataFrame$set_column_from_robj <- function(robj, name) .Call(wrap__DataFrame__set_column_from_robj, self, robj, name) diff --git a/src/rust/src/rdataframe/mod.rs b/src/rust/src/rdataframe/mod.rs index fa63da33c..327ba3e37 100644 --- a/src/rust/src/rdataframe/mod.rs +++ b/src/rust/src/rdataframe/mod.rs @@ -124,6 +124,11 @@ impl DataFrame { LazyFrame(self.0.clone().lazy()) } + //internal use only + pub fn drop_all_in_place(&mut self) { + *self = Self::new_with_capacity(0); + } + //internal use pub fn new_with_capacity(capacity: i32) -> Self { let empty_series: Vec = Vec::with_capacity(capacity as usize); From 8158a53d572ecacc4dde0a845b62e8ad1404dac7 Mon Sep 17 00:00:00 2001 From: sorhawell Date: Sun, 12 Nov 2023 21:46:00 +0100 Subject: [PATCH 2/3] add unittest --- tests/testthat/test-dataframe.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test-dataframe.R b/tests/testthat/test-dataframe.R index a883abb67..cf1c8130b 100644 --- a/tests/testthat/test-dataframe.R +++ b/tests/testthat/test-dataframe.R @@ -1206,3 +1206,19 @@ test_that("transpose", { df_expected ) }) + +test_that("drop_all_in_place", { + + # this test verifies internal function in_place drop all Series in DataFrame + # will not affect a fully cloned DataFrame df_clone + + df_copy = df = polars::pl$DataFrame(mtcars) + df_clone = df$clone() + s = df$get_column("cyl") + .pr$DataFrame$drop_all_in_place(df) # we make this method and instead drop in place - level 3-5 + expect_identical(df$shape, c(0,0)) + expect_identical(df_copy$shape, c(0,0)) + expect_identical(df_clone$shape, c(32,11)) + expect_identical(s$len(), 32) +}) + From b2bb1d92ea3122b6976743ed094f1344acc6d554 Mon Sep 17 00:00:00 2001 From: sorhawell Date: Wed, 15 Nov 2023 01:14:09 +0100 Subject: [PATCH 3/3] add news + drop comment --- NEWS.md | 4 ++++ tests/testthat/test-dataframe.R | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 66ce5bb9c..61da6cb92 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,10 @@ ## What's changed - New methods `$write_json()` and `$write_ndjson()` for DataFrame (#502). +- New private method `.pr$DataFrame$drop_all_in_place(df)` to drop `DataFrame` in-place, + to release memory without invoking gc(). However, if there are other strong references to any of + the underlying Series or arrow arrays, that memory will specifically not be released. This method + is aimed for r-polars extensions, and will be kept stable as much as possible (#504). # polars 0.10.1 diff --git a/tests/testthat/test-dataframe.R b/tests/testthat/test-dataframe.R index cf1c8130b..b3c534178 100644 --- a/tests/testthat/test-dataframe.R +++ b/tests/testthat/test-dataframe.R @@ -1215,7 +1215,7 @@ test_that("drop_all_in_place", { df_copy = df = polars::pl$DataFrame(mtcars) df_clone = df$clone() s = df$get_column("cyl") - .pr$DataFrame$drop_all_in_place(df) # we make this method and instead drop in place - level 3-5 + .pr$DataFrame$drop_all_in_place(df) expect_identical(df$shape, c(0,0)) expect_identical(df_copy$shape, c(0,0)) expect_identical(df_clone$shape, c(32,11))