Skip to content

Commit

Permalink
Bug 559715: Micro-optimize nsCSSPropertySet by adjusting types so tha…
Browse files Browse the repository at this point in the history
…t the compiler can do index calculations more efficiently., r=dbaron, a=dholbert_sheriff
  • Loading branch information
Zack Weinberg committed Apr 23, 2010
1 parent 9a2068c commit 590227c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
12 changes: 6 additions & 6 deletions layout/style/nsCSSDataBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,10 @@ nsCSSExpandedDataBlock::ComputeSizeResult
nsCSSExpandedDataBlock::ComputeSize()
{
ComputeSizeResult result = {0, 0};
for (PRUint32 iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
if (!mPropertiesSet.HasPropertyInChunk(iHigh))
continue;
for (PRInt32 iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
continue;
nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
Expand Down Expand Up @@ -808,10 +808,10 @@ nsCSSExpandedDataBlock::Compress(nsCSSCompressedDataBlock **aNormalBlock,
* corresponding to the stored data in the expanded block, and then
* clearing the data in the expanded block.
*/
for (PRUint32 iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
if (!mPropertiesSet.HasPropertyInChunk(iHigh))
continue;
for (PRInt32 iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
continue;
nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
Expand Down Expand Up @@ -896,10 +896,10 @@ nsCSSExpandedDataBlock::Compress(nsCSSCompressedDataBlock **aNormalBlock,
void
nsCSSExpandedDataBlock::Clear()
{
for (PRUint32 iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
for (size_t iHigh = 0; iHigh < nsCSSPropertySet::kChunkCount; ++iHigh) {
if (!mPropertiesSet.HasPropertyInChunk(iHigh))
continue;
for (PRInt32 iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
for (size_t iLow = 0; iLow < nsCSSPropertySet::kBitsInChunk; ++iLow) {
if (!mPropertiesSet.HasPropertyAt(iHigh, iLow))
continue;
nsCSSProperty iProp = nsCSSPropertySet::CSSPropertyAt(iHigh, iLow);
Expand Down
42 changes: 24 additions & 18 deletions layout/style/nsCSSPropertySet.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define nsCSSPropertySet_h__

#include "nsCSSProperty.h"
#include <limits.h> // for CHAR_BIT

/**
* nsCSSPropertySet maintains a set of non-shorthand CSS properties. In
Expand All @@ -57,56 +58,61 @@ class nsCSSPropertySet {
"out of bounds");
}

// Conversion of aProperty to |size_t| after AssertInSetRange
// lets the compiler generate significantly tighter code.

void AddProperty(nsCSSProperty aProperty) {
AssertInSetRange(aProperty);
mProperties[aProperty / kBitsInChunk] |=
property_set_type(1 << (aProperty % kBitsInChunk));
size_t p = aProperty;
mProperties[p / kBitsInChunk] |=
property_set_type(1) << (p % kBitsInChunk);
}

void RemoveProperty(nsCSSProperty aProperty) {
AssertInSetRange(aProperty);
mProperties[aProperty / kBitsInChunk] &=
~property_set_type(1 << (aProperty % kBitsInChunk));
size_t p = aProperty;
mProperties[p / kBitsInChunk] &=
~(property_set_type(1) << (p % kBitsInChunk));
}

PRBool HasProperty(nsCSSProperty aProperty) const {
bool HasProperty(nsCSSProperty aProperty) const {
AssertInSetRange(aProperty);
return (mProperties[aProperty / kBitsInChunk] &
(1 << (aProperty % kBitsInChunk))) != 0;
size_t p = aProperty;
return (mProperties[p / kBitsInChunk] &
(property_set_type(1) << (p % kBitsInChunk))) != 0;
}

void Empty() {
memset(mProperties, 0, sizeof(mProperties));
}

void AssertIsEmpty(const char* aText) const {
for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(mProperties); ++i) {
for (size_t i = 0; i < NS_ARRAY_LENGTH(mProperties); ++i) {
NS_ASSERTION(mProperties[i] == 0, aText);
}
}

private:
typedef PRUint8 property_set_type;
typedef unsigned long property_set_type;
public:
enum { kBitsInChunk = 8 }; // number of bits in
// |property_set_type|.
// number of bits in |property_set_type|.
static const size_t kBitsInChunk = sizeof(property_set_type)*CHAR_BIT;
// number of |property_set_type|s in the set
enum { kChunkCount =
(eCSSProperty_COUNT_no_shorthands + (kBitsInChunk-1)) /
kBitsInChunk };
static const size_t kChunkCount =
(eCSSProperty_COUNT_no_shorthands + kBitsInChunk - 1) / kBitsInChunk;

/*
* For fast enumeration of all the bits that are set, callers can
* check each chunk against zero (since in normal cases few bits are
* likely to be set).
*/
PRBool HasPropertyInChunk(PRUint32 aChunk) const {
bool HasPropertyInChunk(size_t aChunk) const {
return mProperties[aChunk] != 0;
}
PRBool HasPropertyAt(PRUint32 aChunk, PRInt32 aBit) const {
return (mProperties[aChunk] & (1 << aBit)) != 0;
bool HasPropertyAt(size_t aChunk, size_t aBit) const {
return (mProperties[aChunk] & (property_set_type(1) << aBit)) != 0;
}
static nsCSSProperty CSSPropertyAt(PRUint32 aChunk, PRInt32 aBit) {
static nsCSSProperty CSSPropertyAt(size_t aChunk, size_t aBit) {
return nsCSSProperty(aChunk * kBitsInChunk + aBit);
}

Expand Down

0 comments on commit 590227c

Please sign in to comment.