Skip to content

Conversation

@aitap
Copy link
Member

@aitap aitap commented Dec 19, 2025

Towards #6180.

Replace remaining uses of SET_ATTRIB with SHALLOW_DUPLICATE_ATTRIB or CLEAR_ATTRIB. Replace remaining uses of ATTRIB testing with ANY_ATTRIB; use the new R_mapAttrib interface to walk the attributes.

  • The most invasive fix is in assign.c where attr(., 'index') is adjusted following a by-reference operation that could invalidate some indices. What used to be a for (cons = ATTRIB(x); !isNull(cons); cons = CDR(cons)) loop now has to construct a context struct to 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 like memset(string, '\0', 1) and making use of the new hash table to look up the indices.
  • The compact rownames attribute in dogroups.c is also a bit tricky. Now dogroups creates a throwaway INTSXP c(NA_integer, -whatever), gives it to setAttrib(SD, R_RowNamesSymbol, decoy), and obtains the actual compact rownames attribute 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 modify rownames by reference without spilling it into a large string vector.

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
Copy link

codecov bot commented Dec 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.07%. Comparing base (be67a4d) to head (2c10c37).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7487      +/-   ##
==========================================
+ Coverage   99.04%   99.07%   +0.03%     
==========================================
  Files          87       87              
  Lines       16671    16691      +20     
==========================================
+ Hits        16511    16536      +25     
+ Misses        160      155       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link

  • HEAD=attrib slower P<0.001 for memrecycle regression fixed in #5463
    Comparison Plot

Generated via commit 2c10c37

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 5 minutes and 8 seconds
Installing different package versions 11 minutes and 13 seconds
Running and plotting the test cases 5 minutes and 5 seconds

@ben-schwen ben-schwen added this to the 1.19.0 milestone Dec 21, 2025
Copy link
Member

@HughParsonage HughParsonage left a 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_) {
Copy link
Member

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?

Copy link
Member Author

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;
Copy link
Member

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);
Copy link
Member

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;
Copy link
Member

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);
Copy link
Member

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.

Copy link
Member Author

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?

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);
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 is wrong; check the return value of setAttrib

Copy link
Member

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.

Copy link
Member Author

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.

@aitap
Copy link
Member Author

aitap commented Dec 25, 2025

This is expected to become more of a problem around end of January.

@aitap aitap modified the milestones: 1.19.0, 1.18.2 Dec 25, 2025
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.

3 participants