Browse code

Set sensible initial sizes for growing arrays

Any values should work here, and it doesn't seem to make a siginificant
difference to startup time, as this only adds or reduces a few realloc
calls.

Starts input buffer at 4k of memory, which is nice for being the size of
a page and the size usually used in the read syscall. Initial choice
capacity set to 128, by assuming that candidates average 32 characters
long.

John Hawthorn authored on 24/04/2016 20:37:36
Showing 1 changed files

... ...
@@ -5,7 +5,11 @@
5 5
 #include "choices.h"
6 6
 #include "match.h"
7 7
 
8
-#define INITIAL_CAPACITY 1
8
+/* Initial size of buffer for storing input in memory */
9
+#define INITIAL_BUFFER_SIZE     4096
10
+
11
+/* Initial size of choices array */
12
+#define INITIAL_CHOICE_CAPACITY 128
9 13
 
10 14
 static int cmpchoice(const void *_idx1, const void *_idx2) {
11 15
 	const struct scored_result *a = _idx1;
... ...
@@ -30,7 +34,7 @@ static void *safe_realloc(void *buffer, size_t size) {
30 34
 }
31 35
 
32 36
 void choices_fread(choices_t *c, FILE *file) {
33
-	size_t bufsize = 65536, pos = 0;
37
+	size_t bufsize = INITIAL_BUFFER_SIZE, pos = 0;
34 38
 	char *buf = safe_realloc(NULL, bufsize);
35 39
 
36 40
 	/* Continue reading until we get a "short" read, indicating EOF */
... ...
@@ -76,7 +80,7 @@ void choices_init(choices_t *c) {
76 80
 	c->results = NULL;
77 81
 	c->capacity = c->size = 0;
78 82
 	choices_reset_search(c);
79
-	choices_resize(c, INITIAL_CAPACITY);
83
+	choices_resize(c, INITIAL_CHOICE_CAPACITY);
80 84
 }
81 85
 
82 86
 void choices_free(choices_t *c) {