ref: 48e3fdb718707b564c2a24d5ee796b3a9a6c703a
parent: 95ac24c7cf5e4e73d60fcf8179c40463a25df680
author: Tor Andersson <tor@ccxvii.net>
date: Sun Jan 19 08:33:29 EST 2014
Alloc interned strings at the end of the string nodes. Only use one malloc rather than two for each interned string.
--- a/jsintern.c
+++ b/jsintern.c
@@ -5,19 +5,20 @@
struct js_StringNode
{
- char *string;
js_StringNode *left, *right;
int level;
+ char string[1];
};
-static js_StringNode sentinel = { "", &sentinel, &sentinel, 0 };
+static js_StringNode sentinel = { &sentinel, &sentinel, 0, ""};
static js_StringNode *newstringnode(const char *string, const char **result)
{
- js_StringNode *node = malloc(sizeof(js_StringNode));
- node->string = strdup(string);
+ int n = strlen(string);
+ js_StringNode *node = malloc(offsetof(js_StringNode, string) + n + 1);
node->left = node->right = &sentinel;
node->level = 1;
+ strcpy(node->string, string);
return *result = node->string, node;
}
@@ -91,7 +92,6 @@
{
if (node->left != &sentinel) js_freestringnode(J, node->left);
if (node->right != &sentinel) js_freestringnode(J, node->right);
- free(node->string);
free(node);
}