pack-bitmap: save "have" bitmap from walk
[git/git.git] / pack-objects.h
CommitLineData
2834bc27
VM
1#ifndef PACK_OBJECTS_H
2#define PACK_OBJECTS_H
3
43fa44fa 4#include "object-store.h"
ef3ca954 5#include "pack.h"
43fa44fa 6
9806f5a7
NTND
7#define DEFAULT_DELTA_CACHE_SIZE (256 * 1024 * 1024)
8
0c6804ab 9#define OE_DFS_STATE_BITS 2
b5c0cbd8 10#define OE_DEPTH_BITS 12
43fa44fa 11#define OE_IN_PACK_BITS 10
0cb3c142 12#define OE_Z_DELTA_BITS 20
ac77d0c3
NTND
13/*
14 * Note that oe_set_size() becomes expensive when the given size is
15 * above this limit. Don't lower it too much.
16 */
17#define OE_SIZE_BITS 31
0aca34e8 18#define OE_DELTA_SIZE_BITS 20
0c6804ab
NTND
19
20/*
21 * State flags for depth-first search used for analyzing delta cycles.
22 *
23 * The depth is measured in delta-links to the base (so if A is a delta
24 * against B, then A has a depth of 1, and B a depth of 0).
25 */
26enum dfs_state {
27 DFS_NONE = 0,
28 DFS_ACTIVE,
29 DFS_DONE,
30 DFS_NUM_STATES
31};
32
8d6ccce1 33/*
3b13a5f2
NTND
34 * The size of struct nearly determines pack-objects's memory
35 * consumption. This struct is packed tight for that reason. When you
36 * add or reorder something in this struct, think a bit about this.
37 *
8d6ccce1
NTND
38 * basic object info
39 * -----------------
40 * idx.oid is filled up before delta searching starts. idx.crc32 is
41 * only valid after the object is written out and will be used for
42 * generating the index. idx.offset will be both gradually set and
43 * used in writing phase (base objects get offset first, then deltas
44 * refer to them)
45 *
46 * "size" is the uncompressed object size. Compressed size of the raw
47 * data for an object in a pack is not stored anywhere but is computed
27a7d067
NTND
48 * and made available when reverse .idx is made. Note that when a
49 * delta is reused, "size" is the uncompressed _delta_ size, not the
50 * canonical one after the delta has been applied.
8d6ccce1
NTND
51 *
52 * "hash" contains a path name hash which is used for sorting the
53 * delta list and also during delta searching. Once prepare_pack()
54 * returns it's no longer needed.
55 *
56 * source pack info
57 * ----------------
58 * The (in_pack, in_pack_offset) tuple contains the location of the
59 * object in the source pack. in_pack_header_size allows quickly
60 * skipping the header and going straight to the zlib stream.
61 *
62 * "type" and "in_pack_type" both describe object type. in_pack_type
63 * may contain a delta type, while type is always the canonical type.
64 *
65 * deltas
66 * ------
67 * Delta links (delta, delta_child and delta_sibling) are created to
68 * reflect that delta graph from the source pack then updated or added
69 * during delta searching phase when we find better deltas.
70 *
71 * delta_child and delta_sibling are last needed in
72 * compute_write_order(). "delta" and "delta_size" must remain valid
73 * at object writing phase in case the delta is not cached.
74 *
75 * If a delta is cached in memory and is compressed, delta_data points
76 * to the data and z_delta_size contains the compressed size. If it's
77 * uncompressed [1], z_delta_size must be zero. delta_size is always
78 * the uncompressed size and must be valid even if the delta is not
79 * cached.
80 *
81 * [1] during try_delta phase we don't bother with compressing because
82 * the delta could be quickly replaced with a better one.
83 */
2834bc27
VM
84struct object_entry {
85 struct pack_idx_entry idx;
2834bc27 86 void *delta_data; /* cached delta (uncompressed) */
3b13a5f2 87 off_t in_pack_offset;
2834bc27 88 uint32_t hash; /* name hint hash */
ac77d0c3
NTND
89 unsigned size_:OE_SIZE_BITS;
90 unsigned size_valid:1;
898eba5e
NTND
91 uint32_t delta_idx; /* delta base object */
92 uint32_t delta_child_idx; /* deltified objects who bases me */
93 uint32_t delta_sibling_idx; /* other deltified objects who
94 * uses the same base as me
95 */
0aca34e8
NTND
96 unsigned delta_size_:OE_DELTA_SIZE_BITS; /* delta data size (uncompressed) */
97 unsigned delta_size_valid:1;
3b13a5f2 98 unsigned in_pack_idx:OE_IN_PACK_BITS; /* already in pack */
0cb3c142 99 unsigned z_delta_size:OE_Z_DELTA_BITS;
3b13a5f2 100 unsigned type_valid:1;
fd9b1bae 101 unsigned type_:TYPE_BITS;
3b13a5f2 102 unsigned no_try_delta:1;
fd9b1bae 103 unsigned in_pack_type:TYPE_BITS; /* could be delta */
2834bc27
VM
104 unsigned preferred_base:1; /*
105 * we do not pack this, but is available
106 * to be used as the base object to delta
107 * objects against.
108 */
2834bc27
VM
109 unsigned tagged:1; /* near the very tip of refs */
110 unsigned filled:1; /* assigned write-order */
0c6804ab 111 unsigned dfs_state:OE_DFS_STATE_BITS;
3b13a5f2 112 unsigned char in_pack_header_size;
b5c0cbd8 113 unsigned depth:OE_DEPTH_BITS;
4cf2143e
JK
114
115 /*
3b13a5f2 116 * pahole results on 64-bit linux (gcc and clang)
7dbabbbe 117 *
3b13a5f2
NTND
118 * size: 80, bit_padding: 20 bits, holes: 8 bits
119 *
120 * and on 32-bit (gcc)
121 *
122 * size: 76, bit_padding: 20 bits, holes: 8 bits
4cf2143e 123 */
2834bc27
VM
124};
125
126struct packing_data {
127 struct object_entry *objects;
128 uint32_t nr_objects, nr_alloc;
129
130 int32_t *index;
131 uint32_t index_size;
06af3bba
NTND
132
133 unsigned int *in_pack_pos;
43fa44fa
NTND
134
135 /*
136 * Only one of these can be non-NULL and they have different
137 * sizes. if in_pack_by_idx is allocated, oe_in_pack() returns
138 * the pack of an object using in_pack_idx field. If not,
139 * in_pack[] array is used the same way as in_pack_pos[]
140 */
141 struct packed_git **in_pack_by_idx;
142 struct packed_git **in_pack;
ac77d0c3
NTND
143
144 uintmax_t oe_size_limit;
2834bc27
VM
145};
146
43fa44fa 147void prepare_packing_data(struct packing_data *pdata);
2834bc27
VM
148struct object_entry *packlist_alloc(struct packing_data *pdata,
149 const unsigned char *sha1,
150 uint32_t index_pos);
151
152struct object_entry *packlist_find(struct packing_data *pdata,
153 const unsigned char *sha1,
154 uint32_t *index_pos);
155
68fb36eb
VM
156static inline uint32_t pack_name_hash(const char *name)
157{
158 uint32_t c, hash = 0;
159
160 if (!name)
161 return 0;
162
163 /*
164 * This effectively just creates a sortable number from the
165 * last sixteen non-whitespace characters. Last characters
166 * count "most", so things that end in ".c" sort together.
167 */
168 while ((c = *name++) != 0) {
169 if (isspace(c))
170 continue;
171 hash = (hash >> 2) + (c << 24);
172 }
173 return hash;
174}
175
fd9b1bae
NTND
176static inline enum object_type oe_type(const struct object_entry *e)
177{
178 return e->type_valid ? e->type_ : OBJ_BAD;
179}
180
181static inline void oe_set_type(struct object_entry *e,
182 enum object_type type)
183{
184 if (type >= OBJ_ANY)
185 BUG("OBJ_ANY cannot be set in pack-objects code");
186
187 e->type_valid = type >= OBJ_NONE;
188 e->type_ = (unsigned)type;
189}
190
06af3bba
NTND
191static inline unsigned int oe_in_pack_pos(const struct packing_data *pack,
192 const struct object_entry *e)
193{
194 return pack->in_pack_pos[e - pack->objects];
195}
196
197static inline void oe_set_in_pack_pos(const struct packing_data *pack,
198 const struct object_entry *e,
199 unsigned int pos)
200{
201 pack->in_pack_pos[e - pack->objects] = pos;
202}
203
43fa44fa
NTND
204static inline struct packed_git *oe_in_pack(const struct packing_data *pack,
205 const struct object_entry *e)
206{
207 if (pack->in_pack_by_idx)
208 return pack->in_pack_by_idx[e->in_pack_idx];
209 else
210 return pack->in_pack[e - pack->objects];
211}
212
213void oe_map_new_pack(struct packing_data *pack,
214 struct packed_git *p);
215static inline void oe_set_in_pack(struct packing_data *pack,
216 struct object_entry *e,
217 struct packed_git *p)
218{
219 if (!p->index)
220 oe_map_new_pack(pack, p);
221 if (pack->in_pack_by_idx)
222 e->in_pack_idx = p->index;
223 else
224 pack->in_pack[e - pack->objects] = p;
225}
226
898eba5e
NTND
227static inline struct object_entry *oe_delta(
228 const struct packing_data *pack,
229 const struct object_entry *e)
230{
231 if (e->delta_idx)
232 return &pack->objects[e->delta_idx - 1];
233 return NULL;
234}
235
236static inline void oe_set_delta(struct packing_data *pack,
237 struct object_entry *e,
238 struct object_entry *delta)
239{
240 if (delta)
241 e->delta_idx = (delta - pack->objects) + 1;
242 else
243 e->delta_idx = 0;
244}
245
246static inline struct object_entry *oe_delta_child(
247 const struct packing_data *pack,
248 const struct object_entry *e)
249{
250 if (e->delta_child_idx)
251 return &pack->objects[e->delta_child_idx - 1];
252 return NULL;
253}
254
255static inline void oe_set_delta_child(struct packing_data *pack,
256 struct object_entry *e,
257 struct object_entry *delta)
258{
259 if (delta)
260 e->delta_child_idx = (delta - pack->objects) + 1;
261 else
262 e->delta_child_idx = 0;
263}
264
265static inline struct object_entry *oe_delta_sibling(
266 const struct packing_data *pack,
267 const struct object_entry *e)
268{
269 if (e->delta_sibling_idx)
270 return &pack->objects[e->delta_sibling_idx - 1];
271 return NULL;
272}
273
274static inline void oe_set_delta_sibling(struct packing_data *pack,
275 struct object_entry *e,
276 struct object_entry *delta)
277{
278 if (delta)
279 e->delta_sibling_idx = (delta - pack->objects) + 1;
280 else
281 e->delta_sibling_idx = 0;
282}
283
ac77d0c3
NTND
284unsigned long oe_get_size_slow(struct packing_data *pack,
285 const struct object_entry *e);
286static inline unsigned long oe_size(struct packing_data *pack,
287 const struct object_entry *e)
288{
289 if (e->size_valid)
290 return e->size_;
291
292 return oe_get_size_slow(pack, e);
293}
294
295static inline int oe_size_less_than(struct packing_data *pack,
296 const struct object_entry *lhs,
297 unsigned long rhs)
298{
299 if (lhs->size_valid)
300 return lhs->size_ < rhs;
301 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
302 return 0;
303 return oe_get_size_slow(pack, lhs) < rhs;
304}
305
306static inline int oe_size_greater_than(struct packing_data *pack,
307 const struct object_entry *lhs,
308 unsigned long rhs)
309{
310 if (lhs->size_valid)
311 return lhs->size_ > rhs;
312 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
313 return 1;
314 return oe_get_size_slow(pack, lhs) > rhs;
315}
316
317static inline void oe_set_size(struct packing_data *pack,
318 struct object_entry *e,
319 unsigned long size)
320{
321 if (size < pack->oe_size_limit) {
322 e->size_ = size;
323 e->size_valid = 1;
324 } else {
325 e->size_valid = 0;
326 if (oe_get_size_slow(pack, e) != size)
327 BUG("'size' is supposed to be the object size!");
328 }
329}
330
0aca34e8
NTND
331static inline unsigned long oe_delta_size(struct packing_data *pack,
332 const struct object_entry *e)
333{
334 if (e->delta_size_valid)
335 return e->delta_size_;
336 return oe_size(pack, e);
337}
338
339static inline void oe_set_delta_size(struct packing_data *pack,
340 struct object_entry *e,
341 unsigned long size)
342{
343 e->delta_size_ = size;
344 e->delta_size_valid = e->delta_size_ == size;
345 if (!e->delta_size_valid && size != oe_size(pack, e))
346 BUG("this can only happen in check_object() "
347 "where delta size is the same as entry size");
348}
349
2834bc27 350#endif