[cairo] Using bit fields a bit more in cairo

Carl Worth cworth at cworth.org
Tue Dec 19 13:59:53 PST 2006

On Tue, 19 Dec 2006 13:36:26 -0800, Carl Worth wrote:
> I originally started writing this email because I thought this was a
> feature of C we weren't using in cairo yet. But now I do notice that
> pixman is already using bit fields. So I don't foresee any problem
> here. Some of the problems of bit fields that prevent their use in
> some circumstances, (such as no guarantee of packing order), aren't
> relevant here where we're just using flags and don't care about the
> order.

By the way, there's another potential nuisance with bit fields that I
think is actually a good thing. With a single flags approach, (single
integer manipulated with masks), it's much easier to initialize all
the bits at once:

	path->flags = 0;

or to copy the flags:

	path->flags = other->flags;

compared with flag-by-flag initialization and copying required for bit

	path->has_current_point = 0;
	path->has_curve_to = 0;
	path->has_current_point = other->has_current_point;
	path->has_curve_to = other->curve_to;

So that looks like a nuisance in the bit field approach, but it's
actually a big advantage.

The rationale is that the only cost here is a little extra typing. But
typing is something that's really cheap. Meanwhile, maintainability
(which is much more expensive than typing) is greatly improved.

A similar case is structure copying which can be achieved

	copy->field1 = original->field1;
	copy->field2 = original->field2;

or by using a "shortcut" of structure assignment:

	*copy = *original;

Here again, saving the typing is a bad idea. The reason it's bad is
that this introduces an alias for the way a field is manipulated. If a
programmer wants to audit all manipulations of a given field, the
member-by-member copying makes that a simple matter of "grep" while
the structure assignment hides some manipulations from the grep.

In the early days of cairo I took the structure assignment shortcut a
bunch and really paid for it down the road when I had some obscure bug
I couldn't find, (after changing the semantics of some field and
touching up every manipulation except for the "silent" assignment in
the structure copy).

So the bit-field case works out similarly. It's nice to be able to
grep for something like "has_current_point" and have some assurance
that the results are complete. But in the flags case, grepping for the
HAS_CURRENT_POINT mask value misses the initialization and
assignment. So the poor programmer ends up having to grep for "flags"
to find them, and then that returns more then desired, (giving all
structures with a field name "flags" for example).

Anyway, nothing too important to the current discussion. But this idea
of how bad it can be to "save typing" seemed worth mentioning here.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20061219/69e83612/attachment.pgp

More information about the cairo mailing list