master
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 "uint-bset.h"
28#ifdef DEBUGBUILD
29#define CURL_UINT32_BSET_MAGIC 0x62757473
30#endif
32void Curl_uint32_bset_init(struct uint32_bset *bset)
33{
34 memset(bset, 0, sizeof(*bset));
35#ifdef DEBUGBUILD
36 bset->init = CURL_UINT32_BSET_MAGIC;
37#endif
38}
40CURLcode Curl_uint32_bset_resize(struct uint32_bset *bset, uint32_t nmax)
41{
42 uint32_t nslots = (nmax < (UINT32_MAX - 63)) ?
43 ((nmax + 63) / 64) : (UINT32_MAX / 64);
45 DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
46 if(nslots != bset->nslots) {
47 uint64_t *slots = curlx_calloc(nslots, sizeof(uint64_t));
48 if(!slots)
49 return CURLE_OUT_OF_MEMORY;
51 if(bset->slots) {
52 memcpy(slots, bset->slots,
53 (CURLMIN(nslots, bset->nslots) * sizeof(uint64_t)));
54 curlx_free(bset->slots);
55 }
56 bset->slots = slots;
57 bset->nslots = nslots;
58 bset->first_slot_used = 0;
59 }
60 return CURLE_OK;
61}
63void Curl_uint32_bset_destroy(struct uint32_bset *bset)
64{
65 DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
66 curlx_free(bset->slots);
67 memset(bset, 0, sizeof(*bset));
68}
70#ifdef UNITTESTS
71/* @unittest 3211 */
72UNITTEST uint32_t uint32_bset_capacity(struct uint32_bset *bset);
73UNITTEST uint32_t uint32_bset_capacity(struct uint32_bset *bset)
74{
75 return bset->nslots * 64;
76}
77#endif
79uint32_t Curl_uint32_bset_count(struct uint32_bset *bset)
80{
81 uint32_t i;
82 uint32_t n = 0;
83 for(i = bset->first_slot_used; i < bset->nslots; ++i) {
84 if(bset->slots[i])
85 n += CURL_POPCOUNT64(bset->slots[i]);
86 }
87 return n;
88}
90bool Curl_uint32_bset_empty(struct uint32_bset *bset)
91{
92 uint32_t i;
93 for(i = bset->first_slot_used; i < bset->nslots; ++i) {
94 if(bset->slots[i])
95 return FALSE;
96 }
97 return TRUE;
98}
100void Curl_uint32_bset_clear(struct uint32_bset *bset)
101{
102 if(bset->nslots) {
103 memset(bset->slots, 0, bset->nslots * sizeof(uint64_t));
104 bset->first_slot_used = UINT32_MAX;
105 }
106}
108bool Curl_uint32_bset_add(struct uint32_bset *bset, uint32_t i)
109{
110 uint32_t islot = i / 64;
111 if(islot >= bset->nslots)
112 return FALSE;
113 bset->slots[islot] |= ((uint64_t)1 << (i % 64));
114 if(islot < bset->first_slot_used)
115 bset->first_slot_used = islot;
116 return TRUE;
117}
119void Curl_uint32_bset_remove(struct uint32_bset *bset, uint32_t i)
120{
121 size_t islot = i / 64;
122 if(islot < bset->nslots)
123 bset->slots[islot] &= ~((uint64_t)1 << (i % 64));
124}
126bool Curl_uint32_bset_contains(struct uint32_bset *bset, uint32_t i)
127{
128 uint32_t islot = i / 64;
129 if(islot >= bset->nslots)
130 return FALSE;
131 return (bset->slots[islot] & ((uint64_t)1 << (i % 64))) != 0;
132}
134bool Curl_uint32_bset_first(struct uint32_bset *bset, uint32_t *pfirst)
135{
136 uint32_t i;
137 for(i = bset->first_slot_used; i < bset->nslots; ++i) {
138 if(bset->slots[i]) {
139 *pfirst = (i * 64) + CURL_CTZ64(bset->slots[i]);
140 bset->first_slot_used = i;
141 return TRUE;
142 }
143 }
144 bset->first_slot_used = *pfirst = UINT32_MAX;
145 return FALSE;
146}
148bool Curl_uint32_bset_next(struct uint32_bset *bset, uint32_t last,
149 uint32_t *pnext)
150{
151 uint32_t islot;
152 uint64_t x;
154 ++last; /* look for number one higher than last */
155 islot = last / 64; /* the slot this would be in */
156 if(islot < bset->nslots) {
157 /* shift away the bits we already iterated in this slot */
158 x = (bset->slots[islot] >> (last % 64));
159 if(x) {
160 /* more bits set, next is `last` + trailing 0s of the shifted slot */
161 *pnext = last + CURL_CTZ64(x);
162 return TRUE;
163 }
164 /* no more bits set in the last slot, scan forward */
165 for(islot = islot + 1; islot < bset->nslots; ++islot) {
166 if(bset->slots[islot]) {
167 *pnext = (islot * 64) + CURL_CTZ64(bset->slots[islot]);
168 return TRUE;
169 }
170 }
171 }
172 *pnext = UINT32_MAX; /* a value we cannot store */
173 return FALSE;
174}
176#ifdef CURL_POPCOUNT64_IMPLEMENT
177uint32_t Curl_popcount64(uint64_t x)
178{
179 /* Compute the "Hamming Distance" between 'x' and 0,
180 * which is the number of set bits in 'x'.
181 * See: https://en.wikipedia.org/wiki/Hamming_weight */
182 const uint64_t m1 = 0x5555555555555555LL; /* 0101+ */
183 const uint64_t m2 = 0x3333333333333333LL; /* 00110011+ */
184 const uint64_t m4 = 0x0f0f0f0f0f0f0f0fLL; /* 00001111+ */
185 /* 1 + 256^1 + 256^2 + 256^3 + ... + 256^7 */
186 const uint64_t h01 = 0x0101010101010101LL;
187 x -= (x >> 1) & m1; /* replace every 2 bits with bits present */
188 x = (x & m2) + ((x >> 2) & m2); /* replace every nibble with bits present */
189 x = (x + (x >> 4)) & m4; /* replace every byte with bits present */
190 /* top 8 bits of x + (x << 8) + (x << 16) + (x << 24) + ... which makes the
191 * top byte the sum of all individual 8 bytes, throw away the rest */
192 return (uint32_t)((x * h01) >> 56);
193}
194#endif /* CURL_POPCOUNT64_IMPLEMENT */
196#ifdef CURL_CTZ64_IMPLEMENT
197uint32_t Curl_ctz64(uint64_t x)
198{
199 /* count trailing zeros in a uint64_t.
200 * divide and conquer to find the number of lower 0 bits */
201 const uint64_t ml32 = 0xFFFFFFFF; /* lower 32 bits */
202 const uint64_t ml16 = 0x0000FFFF; /* lower 16 bits */
203 const uint64_t ml8 = 0x000000FF; /* lower 8 bits */
204 const uint64_t ml4 = 0x0000000F; /* lower 4 bits */
205 const uint64_t ml2 = 0x00000003; /* lower 2 bits */
206 uint32_t n;
208 if(!x)
209 return 64;
210 n = 1;
211 if(!(x & ml32)) {
212 n = n + 32;
213 x = x >> 32;
214 }
215 if(!(x & ml16)) {
216 n = n + 16;
217 x = x >> 16;
218 }
219 if(!(x & ml8)) {
220 n = n + 8;
221 x = x >> 8;
222 }
223 if(!(x & ml4)) {
224 n = n + 4;
225 x = x >> 4;
226 }
227 if(!(x & ml2)) {
228 n = n + 2;
229 x = x >> 2;
230 }
231 return n - (uint32_t)(x & 1);
232}
233#endif /* CURL_CTZ64_IMPLEMENT */
Diagnostics4 git calls · 144 ms
CallStatusSizeTime0144 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 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=86Open pack map
fetch2002.2 KiB139 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=139 git.response.bytes=2294 git.response.cache=MISS git.response.duration_ms=139 git.fetch.wants=82b84e3b5af8c0a60b8da4880c0de45787579a55 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=2294 git.pkt.payload.max=2273 git.sideband.band1.bytes=2272 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
- a5e2cc7ce9b47929
- Request
- 2e03eb5d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format