-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
src/altrep-sparse-real.c
Outdated
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 inpos
, but does not appear inpos
, so we ignore it
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
Co-authored-by: Davis Vaughan <davis@posit.co>
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.