mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-26 00:37:43 +03:00
parser: Improve attribute hash table
There's no need to grow the hash table dynamically. The size is known which simplifies the implementation.
This commit is contained in:
140
parser.c
140
parser.c
@@ -111,7 +111,6 @@ struct _xmlParserNsData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct _xmlAttrHashBucket {
|
struct _xmlAttrHashBucket {
|
||||||
unsigned hashValue;
|
|
||||||
int index;
|
int index;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -9393,11 +9392,11 @@ xmlParseAttribute2(xmlParserCtxtPtr ctxt,
|
|||||||
/**
|
/**
|
||||||
* xmlAttrHashInsert:
|
* xmlAttrHashInsert:
|
||||||
* @ctxt: parser context
|
* @ctxt: parser context
|
||||||
* @aindex: attribute index (this is a multiple of 5)
|
* @size: size of the hash table
|
||||||
* @sizePtr: size of the hash table (input/output value)
|
|
||||||
* @name: attribute name
|
* @name: attribute name
|
||||||
* @uri: namespace uri
|
* @uri: namespace uri
|
||||||
* @hashValue: combined hash value of name and uri
|
* @hashValue: combined hash value of name and uri
|
||||||
|
* @aindex: attribute index (this is a multiple of 5)
|
||||||
*
|
*
|
||||||
* Inserts a new attribute into the hash table.
|
* Inserts a new attribute into the hash table.
|
||||||
*
|
*
|
||||||
@@ -9405,19 +9404,16 @@ xmlParseAttribute2(xmlParserCtxtPtr ctxt,
|
|||||||
* index if an attribute was found, -1 if a memory allocation failed.
|
* index if an attribute was found, -1 if a memory allocation failed.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, int aindex, unsigned *sizePtr,
|
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
|
||||||
const xmlChar *name, const xmlChar *uri,
|
const xmlChar *uri, unsigned hashValue, int aindex) {
|
||||||
unsigned hashValue) {
|
|
||||||
xmlAttrHashBucket *table = ctxt->attrHash;
|
xmlAttrHashBucket *table = ctxt->attrHash;
|
||||||
xmlAttrHashBucket *bucket;
|
xmlAttrHashBucket *bucket;
|
||||||
unsigned hindex;
|
unsigned hindex;
|
||||||
unsigned size = *sizePtr;
|
|
||||||
|
|
||||||
if (size > 0) {
|
|
||||||
hindex = hashValue & (size - 1);
|
hindex = hashValue & (size - 1);
|
||||||
bucket = &table[hindex];
|
bucket = &table[hindex];
|
||||||
|
|
||||||
while (bucket->hashValue != 0) {
|
while (bucket->index >= 0) {
|
||||||
const xmlChar **atts = &ctxt->atts[bucket->index];
|
const xmlChar **atts = &ctxt->atts[bucket->index];
|
||||||
|
|
||||||
if (name == atts[0]) {
|
if (name == atts[0]) {
|
||||||
@@ -9436,89 +9432,7 @@ xmlAttrHashInsert(xmlParserCtxtPtr ctxt, int aindex, unsigned *sizePtr,
|
|||||||
bucket = table;
|
bucket = table;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Grow hash table
|
|
||||||
*/
|
|
||||||
if ((unsigned) aindex / 5 >= size / 2) {
|
|
||||||
xmlAttrHashBucket *newTable;
|
|
||||||
unsigned newSize, i, nindex;
|
|
||||||
|
|
||||||
newSize = size ? size * 2 : 8;
|
|
||||||
|
|
||||||
if (newSize > ctxt->attrHashMax) {
|
|
||||||
newTable = xmlRealloc(table, newSize * sizeof(newTable[0]));
|
|
||||||
if (newTable == NULL) {
|
|
||||||
xmlErrMemory(ctxt, NULL);
|
|
||||||
return(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
table = newTable;
|
|
||||||
ctxt->attrHash = newTable;
|
|
||||||
ctxt->attrHashMax = newSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&table[size], 0, (newSize - size) * sizeof(table[0]));
|
|
||||||
|
|
||||||
if (size > 0) {
|
|
||||||
/*
|
|
||||||
* We must search for the start of a probe sequence to make
|
|
||||||
* in-place operation work.
|
|
||||||
*/
|
|
||||||
hindex = 0;
|
|
||||||
bucket = table;
|
|
||||||
while (bucket->hashValue != 0) {
|
|
||||||
hindex++;
|
|
||||||
bucket++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < size; i++) {
|
|
||||||
if (bucket->hashValue != 0) {
|
|
||||||
nindex = bucket->hashValue & (newSize - 1);
|
|
||||||
|
|
||||||
while (nindex != hindex) {
|
|
||||||
if (table[nindex].hashValue == 0) {
|
|
||||||
table[nindex] = *bucket;
|
|
||||||
bucket->hashValue = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
nindex++;
|
|
||||||
if (nindex >= newSize)
|
|
||||||
nindex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hindex++;
|
|
||||||
bucket++;
|
|
||||||
if (hindex >= size) {
|
|
||||||
hindex = 0;
|
|
||||||
bucket = table;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size = newSize;
|
|
||||||
*sizePtr = newSize;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Relookup
|
|
||||||
*/
|
|
||||||
hindex = hashValue & (size - 1);
|
|
||||||
bucket = &table[hindex];
|
|
||||||
|
|
||||||
while (bucket->hashValue != 0) {
|
|
||||||
hindex++;
|
|
||||||
bucket++;
|
|
||||||
if (hindex >= size) {
|
|
||||||
hindex = 0;
|
|
||||||
bucket = table;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bucket->hashValue = hashValue;
|
|
||||||
bucket->index = aindex;
|
bucket->index = aindex;
|
||||||
|
|
||||||
return(INT_MAX);
|
return(INT_MAX);
|
||||||
@@ -9570,7 +9484,7 @@ xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
|
|||||||
unsigned attrHashSize = 0;
|
unsigned attrHashSize = 0;
|
||||||
int maxatts = ctxt->maxatts;
|
int maxatts = ctxt->maxatts;
|
||||||
int nratts, nbatts, nbdef, inputid;
|
int nratts, nbatts, nbdef, inputid;
|
||||||
int i, j, nbNs, attval, nsIndex;
|
int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
|
||||||
int alloc = 0;
|
int alloc = 0;
|
||||||
|
|
||||||
if (RAW != '<') return(NULL);
|
if (RAW != '<') return(NULL);
|
||||||
@@ -9581,6 +9495,7 @@ xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
|
|||||||
nratts = 0;
|
nratts = 0;
|
||||||
nbdef = 0;
|
nbdef = 0;
|
||||||
nbNs = 0;
|
nbNs = 0;
|
||||||
|
nbTotalDef = 0;
|
||||||
attval = 0;
|
attval = 0;
|
||||||
|
|
||||||
if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
|
if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
|
||||||
@@ -9848,6 +9763,8 @@ next_attr:
|
|||||||
if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
|
if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
|
||||||
NULL, 1) > 0)
|
NULL, 1) > 0)
|
||||||
nbNs++;
|
nbNs++;
|
||||||
|
} else {
|
||||||
|
nbTotalDef += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9882,9 +9799,34 @@ next_attr:
|
|||||||
atts[i+2] = (const xmlChar *) (ptrdiff_t) nsIndex;
|
atts[i+2] = (const xmlChar *) (ptrdiff_t) nsIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Maximum number of attributes including default attributes.
|
||||||
|
*/
|
||||||
|
maxAtts = nratts + nbTotalDef;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Verify that attribute names are unique.
|
* Verify that attribute names are unique.
|
||||||
*/
|
*/
|
||||||
|
if (maxAtts > 1) {
|
||||||
|
attrHashSize = 4;
|
||||||
|
while (attrHashSize / 2 < (unsigned) maxAtts)
|
||||||
|
attrHashSize *= 2;
|
||||||
|
|
||||||
|
if (attrHashSize > ctxt->attrHashMax) {
|
||||||
|
xmlAttrHashBucket *tmp;
|
||||||
|
|
||||||
|
tmp = xmlRealloc(ctxt->attrHash, attrHashSize * sizeof(tmp[0]));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
xmlErrMemory(ctxt, NULL);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctxt->attrHash = tmp;
|
||||||
|
ctxt->attrHashMax = attrHashSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(ctxt->attrHash, -1, attrHashSize * sizeof(ctxt->attrHash[0]));
|
||||||
|
|
||||||
for (i = 0, j = 0; j < nratts; i += 5, j++) {
|
for (i = 0, j = 0; j < nratts; i += 5, j++) {
|
||||||
const xmlChar *nsuri;
|
const xmlChar *nsuri;
|
||||||
unsigned hashValue, nameHashValue, uriHashValue;
|
unsigned hashValue, nameHashValue, uriHashValue;
|
||||||
@@ -9908,8 +9850,8 @@ next_attr:
|
|||||||
}
|
}
|
||||||
|
|
||||||
hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
|
hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
|
||||||
res = xmlAttrHashInsert(ctxt, i, &attrHashSize, attname, nsuri,
|
res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
|
||||||
hashValue);
|
hashValue, i);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -9929,6 +9871,7 @@ next_attr:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Default attributes
|
* Default attributes
|
||||||
@@ -9979,9 +9922,11 @@ next_attr:
|
|||||||
/*
|
/*
|
||||||
* Check whether the attribute exists
|
* Check whether the attribute exists
|
||||||
*/
|
*/
|
||||||
hashValue = xmlDictCombineHash(attr->name.hashValue, uriHashValue);
|
if (maxAtts > 1) {
|
||||||
res = xmlAttrHashInsert(ctxt, nbatts, &attrHashSize, attname,
|
hashValue = xmlDictCombineHash(attr->name.hashValue,
|
||||||
nsuri, hashValue);
|
uriHashValue);
|
||||||
|
res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
|
||||||
|
hashValue, nbatts);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
continue;
|
continue;
|
||||||
if (res < INT_MAX) {
|
if (res < INT_MAX) {
|
||||||
@@ -9991,6 +9936,7 @@ next_attr:
|
|||||||
"Namespaced Attribute %s in '%s' redefined\n",
|
"Namespaced Attribute %s in '%s' redefined\n",
|
||||||
attname, nsuri, NULL);
|
attname, nsuri, NULL);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
xmlParserEntityCheck(ctxt, attr->expandedSize);
|
xmlParserEntityCheck(ctxt, attr->expandedSize);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user