[fcxml.c] Embed a static 64-byte attr buffer in FcPStack
Reduces number of mallocs called from FcConfigSaveAttr in my small test from 160 down to 6.
This commit is contained in:
parent
39861b7d9c
commit
de69ee14d3
|
@ -716,8 +716,8 @@ FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
|
||||||
buf->size = size;
|
buf->size = size;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
buf->buf = buf->static_buf;
|
buf->buf = buf->buf_static;
|
||||||
buf->size = sizeof (buf->static_buf);
|
buf->size = sizeof (buf->buf_static);
|
||||||
}
|
}
|
||||||
buf->allocated = FcFalse;
|
buf->allocated = FcFalse;
|
||||||
buf->failed = FcFalse;
|
buf->failed = FcFalse;
|
||||||
|
|
23
src/fcxml.c
23
src/fcxml.c
|
@ -414,6 +414,7 @@ typedef struct _FcPStack {
|
||||||
FcElement element;
|
FcElement element;
|
||||||
FcChar8 **attr;
|
FcChar8 **attr;
|
||||||
FcStrBuf str;
|
FcStrBuf str;
|
||||||
|
FcChar8 *attr_buf_static[16];
|
||||||
} FcPStack;
|
} FcPStack;
|
||||||
|
|
||||||
typedef enum _FcVStackTag {
|
typedef enum _FcVStackTag {
|
||||||
|
@ -920,7 +921,7 @@ FcVStackElements (FcConfigParse *parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
static FcChar8 **
|
static FcChar8 **
|
||||||
FcConfigSaveAttr (const XML_Char **attr)
|
FcConfigSaveAttr (const XML_Char **attr, FcChar8 **buf, int size_bytes)
|
||||||
{
|
{
|
||||||
int slen;
|
int slen;
|
||||||
int i;
|
int i;
|
||||||
|
@ -934,13 +935,19 @@ FcConfigSaveAttr (const XML_Char **attr)
|
||||||
slen += strlen ((char *) attr[i]) + 1;
|
slen += strlen ((char *) attr[i]) + 1;
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return 0;
|
return 0;
|
||||||
new = malloc ((i + 1) * sizeof (FcChar8 *) + slen);
|
slen += (i + 1) * sizeof (FcChar8 *);
|
||||||
if (!new)
|
if (slen <= size_bytes)
|
||||||
|
new = buf;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
FcConfigMessage (0, FcSevereError, "out of memory");
|
new = malloc (slen);
|
||||||
return 0;
|
if (!new)
|
||||||
|
{
|
||||||
|
FcConfigMessage (0, FcSevereError, "out of memory");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
FcMemAlloc (FC_MEM_ATTR, 1); /* size is too expensive */
|
||||||
}
|
}
|
||||||
FcMemAlloc (FC_MEM_ATTR, 1); /* size is too expensive */
|
|
||||||
s = (FcChar8 *) (new + (i + 1));
|
s = (FcChar8 *) (new + (i + 1));
|
||||||
for (i = 0; attr[i]; i++)
|
for (i = 0; attr[i]; i++)
|
||||||
{
|
{
|
||||||
|
@ -969,7 +976,7 @@ FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr)
|
||||||
|
|
||||||
new->prev = parse->pstack;
|
new->prev = parse->pstack;
|
||||||
new->element = element;
|
new->element = element;
|
||||||
new->attr = FcConfigSaveAttr (attr);
|
new->attr = FcConfigSaveAttr (attr, new->attr_buf_static, sizeof (new->attr_buf_static));
|
||||||
FcStrBufInit (&new->str, 0, 0);
|
FcStrBufInit (&new->str, 0, 0);
|
||||||
parse->pstack = new;
|
parse->pstack = new;
|
||||||
return FcTrue;
|
return FcTrue;
|
||||||
|
@ -989,7 +996,7 @@ FcPStackPop (FcConfigParse *parse)
|
||||||
old = parse->pstack;
|
old = parse->pstack;
|
||||||
parse->pstack = old->prev;
|
parse->pstack = old->prev;
|
||||||
FcStrBufDestroy (&old->str);
|
FcStrBufDestroy (&old->str);
|
||||||
if (old->attr)
|
if (old->attr && old->attr != old->attr_buf_static)
|
||||||
{
|
{
|
||||||
FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */
|
FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */
|
||||||
free (old->attr);
|
free (old->attr);
|
||||||
|
|
Loading…
Reference in New Issue