[svn:parrot] r45336 - in branches/avl_string_cache: include/parrot src/string

bacek at svn.parrot.org bacek at svn.parrot.org
Wed Mar 31 05:07:47 UTC 2010


Author: bacek
Date: Wed Mar 31 05:07:47 2010
New Revision: 45336
URL: https://trac.parrot.org/parrot/changeset/45336

Log:
Initial implemenation of string cache based on AVL tree

Modified:
   branches/avl_string_cache/include/parrot/string.h
   branches/avl_string_cache/src/string/api.c

Modified: branches/avl_string_cache/include/parrot/string.h
==============================================================================
--- branches/avl_string_cache/include/parrot/string.h	Wed Mar 31 05:07:27 2010	(r45335)
+++ branches/avl_string_cache/include/parrot/string.h	Wed Mar 31 05:07:47 2010	(r45336)
@@ -20,6 +20,7 @@
 
 #include "parrot/pobj.h"
 #include "parrot/parrot.h"
+#include "parrot/avl_tree.h"
 
 typedef struct parrot_string_t STRING;
 
@@ -53,6 +54,25 @@
 
 /* &end_gen */
 
+
+typedef struct avl_string_node_t AVLStringNode;
+
+/* AVL node. */
+/* We duplicate valuable information to avoid messing with STRING
+ * and simplify lookups */
+struct avl_string_node_t
+{
+    const char *      str;
+    size_t            length;
+    struct _encoding *encoding;
+    struct _charset  *charset;
+
+    STRING                  *value;
+
+    TREE_ENTRY(avl_string_node_t)   tree;
+};
+
+
 #endif /* PARROT_IN_CORE */
 #endif /* PARROT_STRING_H_GUARD */
 

Modified: branches/avl_string_cache/src/string/api.c
==============================================================================
--- branches/avl_string_cache/src/string/api.c	Wed Mar 31 05:07:27 2010	(r45335)
+++ branches/avl_string_cache/src/string/api.c	Wed Mar 31 05:07:47 2010	(r45336)
@@ -713,6 +713,22 @@
         "invalid string representation");
 }
 
+static int
+string_node_compare(AVLStringNode *lhs, AVLStringNode *rhs)
+{
+    if (lhs->length < rhs->length)
+        return -1;
+    else if (lhs->length > rhs->length)
+        return 1;
+    /* TODO Check encoding and charset */
+
+    return strcmp(lhs->str, rhs->str);
+}
+
+typedef TREE_HEAD(_Tree, avl_string_node_t) ConstStringTree;
+TREE_DEFINE(avl_string_node_t, tree);
+
+
 /*
 
 =item C<STRING * Parrot_str_new_constant(PARROT_INTERP, const char *buffer)>
@@ -731,22 +747,36 @@
 {
     ASSERT_ARGS(Parrot_str_new_constant)
     DECL_CONST_CAST;
-    STRING *s;
-    Hash   * const cstring_cache = (Hash *)interp->const_cstring_hash;
 
-    s = (STRING *)parrot_hash_get(interp, cstring_cache, buffer);
+    static ConstStringTree tree = TREE_INITIALIZER(string_node_compare);
 
-    if (s)
-        return s;
+    /* Lookup old value */
+    AVLStringNode node = {
+        buffer,
+        strlen(buffer),
+        NULL, NULL, NULL
+    };
+
+    AVLStringNode *vv = TREE_FIND(&tree, avl_string_node_t, tree, &node);
+    if (vv)
+        return vv->value;
+
+    /* Not found. Allocate new node and insert */
+    vv = mem_gc_allocate_zeroed_typed(interp, AVLStringNode);
+
+    /* Fill it */
+    vv->str      = node.str;
+    vv->length   = node.length;
+    vv->encoding = node.encoding;
+    vv->charset  = node.charset;
 
-    s = Parrot_str_new_init(interp, buffer, strlen(buffer),
+    vv->value = Parrot_str_new_init(interp, buffer, strlen(buffer),
                        PARROT_DEFAULT_ENCODING, PARROT_DEFAULT_CHARSET,
                        PObj_external_FLAG|PObj_constant_FLAG);
 
-    parrot_hash_put(interp, cstring_cache,
-        PARROT_const_cast(char *, buffer), (void *)s);
+    TREE_INSERT(&tree, avl_string_node_t, tree, vv);
 
-    return s;
+    return vv->value;
 }
 
 


More information about the parrot-commits mailing list