Skip to content

Commit

Permalink
Merge pull request #68 from r-lib/no-names-data1
Browse files Browse the repository at this point in the history
No names for data1 list
  • Loading branch information
EmilHvitfeldt authored Jun 11, 2024
2 parents 10dce2a + 8c0a166 commit bfb4b00
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 47 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sparsevctrs (development version)

* All sparse vector types now have a significant smaller base object size. (#67)

# sparsevctrs 0.1.0

* Initial CRAN submission.
8 changes: 4 additions & 4 deletions R/sparse_character.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ sparse_character <- function(values, positions, length, default = "") {

new_sparse_character <- function(values, positions, length, default) {
x <- list(
val = values,
pos = positions,
len = length,
default = default
values,
positions,
length,
default
)

.Call(ffi_altrep_new_sparse_string, x)
Expand Down
8 changes: 4 additions & 4 deletions R/sparse_double.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ sparse_double <- function(values, positions, length, default = 0) {

new_sparse_double <- function(values, positions, length, default) {
x <- list(
val = values,
pos = positions,
len = length,
default = default
values,
positions,
length,
default
)

.Call(ffi_altrep_new_sparse_double, x)
Expand Down
8 changes: 4 additions & 4 deletions R/sparse_integer.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ sparse_integer <- function(values, positions, length, default = 0L) {

new_sparse_integer <- function(values, positions, length, default) {
x <- list(
val = values,
pos = positions,
len = length,
default = default
values,
positions,
length,
default
)

.Call(ffi_altrep_new_sparse_integer, x)
Expand Down
8 changes: 4 additions & 4 deletions R/sparse_logical.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ sparse_logical <- function(values, positions, length, default = FALSE) {

new_sparse_logical <- function(values, positions, length, default) {
x <- list(
val = values,
pos = positions,
len = length,
default = default
values,
positions,
length,
default
)

.Call(ffi_altrep_new_sparse_logical, x)
Expand Down
7 changes: 0 additions & 7 deletions src/altrep-sparse-double.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ static SEXP altrep_sparse_double_Extract_subset(SEXP x, SEXP indx, SEXP call) {
SEXP out_default = extract_default(x);
SET_VECTOR_ELT(out, 3, out_default);

SEXP names = Rf_allocVector(STRSXP, 4);
Rf_setAttrib(out, R_NamesSymbol, names);
SET_STRING_ELT(names, 0, Rf_mkChar("val"));
SET_STRING_ELT(names, 1, Rf_mkChar("pos"));
SET_STRING_ELT(names, 2, Rf_mkChar("len"));
SET_STRING_ELT(names, 3, Rf_mkChar("default"));

R_xlen_t i_out = 0;

for (R_xlen_t i = 0; i < size; ++i) {
Expand Down
7 changes: 0 additions & 7 deletions src/altrep-sparse-integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ static SEXP altrep_sparse_integer_Extract_subset(SEXP x, SEXP indx, SEXP call) {
SEXP out_default = extract_default(x);
SET_VECTOR_ELT(out, 3, out_default);

SEXP names = Rf_allocVector(STRSXP, 4);
Rf_setAttrib(out, R_NamesSymbol, names);
SET_STRING_ELT(names, 0, Rf_mkChar("val"));
SET_STRING_ELT(names, 1, Rf_mkChar("pos"));
SET_STRING_ELT(names, 2, Rf_mkChar("len"));
SET_STRING_ELT(names, 3, Rf_mkChar("default"));

R_xlen_t i_out = 0;

for (R_xlen_t i = 0; i < size; ++i) {
Expand Down
7 changes: 0 additions & 7 deletions src/altrep-sparse-logical.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ static SEXP altrep_sparse_logical_Extract_subset(SEXP x, SEXP indx, SEXP call) {
SEXP out_default = extract_default(x);
SET_VECTOR_ELT(out, 3, out_default);

SEXP names = Rf_allocVector(STRSXP, 4);
Rf_setAttrib(out, R_NamesSymbol, names);
SET_STRING_ELT(names, 0, Rf_mkChar("val"));
SET_STRING_ELT(names, 1, Rf_mkChar("pos"));
SET_STRING_ELT(names, 2, Rf_mkChar("len"));
SET_STRING_ELT(names, 3, Rf_mkChar("default"));

R_xlen_t i_out = 0;

for (R_xlen_t i = 0; i < size; ++i) {
Expand Down
7 changes: 0 additions & 7 deletions src/altrep-sparse-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@ static SEXP altrep_sparse_string_Extract_subset(SEXP x, SEXP indx, SEXP call) {
SEXP out_default = extract_default(x);
SET_VECTOR_ELT(out, 3, out_default);

SEXP names = Rf_allocVector(STRSXP, 4);
Rf_setAttrib(out, R_NamesSymbol, names);
SET_STRING_ELT(names, 0, Rf_mkChar("val"));
SET_STRING_ELT(names, 1, Rf_mkChar("pos"));
SET_STRING_ELT(names, 2, Rf_mkChar("len"));
SET_STRING_ELT(names, 3, Rf_mkChar("default"));

R_xlen_t i_out = 0;

for (R_xlen_t i = 0; i < size; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions vignettes/articles/when-to-use.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ obj_size(integer(2000))
obj_size(integer(3000))
```

The vectors above only contained the value `0`. We can replicate that sparsely with `sparse_integer(integer(), integer(), length = 0)`. We see that the size of a 0-length sparse integer vector has a size of `1304 B = 1.3 kB`.
The vectors above only contained the value `0`. We can replicate that sparsely with `sparse_integer(integer(), integer(), length = 0)`. We see that the size of a 0-length sparse integer vector has a size of `888 B`.

```{r}
obj_size(sparse_integer(integer(), integer(), length = 0))
Expand Down Expand Up @@ -80,9 +80,9 @@ sparse_x <- sparse_integer(1:200, 1:200, 1000)
obj_size(sparse_x)
```

So it all comes down to a trade-off. Dense integer vectors with a size of 313 or less will be smaller than their sparse counterparts no matter what. Dense integer vector vectors with 314 elements will take up the same amount of memory as their sparse counterpart with no values.
So it all comes down to a trade-off. Dense integer vectors with a size of 210 or less will be smaller than their sparse counterparts no matter what. Dense integer vector vectors with 211 elements will take up the same amount of memory as their sparse counterpart with no values.

From these values we can calculate memory equivalent vectors to determine which would be more efficient, noting that sparse vectors increase in size by twice for each non-default value that their dense counterpart. For a vector of length 1000, the sparse vector will be equivalent in size if it has `343` non-default values. And these values continue to go up.
From these values we can calculate memory equivalent vectors to determine which would be more efficient, noting that sparse vectors increase in size by twice for each non-default value that their dense counterpart. For a vector of length 1000, the sparse vector will be equivalent in size if it has `210` non-default values. And these values continue to go up.

## Conversion speed

Expand Down

0 comments on commit bfb4b00

Please sign in to comment.