Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add altrep version of sparse real #2

Merged
merged 37 commits into from
Apr 29, 2024
Merged

Add altrep version of sparse real #2

merged 37 commits into from
Apr 29, 2024

Conversation

EmilHvitfeldt
Copy link
Member

@EmilHvitfeldt EmilHvitfeldt commented Apr 19, 2024

This PR contains what I think is a minimally sufficient altrep class that provides a sparse double vector.

The goal of this PR is to be good C practice, further performance improvements will happen in further PRs.

DESCRIPTION Outdated Show resolved Hide resolved
R/altrep.R Outdated Show resolved Hide resolved
src/altrep-sparse-real.c Outdated Show resolved Hide resolved
src/altrep-sparse-real.c Outdated Show resolved Hide resolved
src/altrep-sparse-real.c Show resolved Hide resolved
src/altrep-sparse-real.c Outdated Show resolved Hide resolved
src/altrep-sparse-real.c Show resolved Hide resolved
src/altrep-sparse-real.c Outdated Show resolved Hide resolved
SEXP data1 = R_altrep_data1(x);
SEXP val_old = VECTOR_ELT(data1, 0);
SEXP pos_old = VECTOR_ELT(data1, 1);
SEXP matches = Rf_match(pos_old, indx, R_NaInt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also use the binary search approach here, and it would be super nice. What I imagine is a loop like

for (i in seq_along(indx)) {
  match[i] = binary_search(indx[i], pos)
}

Where binary_search() is a C function that returns:

  • NA_INTEGER if it can't find the value
  • The C indexed location in pos of the value

You could reuse that same binary_search() function in the Elt method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd also be able to count up the number of matches in that loop directly rather than in the n loop below, like

match[i] = binary_search(indx[i], pos)
count = count + (match[i] != NA_INTEGER)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm not sure about the guarantees on Extract_subset but in theory indx may contain NA_INTEGER or an index that is greater than the length of your vector.

i.e. (1:3)[4] or (1:3)[NA_integer_]

and I'm pretty sure you don't account for that

For example, both of those would be "no match" cases but you do want to allocate space for them in the result (both result in NA values)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the binary search loop could be something like

for (i in seq_along(indx)) {
  if (indx[i] > length) {
    match[i] = NA;
    continue;
  }
  if (index[i] == NA) {
    match[i] = NA;
    continue;
  }
  match[i] = binary_search(indx[i], pos)
}

Where this time binary_search() would return length(pos) in the no match case (that's how a typical binary search works) allowing you to differentiate between the 3 cases of:

  • Index was OOB or NA (NA value in result)
  • Index was located in pos (val value in result)
  • Index was < len, so could have had a value in pos, but does not appear in pos, so we ignore it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If handling NA and OOB values seems too complicated to mix in, then one option is to do an initial loop over indx and look for NA or OOB indices and if you detect any then you return NULL from the function immediately, which is the ALTREP way of saying "i give up, handle this with the fallback method" which will materialize the vector. That's totally reasonable and is what vroom does.

Then you'd just handle values in the range of 1 -> len

int step = 0;
int what_pos = 1;

for (int i = 0; i < Rf_length(matches); ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified into a single loop - we can talk about it over zoom

@EmilHvitfeldt EmilHvitfeldt merged commit 9fc394e into main Apr 29, 2024
10 of 11 checks passed
@EmilHvitfeldt EmilHvitfeldt deleted the altrep branch April 29, 2024 19:25
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