Artifactspublic-v0

curl

master
ply4 git calls688 ms291 KiB2 cachedprotocol v2
510 lines13 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
26#include <stddef.h> /* for offsetof() */
28#include "hash.h"
29#include "rand.h"
31/* random patterns for API verification */
32#ifdef DEBUGBUILD
33#define HASHINIT 0x7017e781
34#define ITERINIT 0x5FEDCBA9
35#endif
38typedef size_t (*hash_function)(const void *key,
39 size_t key_length,
40 size_t slots_num);
42typedef size_t (*comp_function)(const void *key1,
43 size_t key1_len,
44 const void *key2,
45 size_t key2_len);
47/* Curl_hash stores its type in a uint8_t. */
48typedef char hash_types_fit_uint8[
49 ((CURL_HASH_TYPE_LAST - 1) <= UINT8_MAX) ? 1 : -1];
51struct hash_functions {
52 hash_function hash;
53 comp_function compare;
54};
56/* set at global init, read-only afterwards */
57static size_t hash_seed;
59CURLcode Curl_hash_global_init(void)
61 static bool seeded;
62 unsigned char buf[sizeof(size_t)];
63 size_t seed = 0;
64 size_t i;
65 CURLcode result;
67 if(seeded)
68 return CURLE_OK;
70 result = Curl_rand_bytes(NULL,
71#ifdef DEBUGBUILD
72 FALSE,
73#endif
74 buf, sizeof(buf));
75 if(result)
76 return result;
78 for(i = 0; i < sizeof(buf); i++)
79 seed = (seed << 8) | buf[i];
81 hash_seed = seed ? seed : 1;
82 seeded = TRUE;
83 return CURLE_OK;
86/* @unittest 1603
87 */
88UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
89 size_t slots_num, size_t seed);
90UNITTEST size_t hash_str_seeded(const void *key, size_t key_length,
91 size_t slots_num, size_t seed)
93 const char *key_str = (const char *)key;
94 const char *end = key_str + key_length;
95 size_t h = 5381 ^ seed;
97 while(key_str < end) {
98 size_t j = (size_t)*key_str++;
99 h += h << 5;
100 h ^= j;
101 }
103 return (h % slots_num);
106/* @unittest 1603
107 */
108UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num);
109UNITTEST size_t hash_str(const void *key, size_t key_length, size_t slots_num)
111 return hash_str_seeded(key, key_length, slots_num, hash_seed);
114/* Avoid treating the variable-length key[1] as a one-byte object. */
115static char *hash_elem_key(struct Curl_hash_element *he)
117 return (char *)he + offsetof(struct Curl_hash_element, key);
120static size_t hash_socket(const void *key, size_t key_length,
121 size_t slots_num)
123 curl_socket_t socket;
125 DEBUGASSERT(key_length == sizeof(socket));
126 if(key_length != sizeof(socket))
127 return 0;
128 memcpy(&socket, key, sizeof(socket));
129 return (size_t)(socket % (curl_socket_t)slots_num);
132static size_t compare_socket(const void *key1, size_t key1_len,
133 const void *key2, size_t key2_len)
135 curl_socket_t socket1;
136 curl_socket_t socket2;
138 if((key1_len != sizeof(socket1)) || (key2_len != sizeof(socket2)))
139 return 0;
140 memcpy(&socket1, key1, sizeof(socket1));
141 memcpy(&socket2, key2, sizeof(socket2));
142 return socket1 == socket2;
145static size_t compare_bytes(const void *key1, size_t key1_len,
146 const void *key2, size_t key2_len)
148 return (key1_len == key2_len) && !memcmp(key1, key2, key1_len);
151static const struct hash_functions hash_functions[] = {
152 { hash_str, compare_bytes },
153 { hash_socket, compare_socket }
154};
156static const struct hash_functions *hash_get_functions(uint8_t type)
158 DEBUGASSERT(CURL_ARRAYSIZE(hash_functions) == CURL_HASH_TYPE_LAST);
159 if((unsigned int)type >= CURL_HASH_TYPE_LAST)
160 type = CURL_HASH_TYPE_BYTES;
161 return &hash_functions[type];
164static bool hash_key_is_valid(uint8_t type, size_t key_len)
166 return (type == CURL_HASH_TYPE_BYTES) ||
167 ((type == CURL_HASH_TYPE_SOCKET) &&
168 (key_len == sizeof(curl_socket_t)));
171#if 0 /* useful function for debugging hashes and their contents */
172static struct Curl_hash_element *hash_next_element(
173 struct Curl_hash_iterator *iter)
175 struct Curl_hash *h;
176 DEBUGASSERT(iter->init == ITERINIT);
177 h = iter->hash;
178 if(!h->table)
179 return NULL; /* empty hash, nothing to return */
181 /* Get the next element in the current list, if any */
182 if(iter->current)
183 iter->current = iter->current->next;
185 /* If we have reached the end of the list, find the next one */
186 if(!iter->current) {
187 size_t i;
188 for(i = iter->slot_index; i < h->slots; i++) {
189 if(h->table[i]) {
190 iter->current = h->table[i];
191 iter->slot_index = i + 1;
192 break;
193 }
194 }
195 }
197 return iter->current;
200static void hash_start_iterate(struct Curl_hash *hash,
201 struct Curl_hash_iterator *iter)
203 DEBUGASSERT(hash->init == HASHINIT);
204 iter->hash = hash;
205 iter->slot_index = 0;
206 iter->current = NULL;
207#ifdef DEBUGBUILD
208 iter->init = ITERINIT;
209#endif
212void Curl_hash_print(struct Curl_hash *h, void (*func)(void *))
214 struct Curl_hash_iterator iter;
215 struct Curl_hash_element *he;
216 size_t last_index = UINT_MAX;
218 if(!h)
219 return;
221 curl_mfprintf(stderr, "=Hash dump=\n");
223 hash_start_iterate(h, &iter);
225 he = hash_next_element(&iter);
226 while(he) {
227 if(iter.slot_index != last_index) {
228 curl_mfprintf(stderr, "index %d:", (int)iter.slot_index);
229 if(last_index != UINT_MAX) {
230 curl_mfprintf(stderr, "\n");
231 }
232 last_index = iter.slot_index;
233 }
235 if(func)
236 func(he->ptr);
237 else
238 curl_mfprintf(stderr, " [key=%.*s, he=%p, ptr=%p]",
239 (int)he->key_len, hash_elem_key(he),
240 (void *)he, (void *)he->ptr);
242 he = hash_next_element(&iter);
243 }
244 curl_mfprintf(stderr, "\n");
246#endif
248/* Initializes a hash structure.
249 *
250 * @unittest: 1602
251 * @unittest: 1603
252 */
253void Curl_hash_init(struct Curl_hash *h,
254 size_t slots,
255 Curl_hash_type type,
256 Curl_hash_dtor dtor)
258 DEBUGASSERT(h);
259 DEBUGASSERT(slots);
260 DEBUGASSERT((unsigned int)type < CURL_HASH_TYPE_LAST);
261 DEBUGASSERT(dtor);
263 h->table = NULL;
264 h->dtor = dtor;
265 h->size = 0;
266 h->slots = slots;
267 h->type = (uint8_t)type;
268#ifdef DEBUGBUILD
269 h->init = HASHINIT;
270#endif
273static struct Curl_hash_element *hash_elem_create(const void *key,
274 size_t key_len,
275 void *p,
276 Curl_hash_elem_dtor dtor)
278 struct Curl_hash_element *he;
280 /* allocate the struct plus memory after it to store the key */
281 he = curlx_malloc(sizeof(struct Curl_hash_element) + key_len);
282 if(he) {
283 he->next = NULL;
284 /* copy the key */
285 memcpy(hash_elem_key(he), key, key_len);
286 he->key_len = key_len;
287 he->ptr = p;
288 he->dtor = dtor;
289 }
290 return he;
293static void hash_elem_clear_ptr(struct Curl_hash *h,
294 struct Curl_hash_element *he)
296 DEBUGASSERT(h);
297 DEBUGASSERT(he);
298 if(he->ptr) {
299 if(he->dtor)
300 he->dtor(hash_elem_key(he), he->key_len, he->ptr);
301 else
302 h->dtor(he->ptr);
303 he->ptr = NULL;
304 }
307static void hash_elem_destroy(struct Curl_hash *h,
308 struct Curl_hash_element *he)
310 hash_elem_clear_ptr(h, he);
311 curlx_free(he);
314static void hash_elem_unlink(struct Curl_hash *h,
315 struct Curl_hash_element **he_anchor,
316 struct Curl_hash_element *he)
318 *he_anchor = he->next;
319 --h->size;
322static void hash_elem_link(struct Curl_hash *h,
323 struct Curl_hash_element **he_anchor,
324 struct Curl_hash_element *he)
326 he->next = *he_anchor;
327 *he_anchor = he;
328 ++h->size;
331void *Curl_hash_add2(struct Curl_hash *h,
332 const void *key, size_t key_len, void *p,
333 Curl_hash_elem_dtor dtor)
335 const struct hash_functions *functions;
336 struct Curl_hash_element *he, **slot;
338 DEBUGASSERT(h);
339 DEBUGASSERT(h->slots);
340 DEBUGASSERT(h->init == HASHINIT);
341 if(!hash_key_is_valid(h->type, key_len))
342 return NULL;
343 if(!h->table) {
344 h->table = curlx_calloc(h->slots, sizeof(struct Curl_hash_element *));
345 if(!h->table)
346 return NULL; /* OOM */
347 }
349 functions = hash_get_functions(h->type);
350 slot = &h->table[functions->hash(key, key_len, h->slots)];
351 for(he = *slot; he; he = he->next) {
352 if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
353 /* existing key entry, overwrite by clearing old pointer */
354 hash_elem_clear_ptr(h, he);
355 he->ptr = p;
356 he->dtor = dtor;
357 return p;
358 }
359 }
361 he = hash_elem_create(key, key_len, p, dtor);
362 if(!he)
363 return NULL; /* OOM */
365 hash_elem_link(h, slot, he);
366 return p; /* return the new entry */
369/* Insert the data in the hash. If there already was a match in the hash, that
370 * data is replaced. This function also "lazily" allocates the table if
371 * needed, as it is not done in the _init function (anymore).
372 *
373 * @unittest: 1305
374 * @unittest: 1602
375 * @unittest: 1603
376 */
377void *Curl_hash_add(struct Curl_hash *h,
378 const void *key, size_t key_len, void *p)
380 return Curl_hash_add2(h, key, key_len, p, NULL);
383/* Remove the identified hash entry.
384 * Returns non-zero on failure.
385 *
386 * @unittest: 1603
387 */
388int Curl_hash_delete(struct Curl_hash *h, const void *key, size_t key_len)
390 const struct hash_functions *functions;
392 DEBUGASSERT(h);
393 DEBUGASSERT(h->slots);
394 DEBUGASSERT(h->init == HASHINIT);
395 if(!hash_key_is_valid(h->type, key_len))
396 return 1;
397 if(h->table) {
398 struct Curl_hash_element *he, **he_anchor;
400 functions = hash_get_functions(h->type);
401 he_anchor = &h->table[functions->hash(key, key_len, h->slots)];
402 while(*he_anchor) {
403 he = *he_anchor;
404 if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
405 hash_elem_unlink(h, he_anchor, he);
406 hash_elem_destroy(h, he);
407 return 0;
408 }
409 he_anchor = &he->next;
410 }
411 }
412 return 1;
415/* Retrieves a hash element.
416 *
417 * @unittest: 1603
418 */
419void *Curl_hash_pick(struct Curl_hash *h, const void *key, size_t key_len)
421 const struct hash_functions *functions;
423 DEBUGASSERT(h);
424 DEBUGASSERT(h->init == HASHINIT);
425 if(!hash_key_is_valid(h->type, key_len))
426 return NULL;
427 if(h->table) {
428 struct Curl_hash_element *he;
429 DEBUGASSERT(h->slots);
430 functions = hash_get_functions(h->type);
431 he = h->table[functions->hash(key, key_len, h->slots)];
432 while(he) {
433 if(functions->compare(hash_elem_key(he), he->key_len, key, key_len)) {
434 return he->ptr;
435 }
436 he = he->next;
437 }
438 }
439 return NULL;
442/* Destroys all the entries in the given hash and resets its attributes,
443 * prepping the given hash for [static|dynamic] deallocation.
444 *
445 * @unittest: 1305
446 * @unittest: 1602
447 * @unittest: 1603
448 */
449void Curl_hash_destroy(struct Curl_hash *h)
451 DEBUGASSERT(h->init == HASHINIT);
452 if(h->table) {
453 Curl_hash_clean(h);
454 curlx_safefree(h->table);
455 }
456 DEBUGASSERT(h->size == 0);
457 h->slots = 0;
460/* Removes all the entries in the given hash.
461 *
462 * @unittest: 1602
463 */
464void Curl_hash_clean(struct Curl_hash *h)
466 if(h && h->table) {
467 struct Curl_hash_element *he, **he_anchor;
468 size_t i;
469 DEBUGASSERT(h->init == HASHINIT);
470 for(i = 0; i < h->slots; ++i) {
471 he_anchor = &h->table[i];
472 while(*he_anchor) {
473 he = *he_anchor;
474 hash_elem_unlink(h, he_anchor, he);
475 hash_elem_destroy(h, he);
476 }
477 }
478 }
481size_t Curl_hash_count(struct Curl_hash *h)
483 DEBUGASSERT(h->init == HASHINIT);
484 return h->size;
487/* Cleans all entries that pass the comp function criteria. */
488void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
489 int (*comp)(void *, void *))
491 size_t i;
493 if(!h || !h->table)
494 return;
496 DEBUGASSERT(h->init == HASHINIT);
497 for(i = 0; i < h->slots; ++i) {
498 struct Curl_hash_element *he, **he_anchor = &h->table[i];
499 while(*he_anchor) {
500 /* ask the callback function if we shall remove this entry or not */
501 if(!comp || comp(user, (*he_anchor)->ptr)) {
502 he = *he_anchor;
503 hash_elem_unlink(h, he_anchor, he);
504 hash_elem_destroy(h, he);
505 }
506 else
507 he_anchor = &(*he_anchor)->next;
508 }
509 }
Diagnostics4 git calls · 688 ms
CallStatusSizeTime0688 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=4
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2003.9 KiB681 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=681
git.response.bytes=4019
git.response.cache=MISS
git.response.duration_ms=681
git.fetch.wants=36d4dbe014b28b21d02857309cfb3bcbc254fe84
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=4019
git.pkt.payload.max=3998
git.sideband.band1.bytes=3997
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2a069bbe81293
Request
b6deb3e9…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format