1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-24 13:33:01 +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:
Nick Wellnhofer
2023-11-05 00:49:40 +01:00
parent 5859849454
commit 70cc45b81f

140
parser.c
View File

@@ -111,7 +111,6 @@ struct _xmlParserNsData {
};
struct _xmlAttrHashBucket {
unsigned hashValue;
int index;
};
@@ -9393,11 +9392,11 @@ xmlParseAttribute2(xmlParserCtxtPtr ctxt,
/**
* xmlAttrHashInsert:
* @ctxt: parser context
* @aindex: attribute index (this is a multiple of 5)
* @sizePtr: size of the hash table (input/output value)
* @size: size of the hash table
* @name: attribute name
* @uri: namespace 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.
*
@@ -9405,19 +9404,16 @@ xmlParseAttribute2(xmlParserCtxtPtr ctxt,
* index if an attribute was found, -1 if a memory allocation failed.
*/
static int
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, int aindex, unsigned *sizePtr,
const xmlChar *name, const xmlChar *uri,
unsigned hashValue) {
xmlAttrHashInsert(xmlParserCtxtPtr ctxt, unsigned size, const xmlChar *name,
const xmlChar *uri, unsigned hashValue, int aindex) {
xmlAttrHashBucket *table = ctxt->attrHash;
xmlAttrHashBucket *bucket;
unsigned hindex;
unsigned size = *sizePtr;
if (size > 0) {
hindex = hashValue & (size - 1);
bucket = &table[hindex];
while (bucket->hashValue != 0) {
while (bucket->index >= 0) {
const xmlChar **atts = &ctxt->atts[bucket->index];
if (name == atts[0]) {
@@ -9436,89 +9432,7 @@ xmlAttrHashInsert(xmlParserCtxtPtr ctxt, int aindex, unsigned *sizePtr,
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;
return(INT_MAX);
@@ -9570,7 +9484,7 @@ xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
unsigned attrHashSize = 0;
int maxatts = ctxt->maxatts;
int nratts, nbatts, nbdef, inputid;
int i, j, nbNs, attval, nsIndex;
int i, j, nbNs, nbTotalDef, attval, nsIndex, maxAtts;
int alloc = 0;
if (RAW != '<') return(NULL);
@@ -9581,6 +9495,7 @@ xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
nratts = 0;
nbdef = 0;
nbNs = 0;
nbTotalDef = 0;
attval = 0;
if (xmlParserNsStartElement(ctxt->nsdb) < 0) {
@@ -9848,6 +9763,8 @@ next_attr:
if (xmlParserNsPush(ctxt, &attr->name, &attr->value,
NULL, 1) > 0)
nbNs++;
} else {
nbTotalDef += 1;
}
}
}
@@ -9882,9 +9799,34 @@ next_attr:
atts[i+2] = (const xmlChar *) (ptrdiff_t) nsIndex;
}
/*
* Maximum number of attributes including default attributes.
*/
maxAtts = nratts + nbTotalDef;
/*
* 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++) {
const xmlChar *nsuri;
unsigned hashValue, nameHashValue, uriHashValue;
@@ -9908,8 +9850,8 @@ next_attr:
}
hashValue = xmlDictCombineHash(nameHashValue, uriHashValue);
res = xmlAttrHashInsert(ctxt, i, &attrHashSize, attname, nsuri,
hashValue);
res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
hashValue, i);
if (res < 0)
continue;
@@ -9929,6 +9871,7 @@ next_attr:
}
}
}
}
/*
* Default attributes
@@ -9979,9 +9922,11 @@ next_attr:
/*
* Check whether the attribute exists
*/
hashValue = xmlDictCombineHash(attr->name.hashValue, uriHashValue);
res = xmlAttrHashInsert(ctxt, nbatts, &attrHashSize, attname,
nsuri, hashValue);
if (maxAtts > 1) {
hashValue = xmlDictCombineHash(attr->name.hashValue,
uriHashValue);
res = xmlAttrHashInsert(ctxt, attrHashSize, attname, nsuri,
hashValue, nbatts);
if (res < 0)
continue;
if (res < INT_MAX) {
@@ -9991,6 +9936,7 @@ next_attr:
"Namespaced Attribute %s in '%s' redefined\n",
attname, nsuri, NULL);
}
}
xmlParserEntityCheck(ctxt, attr->expandedSize);