canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
16 stars 8 forks source link

Remove unnecessary allocation when scanning unused columns #113

Closed letFunny closed 8 months ago

letFunny commented 9 months ago

We need to add pointers for all of the columns returned by the query but we only care about the ones that contain output expressions. For those unused, we use sql.RawBytes that avoids extra allocations.

letFunny commented 8 months ago

You are right, if you look at the source code the allocation cost is actually pretty similar and that is without taking into account that RawBytes holds a lock for longer.

I am tempted to go for the empty struct with the Scanner as the sql library only does one call without assignments (ref) but it is still not clear to me if there is any benefit because we are still boxing the type to convert it to the Scanner interface requiring one allocation.

I feel this PR would actually be a form of premature optimization for not a clear win so I am closing it until the need arises.

https://github.com/canonical/sqlair/pull/113#discussion_r1369944251