Optimize bulk selection

This commit is contained in:
Arun Prakash Jana 2021-07-15 20:12:07 +05:30
parent 0159c08602
commit 4ec87e3021
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -1406,20 +1406,16 @@ static void writesel(const char *buf, const size_t buflen)
printwarn(NULL); printwarn(NULL);
} }
static bool appendfpath(const char *path, const size_t len) static void appendfpath(const char *path, const size_t len)
{ {
bool ret = FALSE;
if ((selbufpos >= selbuflen) || ((len + 3) > (selbuflen - selbufpos))) { if ((selbufpos >= selbuflen) || ((len + 3) > (selbuflen - selbufpos))) {
selbuflen += PATH_MAX; selbuflen += PATH_MAX;
pselbuf = xrealloc(pselbuf, selbuflen); pselbuf = xrealloc(pselbuf, selbuflen);
ret = TRUE;
if (!pselbuf) if (!pselbuf)
errexit(); errexit();
} }
selbufpos += xstrsncpy(pselbuf + selbufpos, path, len); selbufpos += xstrsncpy(pselbuf + selbufpos, path, len);
return ret;
} }
/* Write selected file paths to fd, linefeed separated */ /* Write selected file paths to fd, linefeed separated */
@ -1717,32 +1713,42 @@ static bool scanselforpath(const char *path)
static void addtoselbuf(char *path, int startid, int endid) static void addtoselbuf(char *path, int startid, int endid)
{ {
size_t len; size_t len, alloclen = 0;
size_t const pathlen = xstrlen(path);
struct entry *dentp; struct entry *dentp;
bool add = FALSE; char *found;
/* Remember current selection buffer position */ /* Remember current selection buffer position */
for (int i = startid; i <= endid; ++i) { for (int i = startid; i <= endid; ++i) {
dentp = &pdents[i]; dentp = &pdents[i];
len = mkpath(path, dentp->name, g_buf);
if (findselpos) { if (findselpos) {
if (dentp->flags & FILE_SCANNED) { len = mkpath(path, dentp->name, g_buf);
if (!(dentp->flags & FILE_SELECTED)) found = findinsel(findselpos, len);
add = TRUE; if (found) {
dentp->flags |= (FILE_SCANNED | FILE_SELECTED);
if (found == findselpos) {
findselpos += len;
if (findselpos == (pselbuf + selbufpos))
findselpos = NULL;
}
} else } else
add = !findinsel(findselpos, len); alloclen += pathlen + dentp->nlen;
} else } else
add = TRUE; alloclen += pathlen + dentp->nlen;
}
if (add) { pselbuf = xrealloc(pselbuf, selbuflen + alloclen);
/* Write the path to selection file to avoid flush */ if (!pselbuf)
if (appendfpath(g_buf, len) && findselpos) errexit();
scanselforpath(path);
for (int i = startid; i <= endid; ++i) {
if (!(pdents[i].flags & FILE_SELECTED)) {
len = mkpath(path, pdents[i].name, g_buf);
appendfpath(g_buf, len);
++nselected; ++nselected;
add = FALSE; pdents[i].flags |= (FILE_SCANNED | FILE_SELECTED);
} }
dentp->flags |= (FILE_SCANNED | FILE_SELECTED);
} }
writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */ writesel(pselbuf, selbufpos - 1); /* Truncate NULL from end */