[PATCH v4 3/7] malloc: replace _int_free by _int_free_chunk in sysmalloc.
Cupertino Miranda
cupertino.miranda@oracle.com
Mon Mar 17 14:43:11 GMT 2025
On 17-03-2025 14:20, Wilco Dijkstra wrote:
> Hi DJ/Cupertino,
>
>>> The idea hwere is to pre-load tcache if/when we happen across chunks the
>>> right size.
>> Right, it makes sense. I understood the benefit for the original code.
>
> These are odd sizes created after splitting off the tail. The size was never requested by
> the user, so if we place it in tcache, it may block merging and increase fragmentation.
> So it's hard to see how it makes sense.
>
> To reduce this external fragmentation we should first check whether it makes sense
> to split the block (eg. if the split block is smallish and a tiny fraction of the overall
> size, don't even split it), and add it to the chunk free lists so that it can be merged if
> it remains unused.
>
>>> Are you saying here that only saving chunks the application
>>> free's is overall faster than pre-loading the cache?
>
> I benchmarked it, and it doesn't have an effect on performance. It may actually be
> faster when splitting a large block (such as the top block) where trying tcache is
> unnecessary overhead.
>
>> This was rather a solution to not allow to cache all chunks that would
>> not be related to actual frees from within the application.
>> When you only freeing chunks to size up to ~1k, that is not problematic
>> at all, but once above that limit (as enabled in patch 4) it is rather
>> not useful, since it is unlikely that the application will request a
>> chunk of a similar size.
>
> And the same applies to smaller sizes. We don't prefill tcache with all possible
> block sizes for good reasons.
Right, I was getting myself confused thinking it was actually preloading
all bins. It only preloads when you attempt to malloc specific size.
>
>
> Note there is one more use of _int_free in _int_realloc that is also splitting a block:
>
> set_head (remainder, remainder_size | PREV_INUSE |
> (av != &main_arena ? NON_MAIN_ARENA : 0));
> /* Mark remainder as inuse so free() won't complain */
> set_inuse_bit_at_offset (remainder, remainder_size);
> _int_free (av, remainder, 1);
> }
Indeed, any split should give back remainder to arena.
>
>
> Cheers,
> Wilco
More information about the Libc-alpha
mailing list