@matheusbsbs / matheus-bagliano / commits / 10831c5513
Fork 0

Reduce memory usage of fast-import.

Some structs are allocated rather frequently, but were using integer types which were far larger than required to actually store their full value range. As packfiles are limited to 4 GiB we don't need more than 32 bits to store the offset of an object within that packfile, an `unsigned long` on a 64 bit system is likely a 64 bit unsigned value. Saving 4 bytes per object on a 64 bit system can add up fast on any sizable import. As atom strings are strictly single components in a path name these are probably limited to just 255 bytes by the underlying OS. Going to that short of a string is probably too restrictive, but certainly `unsigned int` is far too large for their lengths. `unsigned short` is a reasonable limit. Modes within a tree really only need two bytes to store their whole value; using `unsigned int` here is vast overkill. Saving 4 bytes per file entry in an active branch can add up quickly on a project with a large number of files. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Feb 5, 2007 at 16:34 UTC 10831c551323121bdab06c3eaf2f52c6658fd6b8
1 file changed +12 -12
fast-import.c
+12 -12
@@ -130,7 +130,7 @@ Format of STDIN stream:
130 struct object_entry
131 {
132 struct object_entry *next;
133 - unsigned long offset;
133 + uint32_t offset;
134 unsigned type : TYPE_BITS;
135 unsigned pack_id : PACK_ID_BITS;
136 unsigned char sha1[20];
@@ -157,7 +157,7 @@ struct last_object
157 {
158 void *data;
159 unsigned long len;
160 - unsigned long offset;
160 + uint32_t offset;
161 unsigned int depth;
162 unsigned no_free:1;
163 };
@@ -173,7 +173,7 @@ struct mem_pool
173 struct atom_str
174 {
175 struct atom_str *next_atom;
176 - unsigned int str_len;
176 + unsigned short str_len;
177 char str_dat[FLEX_ARRAY]; /* more */
178 };
179
@@ -184,7 +184,7 @@ struct tree_entry
184 struct atom_str* name;
185 struct tree_entry_ms
186 {
187 - unsigned int mode;
187 + uint16_t mode;
188 unsigned char sha1[20];
189 } versions[2];
190 };
@@ -464,7 +464,7 @@ static struct object_entry* find_mark(uintmax_t idnum)
464 return oe;
465 }
466
467 -static struct atom_str* to_atom(const char *s, size_t len)
467 +static struct atom_str* to_atom(const char *s, unsigned short len)
468 {
469 unsigned int hc = hc_str(s, len) % atom_table_sz;
470 struct atom_str *c;
@@ -993,10 +993,10 @@ static void *gfi_unpack_entry(
993 return unpack_entry(p, oe->offset, type, sizep);
994 }
995
996 -static const char *get_mode(const char *str, unsigned int *modep)
996 +static const char *get_mode(const char *str, uint16_t *modep)
997 {
998 unsigned char c;
999 - unsigned int mode = 0;
999 + uint16_t mode = 0;
1000
1001 while ((c = *str++) != ' ') {
1002 if (c < '0' || c > '7')
@@ -1046,7 +1046,7 @@ static void load_tree(struct tree_entry *root)
1046 if (!c)
1047 die("Corrupt mode in %s", sha1_to_hex(sha1));
1048 e->versions[0].mode = e->versions[1].mode;
1049 - e->name = to_atom(c, strlen(c));
1049 + e->name = to_atom(c, (unsigned short)strlen(c));
1050 c += e->name->str_len + 1;
1051 hashcpy(e->versions[0].sha1, (unsigned char*)c);
1052 hashcpy(e->versions[1].sha1, (unsigned char*)c);
@@ -1098,7 +1098,7 @@ static void mktree(struct tree_content *t,
1098 struct tree_entry *e = t->entries[i];
1099 if (!e->versions[v].mode)
1100 continue;
1101 - c += sprintf(c, "%o", e->versions[v].mode);
1101 + c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1102 *c++ = ' ';
1103 strcpy(c, e->name->str_dat);
1104 c += e->name->str_len + 1;
@@ -1161,7 +1161,7 @@ static int tree_content_set(
1161 struct tree_entry *root,
1162 const char *p,
1163 const unsigned char *sha1,
1164 - const unsigned int mode)
1164 + const uint16_t mode)
1165 {
1166 struct tree_content *t = root->tree;
1167 const char *slash1;
@@ -1207,7 +1207,7 @@ static int tree_content_set(
1207 if (t->entry_count == t->entry_capacity)
1208 root->tree = t = grow_tree_content(t, 8);
1209 e = new_tree_entry();
1210 - e->name = to_atom(p, n);
1210 + e->name = to_atom(p, (unsigned short)n);
1211 e->versions[0].mode = 0;
1212 hashclr(e->versions[0].sha1);
1213 t->entries[t->entry_count++] = e;
@@ -1458,7 +1458,7 @@ static void file_change_m(struct branch *b)
1458 const char *endp;
1459 struct object_entry *oe;
1460 unsigned char sha1[20];
1461 - unsigned int mode, inline_data = 0;
1461 + uint16_t mode, inline_data = 0;
1462 char type[20];
1463
1464 p = get_mode(p, &mode);