pack-objects.hon commit repack: add delta-islands support (16d75fa)
   1#ifndef PACK_OBJECTS_H
   2#define PACK_OBJECTS_H
   3
   4#include "object-store.h"
   5
   6#define DEFAULT_DELTA_CACHE_SIZE (256 * 1024 * 1024)
   7
   8#define OE_DFS_STATE_BITS       2
   9#define OE_DEPTH_BITS           12
  10#define OE_IN_PACK_BITS         10
  11#define OE_Z_DELTA_BITS         20
  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
  17#define OE_DELTA_SIZE_BITS      20
  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
  32/*
  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 *
  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
  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.
  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 */
  83struct object_entry {
  84        struct pack_idx_entry idx;
  85        void *delta_data;       /* cached delta (uncompressed) */
  86        off_t in_pack_offset;
  87        uint32_t hash;                  /* name hint hash */
  88        unsigned size_:OE_SIZE_BITS;
  89        unsigned size_valid:1;
  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                                     */
  95        unsigned delta_size_:OE_DELTA_SIZE_BITS; /* delta data size (uncompressed) */
  96        unsigned delta_size_valid:1;
  97        unsigned in_pack_idx:OE_IN_PACK_BITS;   /* already in pack */
  98        unsigned z_delta_size:OE_Z_DELTA_BITS;
  99        unsigned type_valid:1;
 100        unsigned type_:TYPE_BITS;
 101        unsigned no_try_delta:1;
 102        unsigned in_pack_type:TYPE_BITS; /* could be delta */
 103
 104        unsigned int tree_depth; /* should be repositioned for packing? */
 105        unsigned char layer;
 106
 107        unsigned preferred_base:1; /*
 108                                    * we do not pack this, but is available
 109                                    * to be used as the base object to delta
 110                                    * objects against.
 111                                    */
 112        unsigned tagged:1; /* near the very tip of refs */
 113        unsigned filled:1; /* assigned write-order */
 114        unsigned dfs_state:OE_DFS_STATE_BITS;
 115        unsigned char in_pack_header_size;
 116        unsigned depth:OE_DEPTH_BITS;
 117
 118        /*
 119         * pahole results on 64-bit linux (gcc and clang)
 120         *
 121         *   size: 80, bit_padding: 20 bits, holes: 8 bits
 122         *
 123         * and on 32-bit (gcc)
 124         *
 125         *   size: 76, bit_padding: 20 bits, holes: 8 bits
 126         */
 127};
 128
 129struct packing_data {
 130        struct object_entry *objects;
 131        uint32_t nr_objects, nr_alloc;
 132
 133        int32_t *index;
 134        uint32_t index_size;
 135
 136        unsigned int *in_pack_pos;
 137
 138        /*
 139         * Only one of these can be non-NULL and they have different
 140         * sizes. if in_pack_by_idx is allocated, oe_in_pack() returns
 141         * the pack of an object using in_pack_idx field. If not,
 142         * in_pack[] array is used the same way as in_pack_pos[]
 143         */
 144        struct packed_git **in_pack_by_idx;
 145        struct packed_git **in_pack;
 146
 147        uintmax_t oe_size_limit;
 148};
 149
 150void prepare_packing_data(struct packing_data *pdata);
 151struct object_entry *packlist_alloc(struct packing_data *pdata,
 152                                    const unsigned char *sha1,
 153                                    uint32_t index_pos);
 154
 155struct object_entry *packlist_find(struct packing_data *pdata,
 156                                   const unsigned char *sha1,
 157                                   uint32_t *index_pos);
 158
 159static inline uint32_t pack_name_hash(const char *name)
 160{
 161        uint32_t c, hash = 0;
 162
 163        if (!name)
 164                return 0;
 165
 166        /*
 167         * This effectively just creates a sortable number from the
 168         * last sixteen non-whitespace characters. Last characters
 169         * count "most", so things that end in ".c" sort together.
 170         */
 171        while ((c = *name++) != 0) {
 172                if (isspace(c))
 173                        continue;
 174                hash = (hash >> 2) + (c << 24);
 175        }
 176        return hash;
 177}
 178
 179static inline enum object_type oe_type(const struct object_entry *e)
 180{
 181        return e->type_valid ? e->type_ : OBJ_BAD;
 182}
 183
 184static inline void oe_set_type(struct object_entry *e,
 185                               enum object_type type)
 186{
 187        if (type >= OBJ_ANY)
 188                BUG("OBJ_ANY cannot be set in pack-objects code");
 189
 190        e->type_valid = type >= OBJ_NONE;
 191        e->type_ = (unsigned)type;
 192}
 193
 194static inline unsigned int oe_in_pack_pos(const struct packing_data *pack,
 195                                          const struct object_entry *e)
 196{
 197        return pack->in_pack_pos[e - pack->objects];
 198}
 199
 200static inline void oe_set_in_pack_pos(const struct packing_data *pack,
 201                                      const struct object_entry *e,
 202                                      unsigned int pos)
 203{
 204        pack->in_pack_pos[e - pack->objects] = pos;
 205}
 206
 207static inline struct packed_git *oe_in_pack(const struct packing_data *pack,
 208                                            const struct object_entry *e)
 209{
 210        if (pack->in_pack_by_idx)
 211                return pack->in_pack_by_idx[e->in_pack_idx];
 212        else
 213                return pack->in_pack[e - pack->objects];
 214}
 215
 216void oe_map_new_pack(struct packing_data *pack,
 217                     struct packed_git *p);
 218static inline void oe_set_in_pack(struct packing_data *pack,
 219                                  struct object_entry *e,
 220                                  struct packed_git *p)
 221{
 222        if (!p->index)
 223                oe_map_new_pack(pack, p);
 224        if (pack->in_pack_by_idx)
 225                e->in_pack_idx = p->index;
 226        else
 227                pack->in_pack[e - pack->objects] = p;
 228}
 229
 230static inline struct object_entry *oe_delta(
 231                const struct packing_data *pack,
 232                const struct object_entry *e)
 233{
 234        if (e->delta_idx)
 235                return &pack->objects[e->delta_idx - 1];
 236        return NULL;
 237}
 238
 239static inline void oe_set_delta(struct packing_data *pack,
 240                                struct object_entry *e,
 241                                struct object_entry *delta)
 242{
 243        if (delta)
 244                e->delta_idx = (delta - pack->objects) + 1;
 245        else
 246                e->delta_idx = 0;
 247}
 248
 249static inline struct object_entry *oe_delta_child(
 250                const struct packing_data *pack,
 251                const struct object_entry *e)
 252{
 253        if (e->delta_child_idx)
 254                return &pack->objects[e->delta_child_idx - 1];
 255        return NULL;
 256}
 257
 258static inline void oe_set_delta_child(struct packing_data *pack,
 259                                      struct object_entry *e,
 260                                      struct object_entry *delta)
 261{
 262        if (delta)
 263                e->delta_child_idx = (delta - pack->objects) + 1;
 264        else
 265                e->delta_child_idx = 0;
 266}
 267
 268static inline struct object_entry *oe_delta_sibling(
 269                const struct packing_data *pack,
 270                const struct object_entry *e)
 271{
 272        if (e->delta_sibling_idx)
 273                return &pack->objects[e->delta_sibling_idx - 1];
 274        return NULL;
 275}
 276
 277static inline void oe_set_delta_sibling(struct packing_data *pack,
 278                                        struct object_entry *e,
 279                                        struct object_entry *delta)
 280{
 281        if (delta)
 282                e->delta_sibling_idx = (delta - pack->objects) + 1;
 283        else
 284                e->delta_sibling_idx = 0;
 285}
 286
 287unsigned long oe_get_size_slow(struct packing_data *pack,
 288                               const struct object_entry *e);
 289static inline unsigned long oe_size(struct packing_data *pack,
 290                                    const struct object_entry *e)
 291{
 292        if (e->size_valid)
 293                return e->size_;
 294
 295        return oe_get_size_slow(pack, e);
 296}
 297
 298static inline int oe_size_less_than(struct packing_data *pack,
 299                                    const struct object_entry *lhs,
 300                                    unsigned long rhs)
 301{
 302        if (lhs->size_valid)
 303                return lhs->size_ < rhs;
 304        if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
 305                return 0;
 306        return oe_get_size_slow(pack, lhs) < rhs;
 307}
 308
 309static inline int oe_size_greater_than(struct packing_data *pack,
 310                                       const struct object_entry *lhs,
 311                                       unsigned long rhs)
 312{
 313        if (lhs->size_valid)
 314                return lhs->size_ > rhs;
 315        if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
 316                return 1;
 317        return oe_get_size_slow(pack, lhs) > rhs;
 318}
 319
 320static inline void oe_set_size(struct packing_data *pack,
 321                               struct object_entry *e,
 322                               unsigned long size)
 323{
 324        if (size < pack->oe_size_limit) {
 325                e->size_ = size;
 326                e->size_valid = 1;
 327        } else {
 328                e->size_valid = 0;
 329                if (oe_get_size_slow(pack, e) != size)
 330                        BUG("'size' is supposed to be the object size!");
 331        }
 332}
 333
 334static inline unsigned long oe_delta_size(struct packing_data *pack,
 335                                          const struct object_entry *e)
 336{
 337        if (e->delta_size_valid)
 338                return e->delta_size_;
 339        return oe_size(pack, e);
 340}
 341
 342static inline void oe_set_delta_size(struct packing_data *pack,
 343                                     struct object_entry *e,
 344                                     unsigned long size)
 345{
 346        e->delta_size_ = size;
 347        e->delta_size_valid = e->delta_size_ == size;
 348        if (!e->delta_size_valid && size != oe_size(pack, e))
 349                BUG("this can only happen in check_object() "
 350                    "where delta size is the same as entry size");
 351}
 352
 353#endif