-
Notifications
You must be signed in to change notification settings - Fork 1k
Replace ATTRIB, SET_ATTRIB
#7487
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
base: master
Are you sure you want to change the base?
Conversation
Instead, backport and use CLEAR_ATTRIB (R >= 4.5).
Use SHALLOW_DUPLICATE_ATTRIB (R >= 3.3) for the simple case. Also, Backport ANY_ATTRIB (R >= 4.5) instead of testing !isNull(ATTRIB(.)).
Instead of trying to walk ATTRIB in search of the compact 'rownames' attribute to modify, install it anew, take note of the returned reference to the value being installed (a different one!) and modify that.
Instead of walking the attribute list directly, use R_mapAttrib(). Create a hash table of index names instead of relying on chin() and a temporary string vector. Move all temporary allocations onto the R heap.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7487 +/- ##
==========================================
+ Coverage 98.97% 99.00% +0.03%
==========================================
Files 87 87
Lines 16739 16759 +20
==========================================
+ Hits 16567 16592 +25
+ Misses 172 167 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
No obvious timing issues in HEAD=attrib Generated via commit fa2df39 Download link for the artifact containing the test results: ↓ atime-results.zip
|
HughParsonage
left a comment
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.
Maybe add some brief documentation about what each function does and returns if time permits.
| return NULL; | ||
| } | ||
|
|
||
| static SEXP fixIndexAttrib(SEXP tag, SEXP value, void *ctx_) { |
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.
Is SEXP the intended return type?
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.
Yes, that's following the R_mapAttrib interface. I have to return (SEXP)NULL to continue iteration.
src/assign.c
Outdated
| struct attrib_name_ctx { | ||
| hashtab *indexNames; | ||
| SEXP index, assignedNames; | ||
| R_xlen_t len; |
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.
Can this field name be changed to make it clear what length it is the length of. R_xlen_t len_of<...>;
src/assign.c
Outdated
| // indexLength > 0 indicates reordering. Drop it to avoid spurious reordering in non-indexed columns (#2372) | ||
| // shortened name already present indicates that index needs to be dropped to avoid duplicate indices. | ||
| setAttrib(index, tag, R_NilValue); | ||
| hash_set(indexNames, tag, 0); |
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.
Should this be?
hash_set(indexNames, PRINTNAME(tag), 0);(tag is a SYMSXP, not a CHARSXP?)
src/assign.c
Outdated
| // newcolnames : add these columns (if any) | ||
| // cols : column names or numbers corresponding to the values to set | ||
| // rows : row numbers to assign | ||
| R_len_t numToDo, targetlen, vlen, oldncol, oldtncol, coln, protecti=0, newcolnum, indexLength; |
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.
Is indexLength used in assign any more?
|
|
||
| SEXP ret = NULL; | ||
| for (; !isNull(a); REPROTECT(a = CDR(a), i)) { | ||
| ret = fun(TAG(a), CAR(a), ctx); |
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.
Not blocking, but if time permits consider R's structure more closely with protection of the TAG(a) and CAR(a) in teh callback.
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.
AFAIK while a is protected, TAG(a), CAR(a), and CDR(a) should also stay protected. What do you recommend to do instead?
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.
any reason why we are not just backporting the implementation of R, since it seems to have the same level of complexity/speed
| PROTECT_WITH_INDEX(rownames = allocVector(INTSXP, 2), &rownamesi); nprotect++; | ||
| INTEGER(rownames)[0] = NA_INTEGER; | ||
| INTEGER(rownames)[1] = -maxGrpSize; | ||
| REPROTECT(rownames = setAttrib(SD, R_RowNamesSymbol, rownames), rownamesi); |
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 is wrong; check the return value of setAttrib
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.
Consider R_mapAttrib for this.
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.
R is somewhat inconsistent about this. setAttrib(foo, R_CommentSymbol, bar) and setAttrib(foo, R_ClassSymbol, bar) return R_NilValue, but generic installAttrib and our specific case row_names_gets do return the freshly installed value. Since it's not documented, R_mapAttrib might be a safer approach.
|
This is expected to become more of a problem around end of January. |
| SEXP rownames; | ||
| PROTECT_INDEX rownamesi; | ||
| PROTECT_WITH_INDEX(rownames = allocVector(INTSXP, 2), &rownamesi); nprotect++; | ||
| INTEGER(rownames)[0] = NA_INTEGER; | ||
| INTEGER(rownames)[1] = -maxGrpSize; | ||
| REPROTECT(rownames = setAttrib(SD, R_RowNamesSymbol, rownames), rownamesi); |
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.
static SEXP findRowNames(SEXP key, SEXP val, void *data) {
(void)data;
if (key == R_RowNamesSymbol) return val;
return NULL;
}SEXP rownames = R_mapAttrib(SD, findRowNames, NULL);
if (rownames == NULL) error(_("row.names attribute of .SD not found"));
if (!isInteger(rownames) || LENGTH(rownames)!=2 || INTEGER(rownames)[0]!=NA_INTEGER) error(_("row.names of .SD isn't integer length 2 with NA as first item; i.e., .set_row_names(). [%s %d %d]"),type2char(TYPEOF(rownames)),LENGTH(rownames),INTEGER(rownames)[0]);| R_len_t ngrp, nrowgroups, njval=0, ngrpcols, ansloc=0, maxn, estn=-1, thisansloc, grpn, thislen, igrp; | ||
| int nprotect=0; | ||
| SEXP ans=NULL, jval, thiscol, BY, N, I, GRP, iSD, xSD, rownames, s, RHS, target, source; | ||
| SEXP ans=NULL, jval, thiscol, BY, N, I, GRP, iSD, xSD, s, RHS, target, source; |
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.
| SEXP ans=NULL, jval, thiscol, BY, N, I, GRP, iSD, xSD, s, RHS, target, source; | |
| SEXP ans=NULL, jval, thiscol, BY, N, I, GRP, iSD, xSD, RHS, target, source; |
| // with PR#4164 started to copy input list columns too much. Hence PR#4655 in v1.13.2 moved that copy here just where it is needed. | ||
| // Currently the marker is negative truelength. These specials are protected by us here and before we release them | ||
| // we restore the true truelength for when R starts to use vector truelength. | ||
| SEXP attribs, list_el; |
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.
| SEXP list_el; |
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.

Towards #6180.
Replace remaining uses of
SET_ATTRIBwithSHALLOW_DUPLICATE_ATTRIBorCLEAR_ATTRIB. Replace remaining uses ofATTRIBtesting withANY_ATTRIB; use the newR_mapAttribinterface to walk the attributes.assign.cwhereattr(., 'index')is adjusted following a by-reference operation that could invalidate some indices. What used to be afor (cons = ATTRIB(x); !isNull(cons); cons = CDR(cons))loop now has to construct a contextstructto share between the caller and the map callback. On the plus side, I simplified the memory management and string operations a bit, getting rid of things likememset(string, '\0', 1)and making use of the new hash table to look up the indices.rownamesattribute indogroups.cis also a bit tricky. Nowdogroupscreates a throwawayINTSXPc(NA_integer, -whatever), gives it tosetAttrib(SD, R_RowNamesSymbol, decoy), and obtains the actual compactrownamesattribute that R re-creates from the return value. I think this is simpler than walking the attribute list by hand in search of the correct attribute, and the end result is the same: we can modifyrownamesby reference without spilling it into a large string vector.