lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [lucy-commits] [19/50] [abbrv] git commit: refs/heads/master - Rework cloning of Strings
Date Fri, 11 Oct 2013 13:31:57 GMT
Rework cloning of Strings

Now that strings are immutable, Clone and To_String can simply increase
the refcount and return 'self'. Only wrapped strings have to be copied.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/0b4bf991
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/0b4bf991
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/0b4bf991

Branch: refs/heads/master
Commit: 0b4bf991aabecd9886ccb1f54d8637670d92b826
Parents: 7ba4174
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Sep 14 21:08:56 2013 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Fri Oct 11 15:28:17 2013 +0200

----------------------------------------------------------------------
 clownfish/runtime/core/Clownfish/String.c         | 18 ++++++++++++++++--
 clownfish/runtime/core/Clownfish/String.cfh       |  3 +++
 clownfish/runtime/core/Clownfish/Test/TestHash.c  |  7 ++++---
 .../runtime/core/Clownfish/Test/TestVArray.c      |  2 +-
 4 files changed, 24 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/0b4bf991/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index c917a79..e9b0936 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -164,6 +164,20 @@ S_new_substring(String *origin, size_t byte_offset, size_t size) {
     return self;
 }
 
+Obj*
+Str_Inc_RefCount_IMP(String *self) {
+    if (self->origin == NULL) {
+        // Copy wrapped strings when the refcount is increased.
+        String *copy = (String*)VTable_Make_Obj(STRING);
+        return (Obj*)Str_init_from_trusted_utf8(copy, self->ptr, self->size);
+    }
+    else {
+        Str_Inc_RefCount_t super_incref
+            = SUPER_METHOD_PTR(STRING, CFISH_Str_Inc_RefCount);
+        return super_incref(self);
+    }
+}
+
 void
 Str_Destroy_IMP(String *self) {
     if (self->origin == self) {
@@ -202,7 +216,7 @@ S_die_invalid_utf8(const char *text, size_t size, const char *file, int
line,
 
 String*
 Str_To_String_IMP(String *self) {
-    return Str_new_from_trusted_utf8(self->ptr, self->size);
+    return (String*)INCREF(self);
 }
 
 String*
@@ -291,7 +305,7 @@ Str_To_Utf8_IMP(String *self) {
 
 String*
 Str_Clone_IMP(String *self) {
-    return Str_new_from_trusted_utf8(self->ptr, self->size);
+    return (String*)INCREF(self);
 }
 
 String*

http://git-wip-us.apache.org/repos/asf/lucy/blob/0b4bf991/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index a1a0378..e060aa5 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -203,6 +203,9 @@ class Clownfish::String cnick Str
     public incremented String*
     Clone(String *self);
 
+    incremented Obj*
+    Inc_RefCount(String *self);
+
     public void
     Destroy(String *self);
 

http://git-wip-us.apache.org/repos/asf/lucy/blob/0b4bf991/clownfish/runtime/core/Clownfish/Test/TestHash.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/Test/TestHash.c b/clownfish/runtime/core/Clownfish/Test/TestHash.c
index ca35f1a..cb93b2e 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestHash.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestHash.c
@@ -97,7 +97,8 @@ test_Store_and_Fetch(TestBatchRunner *runner) {
     TEST_TRUE(runner, Hash_Fetch(hash, (Obj*)foo) == NULL,
               "Fetch against non-existent key returns NULL");
 
-    Hash_Store(hash, (Obj*)forty, INCREF(foo));
+    Obj *stored_foo = INCREF(foo);
+    Hash_Store(hash, (Obj*)forty, stored_foo);
     TEST_TRUE(runner, SStr_Equals(foo, Hash_Fetch(hash, (Obj*)forty)),
               "Hash_Store replaces existing value");
     TEST_FALSE(runner, Hash_Equals(hash, (Obj*)dupe),
@@ -105,9 +106,9 @@ test_Store_and_Fetch(TestBatchRunner *runner) {
     TEST_INT_EQ(runner, Hash_Get_Size(hash), 100,
                 "size unaffected after value replaced");
 
-    TEST_TRUE(runner, Hash_Delete(hash, (Obj*)forty) == (Obj*)foo,
+    TEST_TRUE(runner, Hash_Delete(hash, (Obj*)forty) == stored_foo,
               "Delete returns value");
-    DECREF(foo);
+    DECREF(stored_foo);
     TEST_INT_EQ(runner, Hash_Get_Size(hash), 99,
                 "size decremented by successful Delete");
     TEST_TRUE(runner, Hash_Delete(hash, (Obj*)forty) == NULL,

http://git-wip-us.apache.org/repos/asf/lucy/blob/0b4bf991/clownfish/runtime/core/Clownfish/Test/TestVArray.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/Test/TestVArray.c b/clownfish/runtime/core/Clownfish/Test/TestVArray.c
index 1abb89e..4aa9ccd 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestVArray.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestVArray.c
@@ -278,7 +278,7 @@ test_Clone_and_Shallow_Copy(TestBatchRunner *runner) {
     uint32_t i;
 
     for (i = 0; i < 10; i++) {
-        VA_Push(array, (Obj*)Str_newf("%u32", i));
+        VA_Push(array, (Obj*)Int32_new(i));
     }
     twin = VA_Shallow_Copy(array);
     TEST_TRUE(runner, VA_Equals(array, (Obj*)twin), "Shallow_Copy");


Mime
View raw message