1
0
Fork 0

07: C: Misc speedups

This commit is contained in:
Mia Herkt 2022-12-08 04:27:28 +01:00
parent 824fe43fe6
commit c09be6c757
Signed by: mia
GPG key ID: 72E154B8622EC191

View file

@ -25,8 +25,8 @@ typedef struct entry {
struct entry *parent;
} entry;
entry *findent(entry *e, const char *name) {
uint32_t id = fnv1a_32(name, strchr(name, '\n') - name);
entry *findent(entry *e, const char *name, size_t nlen) {
uint32_t id = fnv1a_32(name, nlen);
for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->id == id)
return e->entries[i];
@ -35,9 +35,9 @@ entry *findent(entry *e, const char *name) {
return NULL;
}
entry *addent(entry *parent, const char *name) {
entry *addent(entry *parent, const char *name, size_t nlen) {
if (parent->n_entries >= parent->n_alloc) {
parent->n_alloc += 10;
parent->n_alloc += 5;
if (parent->n_entries)
parent->entries = realloc(parent->entries, parent->n_alloc * sizeof(entry*));
else
@ -46,7 +46,7 @@ entry *addent(entry *parent, const char *name) {
parent->n_entries++;
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, strchr(name, '\n') - name);
e->id = fnv1a_32(name, nlen);
e->parent = parent;
return e;
@ -68,34 +68,41 @@ int main(int argc, char **argv) {
if (in == MAP_FAILED) return 1;
char *l = in;
while (l - in < s.st_size) {
if (l[0] == '$') {
while (l != in + s.st_size) {
char *nl = strchr(l, '\n');
if (l[0] == 'd') {
l += 4;
addent(current, l, nl - l);
} else if (l[0] == '$') {
if (l[2] == 'c') {
if (l[5] == '/')
current = root;
else if (l[5] == '.')
else if (l[5] == '.') {
if (current->parent != root)
current->parent->size += current->size;
current = current->parent;
else
current = findent(current, l + 5);
} else {
l += 5;
current = findent(current, l, nl - l);
}
}
} else if (l[0] == 'd') {
addent(current, l + 4);
} else {
size_t sz = 0;
while (isdigit(*l)) sz = 10 * sz + (*l++ - '0');
entry *f = current;
for (; f->parent; f = f->parent)
f->size += sz;
f->size += sz;
do { sz = 10 * sz + (*l++ - '0'); } while (*l != ' ');
current->size += sz;
}
l = strchr(l, '\n') + 1;
l = nl + 1;
}
current = root;
for (size_t i = 0; i < root->n_entries; i++)
root->size += root->entries[i]->size;
if (argc == 4)
required = strtoul(argv[3], NULL, 10) - (strtoul(argv[2], NULL, 10) - root->size);
else
required = 30000000UL - (70000000UL - root->size);
current = root;
do {
if (current->n_entries) {
current->n_entries--;