@matheusbsbs / matheus-bagliano / commits / d2eafb7661
Fork 0

Add raw tree buffer info to "struct tree"

This allows us to avoid allocating information for names etc, because we can just use the information from the tree buffer directly. We still keep the old "tree_entry_list" in struct tree as well, so old users aren't affected, apart from the fact that the allocations are different (if you free a tree entry, you should no longer free the name allocation for it, since it's allocated as part of "tree->buffer") Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 28, 2006 at 15:07 UTC d2eafb76611670d7573f9409b0c727019fe50e18
6 files changed +36 -34
builtin-read-tree.c
+2 -2
@@ -38,7 +38,7 @@ static struct tree_entry_list df_conflict_list = {
38
39 typedef int (*merge_fn_t)(struct cache_entry **src);
40
41 -static int entcmp(char *name1, int dir1, char *name2, int dir2)
41 +static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
42 {
43 int len1 = strlen(name1);
44 int len2 = strlen(name2);
@@ -66,7 +66,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
66 int src_size = len + 1;
67 do {
68 int i;
69 - char *first;
69 + const char *first;
70 int firstdir = 0;
71 int pathlen;
72 unsigned ce_size;
builtin-rev-list.c
+2 -1
@@ -136,10 +136,11 @@ static struct object_list **process_tree(struct tree *tree,
136 p = process_tree(entry->item.tree, p, &me, entry->name);
137 else
138 p = process_blob(entry->item.blob, p, &me, entry->name);
139 - free(entry->name);
139 free(entry);
140 entry = next;
141 }
142 + free(tree->buffer);
143 + tree->buffer = NULL;
144 return p;
145 }
146
fsck-objects.c
+3 -4
@@ -197,17 +197,16 @@ static int fsck_tree(struct tree *item)
197 default:
198 break;
199 }
200 - free(last->name);
200 free(last);
201 }
202
203 last = entry;
204 }
206 - if (last) {
207 - free(last->name);
205 + if (last)
206 free(last);
209 - }
207 item->entries = NULL;
208 + free(item->buffer);
209 + item->buffer = NULL;
210
211 retval = 0;
212 if (has_full_path) {
object.c
+4 -1
@@ -200,8 +200,11 @@ struct object *parse_object(const unsigned char *sha1)
200 obj = &blob->object;
201 } else if (!strcmp(type, tree_type)) {
202 struct tree *tree = lookup_tree(sha1);
203 - parse_tree_buffer(tree, buffer, size);
203 obj = &tree->object;
204 + if (!tree->object.parsed) {
205 + parse_tree_buffer(tree, buffer, size);
206 + buffer = NULL;
207 + }
208 } else if (!strcmp(type, commit_type)) {
209 struct commit *commit = lookup_commit(sha1);
210 parse_commit_buffer(commit, buffer, size);
tree.c
+22 -25
@@ -3,6 +3,7 @@
3 #include "blob.h"
4 #include "commit.h"
5 #include "tag.h"
6 +#include "tree-walk.h"
7 #include <stdlib.h>
8
9 const char *tree_type = "tree";
@@ -145,46 +146,45 @@ struct tree *lookup_tree(const unsigned char *sha1)
146
147 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
148 {
148 - void *bufptr = buffer;
149 + struct tree_desc desc;
150 struct tree_entry_list **list_p;
151 int n_refs = 0;
152
153 if (item->object.parsed)
154 return 0;
155 item->object.parsed = 1;
156 + item->buffer = buffer;
157 + item->size = size;
158 +
159 + desc.buf = buffer;
160 + desc.size = size;
161 +
162 list_p = &item->entries;
156 - while (size) {
157 - struct object *obj;
163 + while (desc.size) {
164 + unsigned mode;
165 + const char *path;
166 + const unsigned char *sha1;
167 struct tree_entry_list *entry;
159 - int len = 1+strlen(bufptr);
160 - unsigned char *file_sha1 = bufptr + len;
161 - char *path = strchr(bufptr, ' ');
162 - unsigned int mode;
163 - if (size < len + 20 || !path ||
164 - sscanf(bufptr, "%o", &mode) != 1)
165 - return -1;
168 +
169 + sha1 = tree_entry_extract(&desc, &path, &mode);
170
171 entry = xmalloc(sizeof(struct tree_entry_list));
168 - entry->name = strdup(path + 1);
172 + entry->name = path;
173 + entry->mode = mode;
174 entry->directory = S_ISDIR(mode) != 0;
175 entry->executable = (mode & S_IXUSR) != 0;
176 entry->symlink = S_ISLNK(mode) != 0;
172 - entry->zeropad = *(char *)bufptr == '0';
173 - entry->mode = mode;
177 + entry->zeropad = *(const char *)(desc.buf) == '0';
178 entry->next = NULL;
179
176 - bufptr += len + 20;
177 - size -= len + 20;
180 + update_tree_entry(&desc);
181
182 if (entry->directory) {
180 - entry->item.tree = lookup_tree(file_sha1);
181 - obj = &entry->item.tree->object;
183 + entry->item.tree = lookup_tree(sha1);
184 } else {
183 - entry->item.blob = lookup_blob(file_sha1);
184 - obj = &entry->item.blob->object;
185 + entry->item.blob = lookup_blob(sha1);
186 }
186 - if (obj)
187 - n_refs++;
187 + n_refs++;
188 *list_p = entry;
189 list_p = &entry->next;
190 }
@@ -206,7 +206,6 @@ int parse_tree(struct tree *item)
206 char type[20];
207 void *buffer;
208 unsigned long size;
209 - int ret;
209
210 if (item->object.parsed)
211 return 0;
@@ -219,9 +218,7 @@ int parse_tree(struct tree *item)
218 return error("Object %s not a tree",
219 sha1_to_hex(item->object.sha1));
220 }
222 - ret = parse_tree_buffer(item, buffer, size);
223 - free(buffer);
224 - return ret;
221 + return parse_tree_buffer(item, buffer, size);
222 }
223
224 struct tree *parse_tree_indirect(const unsigned char *sha1)
tree.h
+3 -1
@@ -12,7 +12,7 @@ struct tree_entry_list {
12 unsigned symlink : 1;
13 unsigned zeropad : 1;
14 unsigned int mode;
15 - char *name;
15 + const char *name;
16 union {
17 struct object *any;
18 struct tree *tree;
@@ -22,6 +22,8 @@ struct tree_entry_list {
22
23 struct tree {
24 struct object object;
25 + void *buffer;
26 + unsigned long size;
27 struct tree_entry_list *entries;
28 };
29