@@ -74,6 +74,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
7474 error (_ ("Failed to allocate upper bound of %" PRId64 " unique column names [sum(lapply(l,ncol))]" ), (int64_t )upperBoundUniqueNames );
7575 savetl_init ();
7676 int nuniq = 0 ;
77+ // first pass - gather unique column names
7778 for (int i = 0 ; i < LENGTH (l ); i ++ ) {
7879 SEXP li = VECTOR_ELT (l , i );
7980 int thisncol = LENGTH (li );
@@ -89,11 +90,8 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
8990 SET_TRUELENGTH (s ,- nuniq );
9091 }
9192 }
92- if (nuniq > 0 ) {
93- SEXP * tt = realloc (uniq , nuniq * sizeof (SEXP )); // shrink to only what we need to release the spare
94- if (!tt ) free (uniq ); // shrink never fails; just keep codacy happy
95- uniq = tt ;
96- }
93+ if (nuniq > 0 ) uniq = realloc (uniq , nuniq * sizeof (SEXP )); // shrink to only what we need to release the spare
94+
9795 // now count the dups (if any) and how they're distributed across the items
9896 int * counts = (int * )calloc (nuniq , sizeof (int )); // counts of names for each colnames
9997 int * maxdup = (int * )calloc (nuniq , sizeof (int )); // the most number of dups for any name within one colname vector
@@ -105,6 +103,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
105103 error (_ ("Failed to allocate nuniq=%d items working memory in rbindlist.c" ), nuniq );
106104 // # nocov end
107105 }
106+ // second pass - count duplicates
108107 for (int i = 0 ; i < LENGTH (l ); i ++ ) {
109108 SEXP li = VECTOR_ELT (l , i );
110109 int thisncol = length (li );
@@ -143,6 +142,7 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
143142 for (int i = 0 ; i < ncol ; ++ i ) {uniqMap [i ] = dupLink [i ] = -1 ;}
144143 int nextCol = 0 , lastDup = ncol - 1 ;
145144
145+ // third pass - create final column mapping colMapRaw
146146 for (int i = 0 ; i < LENGTH (l ); ++ i ) {
147147 SEXP li = VECTOR_ELT (l , i );
148148 int thisncol = length (li );
0 commit comments