[webkit-dev] PSA: Bit fields won't be packed on Windows if you mix types
Ryosuke Niwa
rniwa at webkit.org
Thu Mar 29 01:21:48 PDT 2012
Hi,
Unlike gcc and clang, MSVC pads each consecutive member variables of the
same type in bitfields. e.g. if you have:
struct AB {
unsigned m_1 : 31;
bool m_2 : 1;
}
then *MSVC pads m_1 and allocates sizeof(unsigned) * 2 for AB* whereas gcc
and clang only allocate sizeof(unsigned) * 1 for AB.
This is *not a compiler bug*. It's a spec. compliant behavior, and may in
fact have a better run-time performance in some situations. However, for
core objects like RenderObject and InlineBox, allocating extra 4-8 bytes
per each object is prohibitory expensive.
In such cases, please *use the same POD type for all bitfield member
variables*. (Storage class classifiers and variable qualifiers seem to have
no effect on how variables are packed; e.g. mutable, const, etc...). For
example, MSVC will allocate sizeof(unsigned) * 1 for AB if we rewrite the
above code as:
struct AB {
unsigned m_1 : 31;
unsigned m_2 : 1;
}
When you're making this change, *be sure to audit all code that assigns a
non-boolean value to m_2* because implicit type coercion into boolean is no
longer in effect. For example,
AB ab;
ab.m_2 = 2;
puts(ab.m_2 ? "true" : "false");
will print "true" before the change and will print "false" after the
change. An easy way to ensure you've audited all such code is to add
getters and setters for all bitfield member variables or wrap them in a
special structure or class as done in
http://trac.webkit.org/changeset/103353 and
http://trac.webkit.org/changeset/104254.
Best regards,
Ryosuke Niwa
Software Engineer
Google Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20120329/982775f2/attachment.html>
More information about the webkit-dev
mailing list