[PATCH] malloc: Improve csize2tidx

Cupertino Miranda cupertino.miranda@oracle.com
Wed Mar 19 11:15:48 GMT 2025


Hi Wilco,

On 19-03-2025 10:17, Wilco Dijkstra wrote:
> Hi Cupertino,
> 
>>>    /* When "x" is from chunksize().  */
>>> -# define csize2tidx(x) (((x) - MINSIZE + MALLOC_ALIGNMENT - 1) / MALLOC_ALIGNMENT)
>>> +# define csize2tidx(x) (((x) - MINSIZE) / MALLOC_ALIGNMENT)
> 
>> Does it make sense to lose precision, considering that all this macros
>> will be optimized to a constant value ?
> 
> I'm not sure what you mean? Like I said, the idea is to use chunksize_nomask()
> in free() and still be sure that only valid sizes are accepted for tcache.
> Basically this macro should be as simple and as fast as possible given that it is
> used before entering the tcache code.
Sorry if I did not properly explained it.
MINSIZE + MALLOC_ALIGNMENT - 1 is optimized by the compiler to a 
constant value.
The assembly generated will be exactly the same in both cases, except 
for the constant value that either expressions results in.

If I am not mistaken the original expression could also be used for 
sizes that would not be already standardized as chunk sizes. This is 
what I referred as losing precision.
> 
> Cheers,
> Wilco



More information about the Libc-alpha mailing list