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