Skip to content

Commit

Permalink
perf: 2.5x improvement in reading file (normal mode)
Browse files Browse the repository at this point in the history
Rewritten reading function, yet there is always room for optimizations.
  • Loading branch information
ikozyris committed Jul 14, 2024
1 parent c01c594 commit 3edb996
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions utils/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ inline void ins_b(char ch)
}
}

inline long whereis(const char *str, char ch)
{
const char *end = strchr(str, ch);
if (end == 0)
return -1;
return end - str + 1;
}

// read in single line
void read_fread_sl(FILE *fi)
{
Expand All @@ -140,11 +148,22 @@ void read_fread_sl(FILE *fi)
void read_fread(FILE *fi)
{
char tmp[SZ];
unsigned a;
while ((a = fread(tmp, sizeof(tmp[0]), SZ, fi)))
for (unsigned i = 0; i < a; ++i)
ins_b(tmp[i]);
// if last line does not have a newline, gap does not get updated
it->gps = it->len;
it->gpe = it->cpt;
unsigned a, j = 0;
while ((a = fread(tmp, sizeof(tmp[0]), SZ, fi))) {
long res = whereis(tmp, '\n');
if (res > 0) { // found newline
while ((res = whereis(tmp + j, '\n')) > -1) {
apnd_s(*it, tmp + j, res);
j += res;
if (++curnum >= txt_cpt) {
txt_cpt *= 2;
text.resize(txt_cpt);
}
++it;
}
}
// if last character is not a newline
apnd_s(*it, tmp + j, a - j);
j = 0;
}
}

0 comments on commit 3edb996

Please sign in to comment.