Artifactspublic-v0

curl

master
ply4 git calls29 ms290 KiB3 cachedprotocol v2
311 lines9.0 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 "u8_strset.h"
27#include "curlx/strdup.h"
29/* random patterns for API verification */
30#ifdef DEBUGBUILD
31#define CURL_U8_STRSET_MAGIC 0x7117e783
32#endif
34#define CURL_U8_STRSET_DEBUG 0
36#define CURL_SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
38#define CURL_U8_STRSET_SLOT_IDX(s, i) (uint8_t)((i) & (s)->slotmask)
39#define CURL_U8_SLOT_CNT(i) (uint16_t)((i) ? (1U << (i)) : 0)
40#define CURL_U8_SLOT_MASK(i) (uint8_t)((1U << (i)) - 1)
41#define CURL_U8_STRSET_SLOT_CNT(s) CURL_U8_SLOT_CNT((s)->slotbits)
43/* A hashset for tuples (id, string) using Robin Hood Hashing.
44 * <https://www.cs.cornell.edu/courses/JavaAndDS/files/hashing_RobinHood.pdf>
45 * The basic idea here to handle collisions by robbing "rich" entries and
46 * giving to the "poor":
47 * - We have an array: (id, string) are ideally placed at index "id % size".
48 * - If slot at index is already occupied, we have a collision.
49 * - A simple collision strategy would look at the next index, and the
50 * next until finding an empty slot.
51 * - The drawback is that this may lead to many checks on lookups, as it
52 * will need to also look at subsequent slots until it finds the match.
53 * The amount of lookups is the "probe sequence length" (psl) and this
54 * may vary greatly between entries.
55 * - Robin Hood Hashing balances the 'psl's of all entries more evenly:
56 * - psl == 0 means an entry is in exactly the right slot
57 * - psl == 1 means it is in the slot right after. psl == 2 is the slot
58 * after that, etc.
59 * - when inserting a new entry, track its psl. Finding a slot where
60 * the existing entry has a lower psl makes a swap. Put the new entry
61 * and its psl there, take the previous entry and its psl and find
62 * the next best slot for the previous entry. */
63void Curl_u8_strset_init(struct u8_strset *set)
65#if defined(__GNUC__) && __GNUC__ >= 13
66#pragma GCC diagnostic push
67#pragma GCC diagnostic ignored "-Warray-bounds"
68#endif
69 memset(set, 0, sizeof(*set));
70#if defined(__GNUC__) && __GNUC__ >= 13
71#pragma GCC diagnostic pop
72#endif
73 set->data = set->sdata;
74 set->ids = set->sids;
75 set->psl = set->spsl;
76 set->slotbits = CURL_U8_STRSET_START_BITS;
77 set->slotmask = CURL_U8_SLOT_MASK(set->slotbits);
78 set->count = 0;
79#ifdef DEBUGBUILD
80 set->init = CURL_U8_STRSET_MAGIC;
81#endif
84void Curl_u8_strset_clear(struct u8_strset *set)
86 uint16_t i;
87 DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
88 for(i = 0; i < CURL_U8_STRSET_SLOT_CNT(set); ++i)
89 curlx_safefree(set->data[i]);
91 if(set->data != set->sdata)
92 curlx_safefree(set->data);
93 Curl_u8_strset_init(set);
96static void u8_strset_addn(struct u8_strset *set, uint8_t id, char *val)
98 uint8_t i = CURL_U8_STRSET_SLOT_IDX(set, id);
99 uint8_t psl = 0;
100 while(set->data[i]) {
101 if(psl > set->psl[i]) { /* SWAP */
102 char *tmpdata;
103 tmpdata = set->data[i];
104 set->data[i] = val;
105 val = tmpdata;
106 CURL_SWAP(set->psl[i], psl);
107 CURL_SWAP(set->ids[i], id);
108 }
109 i = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
110 ++psl;
111 }
112 set->ids[i] = id;
113 set->data[i] = val;
114 set->psl[i] = psl;
115 ++set->count;
118static bool u8_strset_grow(struct u8_strset *set)
120 uint8_t *prev_ids, nslotbits;
121 uint16_t prev_slots;
122 char **prev_data;
123 size_t nslots;
124 void *d;
126 if(set->slotbits >= 8)
127 return FALSE;
128 nslotbits = (uint8_t)(set->slotbits + 1);
129#if CURL_U8_STRSET_DEBUG
130 curl_mfprintf(stderr, "u8_strset_grow from %d to %d\n",
131 set->slotbits, nslotbits);
132#endif
133 nslots = CURL_U8_SLOT_CNT(nslotbits);
134 d = curlx_calloc(1, (nslots * sizeof(char *)) + (2 * nslots));
135 if(!d)
136 return FALSE;
138 prev_data = set->data;
139 prev_ids = set->ids;
140 prev_slots = set->slotbits;
141#if defined(__GNUC__) && __GNUC__ >= 13
142#pragma GCC diagnostic push
143#pragma GCC diagnostic ignored "-Wanalyzer-allocation-size"
144#endif
145 set->data = (char **)d;
146#if defined(__GNUC__) && __GNUC__ >= 13
147#pragma GCC diagnostic pop
148#endif
149 set->ids = (uint8_t *)d + (nslots * sizeof(char *));
150 set->psl = set->ids + nslots;
151 set->slotbits = nslotbits;
152 set->slotmask = CURL_U8_SLOT_MASK(set->slotbits);
153 set->count = 0;
154 /* re-add previous entries */
155 {
156 uint16_t i;
157 for(i = 0; i < CURL_U8_SLOT_CNT(prev_slots); ++i) {
158 if(prev_data[i])
159 u8_strset_addn(set, prev_ids[i], prev_data[i]);
160 }
161 }
162 if(prev_data != set->sdata)
163 curlx_free(prev_data);
164 return TRUE;
167static bool u8_strset_get_index(struct u8_strset *set,
168 uint8_t id, uint8_t *pindex)
170 uint8_t i = CURL_U8_STRSET_SLOT_IDX(set, id);
171 uint8_t psl = 0;
172 while(set->data[i] && (psl <= set->psl[i])) {
173 if(set->ids[i] == id) {
174 *pindex = i;
175#if CURL_U8_STRSET_DEBUG
176 curl_mfprintf(stderr, "u8_strset_index %d=%s\n", id, set->data[i]);
177#endif
178 return TRUE;
179 }
180 i = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
181 ++psl;
182 }
183#if CURL_U8_STRSET_DEBUG
184 curl_mfprintf(stderr, "u8_strset_index %d not found\n", id);
185#endif
186 *pindex = 0;
187 return FALSE;
190#ifdef UNITTESTS
191/* @unittest 3211 */
192uint16_t Curl_u8_strset_count(struct u8_strset *set)
194 return set->count;
196#endif
198const char *Curl_u8_strset_get(struct u8_strset *set, uint8_t id)
200 uint8_t i;
201 DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
202 if(u8_strset_get_index(set, id, &i))
203 return set->data[i];
204 return NULL;
207CURLcode Curl_u8_strset_setn(struct u8_strset *set,
208 uint8_t id, char *str)
210 uint8_t i;
212 DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
213#if CURL_U8_STRSET_DEBUG
214 curl_mfprintf(stderr, "u8_strset_setn %d=%s\n", id, str);
215#endif
216 if(!str) {
217 Curl_u8_strset_unset(set, id);
218 return CURLE_OK;
219 }
221 if(u8_strset_get_index(set, id, &i)) {
222 /* `id` is in set, replace value */
223 curlx_free(set->data[i]);
224 set->data[i] = str;
225 return CURLE_OK;
226 }
227 /* `id` not in set yet, grow if full */
228 if((set->count >= CURL_U8_STRSET_SLOT_CNT(set)) && !u8_strset_grow(set)) {
229 curlx_free(str);
230 return CURLE_OUT_OF_MEMORY;
231 }
233 u8_strset_addn(set, id, str);
234 return CURLE_OK;
237CURLcode Curl_u8_strset_setx(struct u8_strset *set,
238 uint8_t id, const char *str, size_t slen)
240 char *val;
242 DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
243 if(!str) {
244 Curl_u8_strset_unset(set, id);
245 return CURLE_OK;
246 }
248 val = curlx_memdup0(str, slen);
249 if(!val)
250 return CURLE_OUT_OF_MEMORY;
251 return Curl_u8_strset_setn(set, id, val);
254/* @unittest 3211 */
255UNITTEST CURLcode u8_strset_set(struct u8_strset *set,
256 uint8_t id, const char *str);
257UNITTEST CURLcode u8_strset_set(struct u8_strset *set,
258 uint8_t id, const char *str)
260 return Curl_u8_strset_setx(set, id, str, str ? strlen(str) : 0);
263static void u8_strset_unset(struct u8_strset *set, uint8_t id, bool zero)
265 uint8_t i, j;
267 DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
268 if(u8_strset_get_index(set, id, &i)) {
269 /* `id` is in set */
270 if(zero)
271 curlx_strzero(set->data[i]);
272 curlx_safefree(set->data[i]);
273 set->ids[i] = set->psl[i] = 0;
274 --set->count;
275 j = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
276 /* shift all entries with positive psl "down" */
277 while(set->data[j] && set->psl[j]) {
278 set->data[i] = set->data[j];
279 set->ids[i] = set->ids[j];
280 set->psl[i] = (uint8_t)(set->psl[j] - 1);
281 set->data[j] = NULL;
282 set->ids[j] = set->psl[j] = 0;
283 i = j;
284 j = CURL_U8_STRSET_SLOT_IDX(set, i + 1);
285 }
286 }
289void Curl_u8_strset_unset(struct u8_strset *set, uint8_t id)
291 u8_strset_unset(set, id, FALSE);
294void Curl_u8_strset_unset0(struct u8_strset *set, uint8_t id)
296 u8_strset_unset(set, id, TRUE);
299CURLcode Curl_u8_strset_copy(struct u8_strset *dest, struct u8_strset *src)
301 CURLcode result = CURLE_OK;
302 uint16_t i;
304 DEBUGASSERT(src->init == CURL_U8_STRSET_MAGIC);
305 Curl_u8_strset_clear(dest);
306 for(i = 0; !result && (i < CURL_U8_STRSET_SLOT_CNT(src)); ++i) {
307 if(src->data[i])
308 result = u8_strset_set(dest, src->ids[i], src->data[i]);
309 }
310 return result;
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
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=23
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
fetch2002.8 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2904
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=e2e368e01a1c0d5ef04716956e25af96accd79a7
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=2904
git.pkt.payload.max=2883
git.sideband.band1.bytes=2882
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
a5e185eadb1cdbb4
Request
451e6c9b…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format