112
algo->final_oid_fn(oid, &ctx);
113
}
114
115
+/*
116
+ * Fast path: precomputed pack data for a 4 GiB + 1 all-NUL blob.
117
+ *
118
+ * The generated pack is almost entirely zeros with a small constant
119
+ * prefix, periodic deflate block headers, and a constant suffix
120
+ * containing the tree, two commits, and the pack checksum. Because
121
+ * every byte is deterministic for a given blob size and hash algorithm,
122
+ * we can write the pack without computing any hashes at all, reducing
123
+ * runtime from minutes of hash computation to seconds of pure I/O.
124
+ *
125
+ * The blob is stored as an uncompressed deflate stream: a two-byte
126
+ * zlib header, then 65538 blocks of up to 0xffff bytes each, followed
127
+ * by an adler32 checksum. The pack header and deflate framing are
128
+ * shared across hash algorithms; only the suffix (which contains OIDs
129
+ * and the pack checksum) differs.
130
+ *
131
+ * Constants were generated by running the generic path and extracting
132
+ * the non-zero bytes from the resulting pack file.
133
+ */
134
+
135
+#define FAST_PACK_4G1_BLOB_SIZE ((size_t)4 * 1024 * 1024 * 1024 + 1)
136
+#define FAST_PACK_4G1_N_FULL_BLOCKS 65537
137
+
138
+/*
139
+ * Per-hash-algorithm constants for the fast path. The prefix and
140
+ * deflate block structure are identical across algorithms; only the
141
+ * suffix (tree, commits, pack checksum) and the commit OID differ.
142
+ */
143
+struct fast_pack {
144
+ uint32_t format_id;
145
+ const unsigned char *suffix;
146
+ size_t suffix_len;
147
+ const char *commit_oid;
148
+};
149
+
150
+/* Pack header + pack object header + zlib header + first block header */
151
+static const unsigned char fast_pack_prefix[] = {
152
+ /* PACK header: signature, version 2, 5 objects */
153
+ 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02,
154
+ 0x00, 0x00, 0x00, 0x05,
155
+ /* pack object header: blob, size = 4294967297 */
156
+ 0xb1, 0x80, 0x80, 0x80, 0x80, 0x01,
157
+ /* zlib header: CMF=0x78, FLG=0x01 */
158
+ 0x78, 0x01,
159
+ /* first non-final block header: BFINAL=0, LEN=0xffff, NLEN=0x0000 */
160
+ 0x00, 0xff, 0xff, 0x00, 0x00
161
+};
162
+
163
+/* Every non-final deflate block header is identical */
164
+static const unsigned char fast_pack_block_header[] = {
165
+ 0x00, 0xff, 0xff, 0x00, 0x00
166
+};
167
+
168
+/* Final block (2 data bytes) + adler32 of 4294967297 NUL bytes */
169
+static const unsigned char fast_pack_final_block[] = {
170
+ /* BFINAL=1, LEN=2, NLEN=0xfffd */
171
+ 0x01, 0x02, 0x00, 0xfd, 0xff,
172
+ /* 2 NUL data bytes */
173
+ 0x00, 0x00,
174
+ /* adler32 */
175
+ 0x00, 0xe2, 0x00, 0x01
176
+};
177
+
178
+/*
179
+ * SHA-1 suffix: tree, commit, empty tree, final commit, pack checksum.
180
+ */
181
+static const unsigned char fast_pack_sha1_suffix[] = {
182
+ 0xa0, 0x02, 0x78, 0x01, 0x01, 0x20, 0x00, 0xdf,
183
+ 0xff, 0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20,
184
+ 0x66, 0x69, 0x6c, 0x65, 0x00, 0x3e, 0xb7, 0xfe,
185
+ 0xb1, 0x41, 0x3c, 0x75, 0x7f, 0x0d, 0x81, 0x81,
186
+ 0xde, 0xb2, 0x8d, 0x1d, 0xab, 0x03, 0xd6, 0x48,
187
+ 0x46, 0xb4, 0xb4, 0x0c, 0x60, 0x95, 0x0b, 0x78,
188
+ 0x01, 0x01, 0xb5, 0x00, 0x4a, 0xff, 0x74, 0x72,
189
+ 0x65, 0x65, 0x20, 0x63, 0x36, 0x38, 0x33, 0x66,
190
+ 0x63, 0x63, 0x37, 0x64, 0x31, 0x64, 0x38, 0x33,
191
+ 0x65, 0x66, 0x32, 0x66, 0x65, 0x31, 0x61, 0x66,
192
+ 0x35, 0x35, 0x32, 0x31, 0x35, 0x64, 0x30, 0x31,
193
+ 0x36, 0x38, 0x64, 0x62, 0x35, 0x32, 0x61, 0x33,
194
+ 0x61, 0x33, 0x62, 0x0a, 0x61, 0x75, 0x74, 0x68,
195
+ 0x6f, 0x72, 0x20, 0x41, 0x20, 0x55, 0x20, 0x54,
196
+ 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, 0x75, 0x74,
197
+ 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d,
198
+ 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e,
199
+ 0x20, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
200
+ 0x38, 0x39, 0x30, 0x20, 0x2b, 0x30, 0x30, 0x30,
201
+ 0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
202
+ 0x74, 0x65, 0x72, 0x20, 0x43, 0x20, 0x4f, 0x20,
203
+ 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x3c,
204
+ 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65,
205
+ 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
206
+ 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31,
207
+ 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
208
+ 0x30, 0x20, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a,
209
+ 0x0a, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x20, 0x62,
210
+ 0x6c, 0x6f, 0x62, 0x20, 0x63, 0x6f, 0x6d, 0x6d,
211
+ 0x69, 0x74, 0x0a, 0xc6, 0x55, 0x37, 0x6b, 0x20,
212
+ 0x78, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
213
+ 0x00, 0x00, 0x01, 0x95, 0x0e, 0x78, 0x01, 0x01,
214
+ 0xe5, 0x00, 0x1a, 0xff, 0x74, 0x72, 0x65, 0x65,
215
+ 0x20, 0x34, 0x62, 0x38, 0x32, 0x35, 0x64, 0x63,
216
+ 0x36, 0x34, 0x32, 0x63, 0x62, 0x36, 0x65, 0x62,
217
+ 0x39, 0x61, 0x30, 0x36, 0x30, 0x65, 0x35, 0x34,
218
+ 0x62, 0x66, 0x38, 0x64, 0x36, 0x39, 0x32, 0x38,
219
+ 0x38, 0x66, 0x62, 0x65, 0x65, 0x34, 0x39, 0x30,
220
+ 0x34, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74,
221
+ 0x20, 0x63, 0x35, 0x62, 0x32, 0x31, 0x63, 0x36,
222
+ 0x31, 0x31, 0x61, 0x61, 0x35, 0x39, 0x34, 0x65,
223
+ 0x63, 0x39, 0x66, 0x64, 0x37, 0x65, 0x39, 0x32,
224
+ 0x63, 0x66, 0x39, 0x36, 0x34, 0x38, 0x39, 0x31,
225
+ 0x34, 0x63, 0x61, 0x34, 0x63, 0x32, 0x34, 0x31,
226
+ 0x32, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72,
227
+ 0x20, 0x41, 0x20, 0x55, 0x20, 0x54, 0x68, 0x6f,
228
+ 0x72, 0x20, 0x3c, 0x61, 0x75, 0x74, 0x68, 0x6f,
229
+ 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
230
+ 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31,
231
+ 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
232
+ 0x30, 0x20, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a,
233
+ 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65,
234
+ 0x72, 0x20, 0x43, 0x20, 0x4f, 0x20, 0x4d, 0x69,
235
+ 0x74, 0x74, 0x65, 0x72, 0x20, 0x3c, 0x63, 0x6f,
236
+ 0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65, 0x72, 0x40,
237
+ 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
238
+ 0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31, 0x32, 0x33,
239
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x20,
240
+ 0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x0a, 0x45,
241
+ 0x6d, 0x70, 0x74, 0x79, 0x20, 0x74, 0x72, 0x65,
242
+ 0x65, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
243
+ 0x0a, 0xaa, 0xb8, 0x45, 0x01, 0x8e, 0xfc, 0xf0,
244
+ 0x2f, 0x9c, 0xc5, 0xcc, 0x4f, 0x6a, 0x1a, 0xc9,
245
+ 0x2b, 0x23, 0xa9, 0xff, 0x91, 0x06, 0xc2, 0x70,
246
+ 0xe3
247
+};
248
+
249
+static const struct fast_pack fast_packs[] = {
250
+ {
251
+ .format_id = GIT_SHA1_FORMAT_ID,
252
+ .suffix = fast_pack_sha1_suffix,
253
+ .suffix_len = sizeof(fast_pack_sha1_suffix),
254
+ .commit_oid = "aac43daf40d0377af31aa9c798a4ae8a31b55c1d",
255
+ },
256
+};
257
+
258
+/*
259
+ * Try the fast path for known blob sizes. Returns 1 if the pack was
260
+ * written from precomputed constants, 0 if the caller should fall
261
+ * through to the generic path.
262
+ */
263
+static int generate_fast_pack(const char *path, size_t blob_size,
264
+ const struct git_hash_algo *algo)
265
+{
266
+ const struct fast_pack *fp = NULL;
267
+ FILE *f;
268
+ size_t i;
269
+
270
+ if (blob_size != FAST_PACK_4G1_BLOB_SIZE)
271
+ return 0;
272
+
273
+ for (i = 0; i < ARRAY_SIZE(fast_packs); i++) {
274
+ if (fast_packs[i].format_id == algo->format_id) {
275
+ fp = &fast_packs[i];
276
+ break;
277
+ }
278
+ }
279
+ if (!fp)
280
+ return 0;
281
+
282
+ f = xfopen(path, "wb");
283
+
284
+ fwrite_or_die(f, fast_pack_prefix, sizeof(fast_pack_prefix));
285
+
286
+ /* First full block: 0xffff zero bytes (header already in prefix) */
287
+ fwrite_or_die(f, zeros, BLOCK_SIZE);
288
+
289
+ /* Remaining non-final full blocks */
290
+ for (i = 1; i < FAST_PACK_4G1_N_FULL_BLOCKS; i++) {
291
+ fwrite_or_die(f, fast_pack_block_header,
292
+ sizeof(fast_pack_block_header));
293
+ fwrite_or_die(f, zeros, BLOCK_SIZE);
294
+ }
295
+
296
+ /* Final block (2 data bytes) + adler32 */
297
+ fwrite_or_die(f, fast_pack_final_block,
298
+ sizeof(fast_pack_final_block));
299
+
300
+ /* Tree, commits, and pack checksum */
301
+ fwrite_or_die(f, fp->suffix, fp->suffix_len);
302
+
303
+ if (fclose(f))
304
+ die_errno(_("could not close '%s'"), path);
305
+
306
+ printf("%s\n", fp->commit_oid);
307
+ return 1;
308
+}
309
+
310
/*
311
* Generate a pack file with a single large (>4GB) reachable object.
312
*
322
static int generate_pack_with_large_object(const char *path, size_t blob_size,
323
const struct git_hash_algo *algo)
324
{
130
- FILE *f = xfopen(path, "wb");
325
+ FILE *f;
326
struct git_hash_ctx pack_ctx;
327
unsigned char pack_hash[GIT_MAX_RAWSZ];
328
struct object_id blob_oid, tree_oid, commit_oid, empty_tree_oid, final_commit_oid;
334
.hdr_entries = htonl(object_count),
335
};
336
337
+ if (generate_fast_pack(path, blob_size, algo))
338
+ return 0;
339
+
340
+ f = xfopen(path, "wb");
341
+
342
algo->init_fn(&pack_ctx);
343
344
/* Write pack header */