]> git.proxmox.com Git - libgit2.git/blob - src/oid.c
oid: optimize git_oid_fromstrn by using memset
[libgit2.git] / src / oid.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9 #include "git2/oid.h"
10 #include "repository.h"
11 #include <string.h>
12 #include <limits.h>
13
14 static signed char from_hex[] = {
15 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
16 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
17 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
18 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
19 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
21 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
22 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
24 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
31 };
32 static char to_hex[] = "0123456789abcdef";
33
34 int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
35 {
36 size_t p;
37 int v;
38
39 if (length > GIT_OID_HEXSZ)
40 length = GIT_OID_HEXSZ;
41
42 for (p = 0; p < length - 1; p += 2) {
43 v = (from_hex[(unsigned char)str[p + 0]] << 4)
44 | from_hex[(unsigned char)str[p + 1]];
45
46 if (v < 0)
47 return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
48
49 out->id[p / 2] = (unsigned char)v;
50 }
51
52 if (length % 2) {
53 v = (from_hex[(unsigned char)str[p + 0]] << 4);
54 out->id[p / 2] = (unsigned char)v;
55 p += 2;
56 }
57
58 memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
59
60 return GIT_SUCCESS;
61 }
62
63 int git_oid_fromstr(git_oid *out, const char *str)
64 {
65 return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
66 }
67
68 GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
69 {
70 *str++ = to_hex[val >> 4];
71 *str++ = to_hex[val & 0xf];
72 return str;
73 }
74
75 void git_oid_fmt(char *str, const git_oid *oid)
76 {
77 size_t i;
78
79 for (i = 0; i < sizeof(oid->id); i++)
80 str = fmt_one(str, oid->id[i]);
81 }
82
83 void git_oid_pathfmt(char *str, const git_oid *oid)
84 {
85 size_t i;
86
87 str = fmt_one(str, oid->id[0]);
88 *str++ = '/';
89 for (i = 1; i < sizeof(oid->id); i++)
90 str = fmt_one(str, oid->id[i]);
91 }
92
93 char *git_oid_allocfmt(const git_oid *oid)
94 {
95 char *str = git__malloc(GIT_OID_HEXSZ + 1);
96 if (!str)
97 return NULL;
98 git_oid_fmt(str, oid);
99 str[GIT_OID_HEXSZ] = '\0';
100 return str;
101 }
102
103 char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
104 {
105 char str[GIT_OID_HEXSZ];
106
107 if (!out || n == 0 || !oid)
108 return "";
109
110 n--; /* allow room for terminating NUL */
111
112 if (n > 0) {
113 git_oid_fmt(str, oid);
114 if (n > GIT_OID_HEXSZ)
115 n = GIT_OID_HEXSZ;
116 memcpy(out, str, n);
117 }
118
119 out[n] = '\0';
120
121 return out;
122 }
123
124 int git_oid__parse(git_oid *oid, const char **buffer_out,
125 const char *buffer_end, const char *header)
126 {
127 const size_t sha_len = GIT_OID_HEXSZ;
128 const size_t header_len = strlen(header);
129
130 const char *buffer = *buffer_out;
131
132 if (buffer + (header_len + sha_len + 1) > buffer_end)
133 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer too small");
134
135 if (memcmp(buffer, header, header_len) != 0)
136 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer and header do not match");
137
138 if (buffer[header_len + sha_len] != '\n')
139 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer not terminated correctly");
140
141 if (git_oid_fromstr(oid, buffer + header_len) < GIT_SUCCESS)
142 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Failed to generate sha1");
143
144 *buffer_out = buffer + (header_len + sha_len + 1);
145
146 return GIT_SUCCESS;
147 }
148
149 void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
150 {
151 char hex_oid[GIT_OID_HEXSZ];
152
153 git_oid_fmt(hex_oid, oid);
154 git_buf_puts(buf, header);
155 git_buf_put(buf, hex_oid, GIT_OID_HEXSZ);
156 git_buf_putc(buf, '\n');
157 }
158
159 void git_oid_fromraw(git_oid *out, const unsigned char *raw)
160 {
161 memcpy(out->id, raw, sizeof(out->id));
162 }
163
164 void git_oid_cpy(git_oid *out, const git_oid *src)
165 {
166 memcpy(out->id, src->id, sizeof(out->id));
167 }
168
169 int git_oid_cmp(const git_oid *a, const git_oid *b)
170 {
171 return memcmp(a->id, b->id, sizeof(a->id));
172 }
173
174 int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len)
175 {
176 const unsigned char *a = oid_a->id;
177 const unsigned char *b = oid_b->id;
178
179 do {
180 if (*a != *b)
181 return 1;
182 a++;
183 b++;
184 len -= 2;
185 } while (len > 1);
186
187 if (len)
188 if ((*a ^ *b) & 0xf0)
189 return 1;
190
191 return 0;
192 }
193
194 typedef short node_index;
195
196 typedef union {
197 const char *tail;
198 node_index children[16];
199 } trie_node;
200
201 struct git_oid_shorten {
202 trie_node *nodes;
203 size_t node_count, size;
204 int min_length, full;
205 };
206
207 static int resize_trie(git_oid_shorten *self, size_t new_size)
208 {
209 self->nodes = realloc(self->nodes, new_size * sizeof(trie_node));
210 if (self->nodes == NULL)
211 return GIT_ENOMEM;
212
213 if (new_size > self->size) {
214 memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
215 }
216
217 self->size = new_size;
218 return GIT_SUCCESS;
219 }
220
221 static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
222 {
223 trie_node *node, *leaf;
224 node_index idx_leaf;
225
226 if (os->node_count >= os->size) {
227 if (resize_trie(os, os->size * 2) < GIT_SUCCESS)
228 return NULL;
229 }
230
231 idx_leaf = (node_index)os->node_count++;
232
233 if (os->node_count == SHRT_MAX)
234 os->full = 1;
235
236 node = &os->nodes[idx];
237 node->children[push_at] = -idx_leaf;
238
239 leaf = &os->nodes[idx_leaf];
240 leaf->tail = oid;
241
242 return node;
243 }
244
245 git_oid_shorten *git_oid_shorten_new(size_t min_length)
246 {
247 git_oid_shorten *os;
248
249 os = git__malloc(sizeof(git_oid_shorten));
250 if (os == NULL)
251 return NULL;
252
253 memset(os, 0x0, sizeof(git_oid_shorten));
254
255 if (resize_trie(os, 16) < GIT_SUCCESS) {
256 free(os);
257 return NULL;
258 }
259
260 os->node_count = 1;
261 os->min_length = min_length;
262
263 return os;
264 }
265
266 void git_oid_shorten_free(git_oid_shorten *os)
267 {
268 free(os->nodes);
269 free(os);
270 }
271
272
273 /*
274 * What wizardry is this?
275 *
276 * This is just a memory-optimized trie: basically a very fancy
277 * 16-ary tree, which is used to store the prefixes of the OID
278 * strings.
279 *
280 * Read more: http://en.wikipedia.org/wiki/Trie
281 *
282 * Magic that happens in this method:
283 *
284 * - Each node in the trie is an union, so it can work both as
285 * a normal node, or as a leaf.
286 *
287 * - Each normal node points to 16 children (one for each possible
288 * character in the oid). This is *not* stored in an array of
289 * pointers, because in a 64-bit arch this would be sucking
290 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
291 * insane. What we do is store Node Indexes, and use these indexes
292 * to look up each node in the om->index array. These indexes are
293 * signed shorts, so this limits the amount of unique OIDs that
294 * fit in the structure to about 20000 (assuming a more or less uniform
295 * distribution).
296 *
297 * - All the nodes in om->index array are stored contiguously in
298 * memory, and each of them is 32 bytes, so we fit 2x nodes per
299 * cache line. Convenient for speed.
300 *
301 * - To differentiate the leafs from the normal nodes, we store all
302 * the indexes towards a leaf as a negative index (indexes to normal
303 * nodes are positives). When we find that one of the children for
304 * a node has a negative value, that means it's going to be a leaf.
305 * This reduces the amount of indexes we have by two, but also reduces
306 * the size of each node by 1-4 bytes (the amount we would need to
307 * add a `is_leaf` field): this is good because it allows the nodes
308 * to fit cleanly in cache lines.
309 *
310 * - Once we reach an empty children, instead of continuing to insert
311 * new nodes for each remaining character of the OID, we store a pointer
312 * to the tail in the leaf; if the leaf is reached again, we turn it
313 * into a normal node and use the tail to create a new leaf.
314 *
315 * This is a pretty good balance between performance and memory usage.
316 */
317 int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
318 {
319 int i, is_leaf;
320 node_index idx;
321
322 if (os->full)
323 return GIT_ENOMEM;
324
325 if (text_oid == NULL)
326 return os->min_length;
327
328 idx = 0;
329 is_leaf = 0;
330
331 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
332 int c = from_hex[(int)text_oid[i]];
333 trie_node *node;
334
335 if (c == -1)
336 return git__throw(GIT_ENOTOID, "Failed to shorten OID. Invalid hex value");
337
338 node = &os->nodes[idx];
339
340 if (is_leaf) {
341 const char *tail;
342
343 tail = node->tail;
344 node->tail = NULL;
345
346 node = push_leaf(os, idx, from_hex[(int)tail[0]], &tail[1]);
347 if (node == NULL)
348 return GIT_ENOMEM;
349 }
350
351 if (node->children[c] == 0) {
352 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
353 return GIT_ENOMEM;
354 break;
355 }
356
357 idx = node->children[c];
358 is_leaf = 0;
359
360 if (idx < 0) {
361 node->children[c] = idx = -idx;
362 is_leaf = 1;
363 }
364 }
365
366 if (++i > os->min_length)
367 os->min_length = i;
368
369 return os->min_length;
370 }
371