-
Notifications
You must be signed in to change notification settings - Fork 1k
delete rows by reference #7536
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
Open
ben-schwen
wants to merge
3
commits into
setallocrow
Choose a base branch
from
delete_by_ref
base: setallocrow
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
delete rows by reference #7536
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| #include "data.table.h" | ||
|
|
||
| static void computePrefixSum(const int *keep, int *dest, R_xlen_t n, int nthreads); | ||
| static void compactVectorRaw(SEXP col, const int *dest, const int *keep, R_xlen_t new_nrow, R_xlen_t old_nrow); | ||
|
|
||
| SEXP deleteRows(SEXP dt, SEXP rows_to_delete) { | ||
| if (!isNewList(dt)) | ||
| error("Internal error: deleteRows received non-list dt"); // #nocov | ||
| if (!xlength(dt)) return dt; // zero-column data.table | ||
|
|
||
| const R_xlen_t ncol = length(dt); | ||
| const R_xlen_t old_nrow = length(VECTOR_ELT(dt, 0)); | ||
| int nprotect = 0; | ||
|
|
||
| if (old_nrow == 0) return dt; | ||
|
|
||
| if (!isInteger(rows_to_delete) && !isLogical(rows_to_delete)) | ||
| internal_error(__func__, "rows_to_delete must be logical, integer, or numeric"); // #nocov | ||
|
|
||
| int *keep = (int *)R_alloc(old_nrow, sizeof(int)); | ||
| const R_xlen_t n = length(rows_to_delete); | ||
| for (R_xlen_t i = 0; i < old_nrow; i++) keep[i] = 1; | ||
| int *idx = INTEGER(rows_to_delete); | ||
| for (R_xlen_t j = 0; j < n; j++) { | ||
| if (idx[j] == NA_INTEGER) continue; | ||
| // should be checked from irows in [ | ||
| if (idx[j] < 1 || idx[j] > old_nrow) internal_error(__func__, "Row index %d out of range [1, %lld]", idx[j], (long long)old_nrow); //# nocov | ||
| keep[idx[j] - 1] = 0; | ||
| } | ||
|
|
||
| R_xlen_t new_nrow = 0; | ||
| for (R_xlen_t i = 0; i < old_nrow; i++) new_nrow += keep[i]; | ||
| if (new_nrow == old_nrow) return dt; | ||
|
|
||
| int *dest = (int *)R_alloc(old_nrow, sizeof(int)); | ||
| const int nthreads = getDTthreads(old_nrow, true); | ||
| computePrefixSum(keep, dest, old_nrow, nthreads); | ||
|
|
||
| // Compact each column | ||
| for (R_xlen_t j = 0; j < ncol; j++) { | ||
| SEXP col = VECTOR_ELT(dt, j); | ||
| if (!R_isResizable(col)) { | ||
| // catered for ALTREP above | ||
| SEXP newcol = PROTECT(copyAsPlain(col, 0)); nprotect++; | ||
| SET_VECTOR_ELT(dt, j, newcol); | ||
| col = newcol; | ||
| } | ||
| compactVectorRaw(col, dest, keep, new_nrow, old_nrow); | ||
| R_resizeVector(col, new_nrow); | ||
| SET_VECTOR_ELT(dt, j, col); | ||
| } | ||
|
|
||
| SEXP rownames = PROTECT(getAttrib(dt, R_RowNamesSymbol)); nprotect++; | ||
| if (!isNull(rownames)) { | ||
| // create them from scratch like in dogroups or subset to avoid R internal issues | ||
| SEXP rn = PROTECT(allocVector(INTSXP, 2)); nprotect++; | ||
| INTEGER(rn)[0] = NA_INTEGER; | ||
| INTEGER(rn)[1] = -(int)new_nrow; | ||
| setAttrib(dt, R_RowNamesSymbol, rn); | ||
| } | ||
|
|
||
| // Clear key and indices | ||
| setAttrib(dt, install("sorted"), R_NilValue); | ||
| setAttrib(dt, install("index"), R_NilValue); | ||
|
|
||
| UNPROTECT(nprotect); | ||
| return dt; | ||
| } | ||
|
|
||
| // Parallel prefix sum (exclusive scan) | ||
| // Two-pass algorithm: first count per thread, then scan, then local prefix sum | ||
| static void computePrefixSum(const int *keep, int *dest, R_xlen_t n, int nthreads) { | ||
| if (nthreads == 1) { | ||
| // Sequential version | ||
| int sum = 0; | ||
| for (R_xlen_t i = 0; i < n; i++) { | ||
| dest[i] = sum; | ||
| sum += keep[i]; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Parallel version with two passes | ||
| int *thread_counts = (int *)R_alloc(nthreads, sizeof(int)); | ||
|
|
||
| // Pass 1: Count keeps per thread | ||
| #pragma omp parallel num_threads(nthreads) | ||
| { | ||
| const int tid = omp_get_thread_num(); | ||
| const R_xlen_t chunk_size = (n + nthreads - 1) / nthreads; | ||
| const R_xlen_t start = tid * chunk_size; | ||
| const R_xlen_t end = (start + chunk_size > n) ? n : start + chunk_size; | ||
|
|
||
| int local_count = 0; | ||
| for (R_xlen_t i = start; i < end; i++) { | ||
| local_count += keep[i]; | ||
| } | ||
| thread_counts[tid] = local_count; | ||
| } | ||
|
|
||
| // Sequential scan of thread counts to get offsets | ||
| int *thread_offsets = (int *)R_alloc(nthreads, sizeof(int)); | ||
| thread_offsets[0] = 0; | ||
| for (int t = 1; t < nthreads; t++) { | ||
| thread_offsets[t] = thread_offsets[t-1] + thread_counts[t-1]; | ||
| } | ||
|
|
||
| // Pass 2: Compute local prefix sum with offset | ||
| #pragma omp parallel num_threads(nthreads) | ||
| { | ||
| const int tid = omp_get_thread_num(); | ||
| const R_xlen_t chunk_size = (n + nthreads - 1) / nthreads; | ||
| const R_xlen_t start = tid * chunk_size; | ||
| const R_xlen_t end = (start + chunk_size > n) ? n : start + chunk_size; | ||
|
|
||
| int local_sum = thread_offsets[tid]; | ||
| for (R_xlen_t i = start; i < end; i++) { | ||
| dest[i] = local_sum; | ||
| local_sum += keep[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #define COMPACT(CTYPE, ACCESSOR) { \ | ||
| CTYPE *p = ACCESSOR(col); \ | ||
| R_xlen_t i = 0; \ | ||
| while (i < old_nrow) { \ | ||
| if (!keep[i]) { \ | ||
| i++; \ | ||
| continue; \ | ||
| } \ | ||
| R_xlen_t run_start = i; \ | ||
| int target_idx = dest[i]; \ | ||
| while (i < old_nrow && keep[i]) i++; \ | ||
| size_t run_len = i - run_start; \ | ||
| if (target_idx != run_start) { \ | ||
| memmove(p + target_idx, p + run_start, run_len * sizeof(CTYPE)); \ | ||
| } \ | ||
| } \ | ||
| } | ||
|
|
||
|
|
||
| // Type-specific stream compaction | ||
| static void compactVectorRaw(SEXP col, const int *dest, const int *keep, | ||
| R_xlen_t new_nrow, R_xlen_t old_nrow) { | ||
| switch(TYPEOF(col)) { | ||
| case INTSXP: | ||
| case LGLSXP: { | ||
| COMPACT(int, INTEGER); | ||
| break; | ||
| } | ||
| case REALSXP: { | ||
| COMPACT(double, REAL); | ||
| break; | ||
| } | ||
| case CPLXSXP: { | ||
| COMPACT(Rcomplex, COMPLEX); | ||
| break; | ||
| } | ||
| case RAWSXP: { | ||
| COMPACT(Rbyte, RAW); | ||
| break; | ||
| } | ||
| case STRSXP: { | ||
| for (R_xlen_t i = 0; i < old_nrow; i++) { | ||
| if (keep[i]) SET_STRING_ELT(col, dest[i], STRING_ELT(col, i)); | ||
| } | ||
| break; | ||
| } | ||
| case VECSXP: { | ||
| for (R_xlen_t i = 0; i < old_nrow; i++) { | ||
| if (keep[i]) SET_VECTOR_ELT(col, dest[i], VECTOR_ELT(col, i)); | ||
| } | ||
| break; | ||
| } | ||
| default: | ||
| error("Unsupported column type %s", type2char(TYPEOF(col))); // #nocov | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 am not 100% sure here... On first time when we delete rows by reference, then columns are not yet marked as resizable, therefore they need to be allocated as resizable, so their addresses will change. If so I think it make sense to describe that logic. AFAIR it was the reason for having setallocrow, to make columns resizable in advance to avoid unexpected reallocations later on.