1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Mia Herkt 9ca9c342c2
07: C: Faster number conversion 2022-12-07 23:51:57 +01:00
Mia Herkt 87dd0adf8e
07: C: Use mmap instead of fgets 2022-12-07 23:51:13 +01:00
1 changed files with 22 additions and 6 deletions

View File

@ -2,6 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <ctype.h>
uint32_t fnv1a_32(const char *buf, size_t len) uint32_t fnv1a_32(const char *buf, size_t len)
{ {
@ -22,7 +26,7 @@ typedef struct entry {
} entry; } entry;
entry *findent(entry *e, const char *name) { entry *findent(entry *e, const char *name) {
uint32_t id = fnv1a_32(name, strlen(name)); uint32_t id = fnv1a_32(name, strchr(name, '\n') - name);
for (size_t i = 0; i < e->n_entries; i++) { for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->id == id) if (e->entries[i]->id == id)
return e->entries[i]; return e->entries[i];
@ -42,19 +46,29 @@ entry *addent(entry *parent, const char *name) {
parent->n_entries++; parent->n_entries++;
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry)); entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, strlen(name)); e->id = fnv1a_32(name, strchr(name, '\n') - name);
e->parent = parent; e->parent = parent;
return e; return e;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
char l[64]; if (argc < 2) return 1;
entry *root = calloc(1, sizeof(entry)); entry *root = calloc(1, sizeof(entry));
entry *current = root; entry *current = root;
size_t S = 0, G = 0, required = 0; size_t S = 0, G = 0, required = 0;
while (fgets(l, 64, stdin)) { struct stat s;
if (stat(argv[1], &s) == -1) return 1;
int fd = open(argv[1], O_RDONLY);
if (fd == -1) return 1;
char *in = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (in == MAP_FAILED) return 1;
char *l = in;
while (l - in < s.st_size) {
if (l[0] == '$') { if (l[0] == '$') {
if (l[2] == 'c') { if (l[2] == 'c') {
if (l[5] == '/') if (l[5] == '/')
@ -67,16 +81,18 @@ int main(int argc, char **argv) {
} else if (l[0] == 'd') { } else if (l[0] == 'd') {
addent(current, l + 4); addent(current, l + 4);
} else { } else {
size_t sz = strtoul(l, NULL, 10); size_t sz = 0;
while (isdigit(*l)) sz = 10 * sz + (*l++ - '0');
entry *f = current; entry *f = current;
for (; f->parent; f = f->parent) for (; f->parent; f = f->parent)
f->size += sz; f->size += sz;
f->size += sz; f->size += sz;
} }
l = strchr(l, '\n') + 1;
} }
current = root; current = root;
if (argc == 3) if (argc == 4)
required = strtoul(argv[2], NULL, 10) - (strtoul(argv[1], NULL, 10) - root->size); required = strtoul(argv[2], NULL, 10) - (strtoul(argv[1], NULL, 10) - root->size);
else else
required = 30000000UL - (70000000UL - root->size); required = 30000000UL - (70000000UL - root->size);