Re: [PATCH v2 4/7] index-pack: remove redundant child field
- From
Josh Steadmon <steadmon@google.com>
- Date
- Feb 28, 2020, 00:04 UTC
- Message-ID
- <20200228000437.GD12115@google.com>
- In-Reply-To
- <3359b66b841e7eabdc45d3d937e97208b22e2901.1571343096.git.jonathantanmy@google.com>
On 2019.10.17 13:17, Jonathan Tan wrote:
Show 37 quoted lines
> @@ -380,27 +378,37 @@ static void free_base_data(struct base_data *c)
> }
> }
>
> -static void prune_base_data(struct base_data *retain)
> +static void prune_base_data(struct base_data *youngest_child)
> {
> struct base_data *b;
> struct thread_local *data = get_thread_data();
> - for (b = data->base_cache;
> - data->base_cache_used > delta_base_cache_limit && b;
> - b = b->child) {
> - if (b->data && b != retain)
> - free_base_data(b);
> + struct base_data **ancestry = NULL;
> + size_t nr = 0, alloc = 0;
> + ssize_t i;
> +
> + if (data->base_cache_used <= delta_base_cache_limit)
> + return;
> +
> + /*
> + * Free all ancestors of youngest_child until we have enough space,
> + * starting with the oldest. (We cannot free youngest_child itself.)
> + */
> + for (b = youngest_child->base; b != NULL; b = b->base) {
> + ALLOC_GROW(ancestry, nr + 1, alloc);
> + ancestry[nr++] = b;
> }
> + for (i = nr - 1;
> + i >= 0 && data->base_cache_used > delta_base_cache_limit;
> + i--) {
> + if (ancestry[i]->data)
> + free_base_data(ancestry[i]);
> + }
> + free(ancestry);
> }I had a small complaint that we're allocating new memory in a function where we are trying to free up space, but in practice it probably doesn't matter. And this is removed in a later patch anyway.