| 1394 | } |
| 1395 | |
| 1396 | static int should_pack_refs(struct files_ref_store *refs, |
| 1397 | struct refs_optimize_opts *opts) |
| 1398 | { |
| 1399 | struct ref_iterator *iter; |
| 1400 | size_t packed_size; |
| 1401 | size_t refcount = 0; |
| 1402 | size_t limit; |
| 1403 | int ret; |
| 1404 | |
| 1405 | if (!(opts->flags & REFS_OPTIMIZE_AUTO)) |
| 1406 | return 1; |
| 1407 | |
| 1408 | ret = packed_refs_size(refs->packed_ref_store, &packed_size); |
| 1409 | if (ret < 0) |
| 1410 | die("cannot determine packed-refs size"); |
| 1411 | |
| 1412 | /* |
| 1413 | * Packing loose references into the packed-refs file scales with the |
| 1414 | * number of references we're about to write. We thus decide whether we |
| 1415 | * repack refs by weighing the current size of the packed-refs file |
| 1416 | * against the number of loose references. This is done such that we do |
| 1417 | * not repack too often on repositories with a huge number of |
| 1418 | * references, where we can expect a lot of churn in the number of |
| 1419 | * references. |
| 1420 | * |
| 1421 | * As a heuristic, we repack if the number of loose references in the |
| 1422 | * repository exceeds `log2(nr_packed_refs) * 5`, where we estimate |
| 1423 | * `nr_packed_refs = packed_size / 100`, which scales as following: |
| 1424 | * |
| 1425 | * - 1kB ~ 10 packed refs: 16 refs |
| 1426 | * - 10kB ~ 100 packed refs: 33 refs |
| 1427 | * - 100kB ~ 1k packed refs: 49 refs |
| 1428 | * - 1MB ~ 10k packed refs: 66 refs |
| 1429 | * - 10MB ~ 100k packed refs: 82 refs |
| 1430 | * - 100MB ~ 1m packed refs: 99 refs |
| 1431 | * |
| 1432 | * We thus allow roughly 16 additional loose refs per factor of ten of |
| 1433 | * packed refs. This heuristic may be tweaked in the future, but should |
| 1434 | * serve as a sufficiently good first iteration. |
| 1435 | */ |
| 1436 | limit = log2u(packed_size / 100) * 5; |
| 1437 | if (limit < 16) |
| 1438 | limit = 16; |
| 1439 | |
| 1440 | iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL, |
| 1441 | refs->base.repo, 0); |
| 1442 | while ((ret = ref_iterator_advance(iter)) == ITER_OK) { |
| 1443 | if (should_pack_ref(refs, &iter->ref, opts)) |
| 1444 | refcount++; |
| 1445 | if (refcount >= limit) { |
| 1446 | ref_iterator_free(iter); |
| 1447 | return 1; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | if (ret != ITER_DONE) |
| 1452 | die("error while iterating over references"); |
| 1453 |
no test coverage detected