Skip to content

Commit

Permalink
++perf in reading: 2x improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ikozyris committed Jun 1, 2024
1 parent 3360e94 commit 14c87b0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion headers/gapbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void mv_curs(gap_buf &a, const unsigned pos)
if (pos > a.gps) // move gap to right
memmove(a.buffer + a.gps, a.buffer + a.gps + gpl, pos - a.gps);
else if (pos < a.gps) // move gap to left
memmove(a.buffer + pos + gpl, a.buffer + pos, a.gps-pos);
memmove(a.buffer + pos + gpl, a.buffer + pos, a.gps - pos);
if (pos >= a.len)
a.gpe = a.cpt;
else
Expand Down
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ int main(int argc, char *argv[])
print2header("Read-Only", 3);
goto ro;
} else {
read_getc(fi);
read_fread_b(fi);
//read_getc(fi);
print_text();
}
//read_fgets(fi);
Expand Down
2 changes: 1 addition & 1 deletion utils/init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sizes.c"

#define name "Yocto 0.8-beta2"
#define name "Yocto 0.8-beta3"

void init_curses()
{
Expand Down
60 changes: 36 additions & 24 deletions utils/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,56 @@ unsigned print_text_w(unsigned start)
return buf_indx - start; // how many characters were printed
}

inline void read_getc(FILE *fi)
{
while ((ch = getc_unlocked(fi)) != EOF) {
it->buffer[it->len++] = ch;
if (it->len >= it->cpt) { [[unlikely]]
it->cpt *= 2;
it->buffer = (char*)realloc(it->buffer, it->cpt);
} if (ch == '\n') { [[unlikely]]
it->gps = it->len;
it->gpe = it->cpt;
if (++curnum >= txt_cpt) { [[unlikely]]
txt_cpt = text.size() * 2;
text.resize(txt_cpt);
}
++it;
inline void ins_b(char ch) {
it->buffer[it->len++] = ch;
if (it->len >= it->cpt) { [[unlikely]]
it->cpt *= 2;
it->buffer = (char*)realloc(it->buffer, it->cpt);
} if (ch == '\n') { [[unlikely]]
it->gps = it->len;
it->gpe = it->cpt;
if (++curnum >= txt_cpt) { [[unlikely]]
txt_cpt = text.size() * 2;
text.resize(txt_cpt);
}
++it;
}
}

#define sz 65536
void read_getc(FILE *fi)
{
while ((ch = getc_unlocked(fi)) != EOF)
ins_b(ch);
}

// For size see: https://github.com/ikozyris/yocto/wiki/Comments-on-optimizations#buffer-size-for-reading
#define SZ 524288 // 512 KB

inline void read_fgets(FILE *fi)
void read_fgets(FILE *fi)
{
char tmp[sz + 2];
while ((fgets_unlocked(tmp, sz, fi))) {
char tmp[SZ];
while ((fgets_unlocked(tmp, SZ - 2, fi))) {
apnd_s(*it, tmp);
if (tmp[sz - 1] == 0) { [[unlikely]]
if (tmp[SZ - 3] == 0) { [[unlikely]]
++curnum;
++it;
}
}
}

inline void read_fread(FILE *fi)
void read_fread(FILE *fi)
{
char tmp[sz];
char tmp[SZ];
unsigned a = 0; // bytes read
while ((a = fread(tmp, sizeof(tmp[0]), sz, fi))) {
while ((a = fread(tmp, sizeof(tmp[0]), SZ, fi)))
apnd_s(*it, tmp, a);
}
}

void read_fread_b(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]);
}

0 comments on commit 14c87b0

Please sign in to comment.