Обсуждение: Saving bytes in custom data type
Hello,
I'm writing a variable size custom datatype in C. The variable part is
an array of unsigned long, and it needs to be aligned. I further need
to store a few flags, for which a single byte would be more than
enough (I would actually need just a single bit, but I'd probably keep
some bits to store a data version too).
Using a struct like
{
char vl_len_[4]; /* varlena header */
unsigned char flags;
unsigned long data[1];
}
3 bytes are always wasted in padding as offsetof(data) is 8.
I may complicate fetching a little bit and store the flags at the end
of the data, so that the total size would be 5 + data instead of 8 +
data, and access them with some pointers arithmetic.
In terms of disk space, does it worth the hassle or (as I suspect)
would this effort be wasted by on-disk alignment of the data in the
rows?
Thanks,
-- Daniele
On Wed, Mar 16, 2011 at 12:19 PM, Daniele Varrazzo
<daniele.varrazzo@gmail.com> wrote:
> Hello,
>
> I'm writing a variable size custom datatype in C. The variable part is
> an array of unsigned long, and it needs to be aligned. I further need
> to store a few flags, for which a single byte would be more than
> enough (I would actually need just a single bit, but I'd probably keep
> some bits to store a data version too).
>
> Using a struct like
>
> {
> char vl_len_[4]; /* varlena header */
> unsigned char flags;
> unsigned long data[1];
> }
>
> 3 bytes are always wasted in padding as offsetof(data) is 8.
>
> I may complicate fetching a little bit and store the flags at the end
> of the data, so that the total size would be 5 + data instead of 8 +
> data, and access them with some pointers arithmetic.
>
> In terms of disk space, does it worth the hassle or (as I suspect)
> would this effort be wasted by on-disk alignment of the data in the
> rows?
question: if you are storing just flags and bytes, why not use a bytea
and store the flags out of line?
merlin
On Wed, Mar 16, 2011 at 6:29 PM, Merlin Moncure <mmoncure@gmail.com> wrote: > question: if you are storing just flags and bytes, why not use a bytea > and store the flags out of line? I'm not sure I understand your question. I am writing a custom datatype with variable size more or less following the guidelines in <http://www.postgresql.org/docs/9.0/static/xtypes.html>. More in details I am hacking at a GMP wrapper <http://pgmp.projects.postgresql.org/> and trying to store GMP bignums, which I can re-create by just storing a variable list of unsigned longs and a flag for the sign (note that this is not the structure the GMP library use, but is enough data to re-create it and one of the possible form of storage as a varlena - I'm just exploring to decide the best one). What do you mean for storing the flag out of line? -- Daniele