Skip to content

Commit

Permalink
Pass length to cJSON and improve status printout
Browse files Browse the repository at this point in the history
Made human_file_size() slightly nicer, it can now take an optional
stack buffer to skip a dynamic allocation.
I observed that we already know the length of the JSON payload, and I
was just needlessly having cJSON do another strlen() on it, so this
saves a bit of time.
  • Loading branch information
vkoskiv committed Nov 16, 2023
1 parent 03fe914 commit 30f9b07
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ int main(int argc, char *argv[]) {
ret = -1;
goto done;
}
logr(info, "%zi bytes of input JSON loaded from %s, parsing.\n", bytes, args_is_set(opts, "inputFile") ? "file" : "stdin");
cJSON *scene = cJSON_Parse(input);
char size_buf[64];
logr(info, "%s of input JSON loaded from %s, parsing.\n", human_file_size(bytes, size_buf), args_is_set(opts, "inputFile") ? "file" : "stdin");
struct timeval json_timer;
timer_start(&json_timer);
cJSON *scene = cJSON_ParseWithLength(input, bytes);
size_t json_ms = timer_get_ms(json_timer);
if (!scene) {
const char *errptr = cJSON_GetErrorPtr();
if (errptr) {
Expand All @@ -103,6 +107,7 @@ int main(int argc, char *argv[]) {
goto done;
}
}
logr(info, "JSON parse took %lums\n", json_ms);

//FIXME: mmap() input
free(input);
Expand Down
7 changes: 3 additions & 4 deletions src/utils/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void write_file(const unsigned char *buf, size_t bufsize, const char *filePath)
//We determine the file size after saving, because the lodePNG library doesn't have a way to tell the compressed file size
//This will work for all image formats
unsigned long bytes = get_file_size(backupPath ? backupPath : filePath);
char *sizeString = human_file_size(bytes);
char *sizeString = human_file_size(bytes, NULL);
logr(info, "Wrote %s to file.\n", sizeString);
free(sizeString);
}
Expand Down Expand Up @@ -241,8 +241,7 @@ char *read_stdin(size_t *bytes) {
return buf;
}

// FIXME: Have this take a buffer instead of allocating it in here
char *human_file_size(unsigned long bytes) {
char *human_file_size(unsigned long bytes, char *stat_buf) {
double kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, yottabytes; // <- Futureproofing?!
kilobytes = bytes / 1000.0;
megabytes = kilobytes / 1000.0;
Expand All @@ -258,7 +257,7 @@ char *human_file_size(unsigned long bytes) {
// I *did* get it to go to yottabytes using __uint128_t, but that's
// not in C99. Maybe in the future.

char *buf = calloc(64, sizeof(*buf));
char *buf = stat_buf ? stat_buf : calloc(64, sizeof(*buf));

if (zettabytes >= 1000) {
sprintf(buf, "%.02fYB", yottabytes);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum fileType {
};

enum fileType guess_file_type(const char *filePath);
char *human_file_size(unsigned long bytes);
char *human_file_size(unsigned long bytes, char *stat_buf);
char *load_file(const char *filePath, size_t *bytes, struct file_cache *cache);
// This is a more robust file writing function, that will seek alternate directories
// if the specified one wasn't writeable.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/loaders/textureloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static struct texture *load_env_map(unsigned char *buf, size_t buflen, const cha
logr(warning, "Error while decoding HDR from %s - Corrupted?\n", path);
return NULL;
}
char *fsbuf = human_file_size(buflen);
char *fsbuf = human_file_size(buflen, NULL);
printf(" %s\n", fsbuf);
free(fsbuf);
return tex;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void *allocBlock(struct block **head, size_t size) {
if ((*head)->size + size > (*head)->capacity) {
// Need to add a new block
size_t nextSize = (*head)->capacity > size ? (*head)->capacity : size;
char *new_size = human_file_size(nextSize);
char *prev_size = human_file_size((*head)->size);
char *new_size = human_file_size(nextSize, NULL);
char *prev_size = human_file_size((*head)->size, NULL);
logr(debug, "Appending a new block of size %s. Previous head occupancy: %s\n", new_size, prev_size);
free(new_size);
free(prev_size);
Expand All @@ -58,7 +58,7 @@ void destroyBlocks(struct block *head) {
numDestroyed++;
head = prev;
}
char *size = human_file_size(bytesfreed);
char *size = human_file_size(bytesfreed, NULL);
logr(debug, "Destroyed %lu blocks, %s\n", numDestroyed, size);
free(size);
}
2 changes: 1 addition & 1 deletion src/utils/protocol/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ struct renderClient *syncWithClients(const struct renderer *r, size_t *count) {
char *assetCache = cache_encode(r->state.file_cache);

size_t transfer_bytes = strlen(assetCache) + strlen(r->sceneCache);
char *transfer_size = human_file_size(transfer_bytes);
char *transfer_size = human_file_size(transfer_bytes, NULL);
logr(info, "Sending %s to %lu client%s...\n", transfer_size, clientCount, PLURAL(clientCount));
free(transfer_size);

Expand Down
10 changes: 5 additions & 5 deletions tests/test_fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ bool fileio_humanFileSize(void) {

char *humanSize = NULL;

humanSize = human_file_size(600);
humanSize = human_file_size(600, NULL);
test_assert(stringEquals(humanSize, "600B"));
free(humanSize);

humanSize = human_file_size(1000);
humanSize = human_file_size(1000, NULL);
test_assert(stringEquals(humanSize, "1.00kB"));
free(humanSize);

humanSize = human_file_size(1000 * 1000);
humanSize = human_file_size(1000 * 1000, NULL);
test_assert(stringEquals(humanSize, "1.00MB"));
free(humanSize);

humanSize = human_file_size(1000 * 1000 * 1000);
humanSize = human_file_size(1000 * 1000 * 1000, NULL);
test_assert(stringEquals(humanSize, "1.00GB"));
free(humanSize);

humanSize = human_file_size((unsigned long)1000 * 1000 * 1000 * 1000);
humanSize = human_file_size((unsigned long)1000 * 1000 * 1000 * 1000, NULL);
test_assert(stringEquals(humanSize, "1.00TB"));
free(humanSize);

Expand Down

0 comments on commit 30f9b07

Please sign in to comment.