Permalink
Browse files

cleaning up unused vars in tests and also cache8

  • Loading branch information...
ohler55 committed Oct 23, 2012
1 parent 82d757e commit 9fda2e48f3a770da55f4f6bf2ac9dabfe6b518b8
Showing with 54 additions and 80 deletions.
  1. +41 −67 ext/oj/cache8.c
  2. +8 −8 ext/oj/cache8.h
  3. +3 −3 test/test_fast.rb
  4. +2 −2 test/tests.rb
View
@@ -1,66 +1,38 @@
-/* cache8.c
- * Copyright (c) 2011, Peter Ohler
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * - Neither the name of Peter Ohler nor the names of its contributors may be
- * used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
+#include "ruby.h"
#include "cache8.h"
-#define BITS 4
-#define MASK 0x000000000000000F
-#define SLOT_CNT 16
-#define DEPTH 16
+#define BITS 4
+#define MASK 0x000000000000000FULL
+#define SLOT_CNT 16
+#define DEPTH 16
+
+typedef union {
+ struct _Cache8 *child;
+ slot_t value;
+} Bucket;
struct _Cache8 {
- union {
- struct _Cache8 *slots[SLOT_CNT];
- slot_t values[SLOT_CNT];
- };
+ Bucket buckets[SLOT_CNT];
};
static void cache8_delete(Cache8 cache, int depth);
-static void slot_print(Cache8 cache, VALUE key, unsigned int depth);
+static void slot_print(Cache8 cache, sid_t key, unsigned int depth);
void
oj_cache8_new(Cache8 *cache) {
- Cache8 *cp;
+ Bucket *b;
int i;
- if (0 == (*cache = ALLOC(struct _Cache8))) {
- rb_raise(rb_eNoMemError, "not enough memory\n");
- }
- for (i = SLOT_CNT, cp = (*cache)->slots; 0 < i; i--, cp++) {
- *cp = 0;
+ *cache = ALLOC(struct _Cache8);
+ for (i = SLOT_CNT, b = (*cache)->buckets; 0 < i; i--, b++) {
+ b->value = 0;
}
}
@@ -71,58 +43,60 @@ oj_cache8_delete(Cache8 cache) {
static void
cache8_delete(Cache8 cache, int depth) {
- Cache8 *cp;
+ Bucket *b;
unsigned int i;
- for (i = 0, cp = cache->slots; i < SLOT_CNT; i++, cp++) {
- if (0 != *cp) {
+ for (i = 0, b = cache->buckets; i < SLOT_CNT; i++, b++) {
+ if (0 != b->child) {
if (DEPTH - 1 != depth) {
- cache8_delete(*cp, depth + 1);
+ cache8_delete(b->child, depth + 1);
}
}
}
xfree(cache);
}
slot_t
-oj_cache8_get(Cache8 cache, VALUE key, slot_t **slot) {
- Cache8 *cp;
+oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot) {
+ Bucket *b;
int i;
- VALUE k;
+ sid_t k8 = (sid_t)key;
+ sid_t k;
for (i = 64 - BITS; 0 < i; i -= BITS) {
- k = (key >> i) & MASK;
- cp = cache->slots + k;
- if (0 == *cp) {
- oj_cache8_new(cp);
+ k = (k8 >> i) & MASK;
+ b = cache->buckets + k;
+ if (0 == b->child) {
+ oj_cache8_new(&b->child);
}
- cache = *cp;
+ cache = b->child;
}
- *slot = cache->values + (key & MASK);
+ *slot = &(cache->buckets + (k8 & MASK))->value;
return **slot;
}
void
oj_cache8_print(Cache8 cache) {
- //printf("-------------------------------------------\n");
+ /*printf("-------------------------------------------\n"); */
slot_print(cache, 0, 0);
}
static void
-slot_print(Cache8 c, VALUE key, unsigned int depth) {
- Cache8 *cp;
+slot_print(Cache8 c, sid_t key, unsigned int depth) {
+ Bucket *b;
unsigned int i;
- unsigned long k;
+ sid_t k8 = (sid_t)key;
+ sid_t k;
- for (i = 0, cp = c->slots; i < SLOT_CNT; i++, cp++) {
- if (0 != *cp) {
- k = (key << BITS) | i;
- //printf("*** key: 0x%016lx depth: %u i: %u\n", k, depth, i);
+ for (i = 0, b = c->buckets; i < SLOT_CNT; i++, b++) {
+ if (0 != b->child) {
+ k = (k8 << BITS) | i;
+ /*printf("*** key: 0x%016llx depth: %u i: %u\n", k, depth, i); */
if (DEPTH - 1 == depth) {
- printf("0x%016lx: %4lu\n", k, (unsigned long)*cp);
+ printf("0x%016llx: %4llu\n", k, b->value);
} else {
- slot_print(*cp, k, depth + 1);
+ slot_print(b->child, k, depth + 1);
}
}
}
View
@@ -31,18 +31,18 @@
#ifndef __OJ_CACHE8_H__
#define __OJ_CACHE8_H__
-#define RSTRING_NOT_MODIFIED
-
#include "ruby.h"
+#include "stdint.h"
-typedef struct _Cache8 *Cache8;
-typedef unsigned long slot_t;
+typedef struct _Cache8 *Cache8;
+typedef uint64_t slot_t;
+typedef uint64_t sid_t;
-extern void oj_cache8_new(Cache8 *cache);
-extern void oj_cache8_delete(Cache8 cache);
+extern void oj_cache8_new(Cache8 *cache);
+extern void oj_cache8_delete(Cache8 cache);
-extern slot_t oj_cache8_get(Cache8 cache, VALUE key, slot_t **slot);
+extern slot_t oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot);
-extern void oj_cache8_print(Cache8 cache);
+extern void oj_cache8_print(Cache8 cache);
#endif /* __OJ_CACHE8_H__ */
View
@@ -151,7 +151,7 @@ def test_move
end
begin
doc.move('/array/x')
- rescue Exception => e
+ rescue Exception
assert_equal('/', doc.where?)
assert(true)
end
@@ -330,7 +330,7 @@ def test_open_close
doc.close()
begin
doc.home()
- rescue Exception => e
+ rescue Exception
assert(true)
end
end
@@ -349,7 +349,7 @@ def test_file_open_close
doc.close()
begin
doc.home()
- rescue Exception => e
+ rescue Exception
assert(true)
end
end
View
@@ -778,7 +778,7 @@ def test_circular_hash
json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
ha = Oj.load(json, :mode => :strict)
assert_equal({'^i' => 1, 'a' => 7, 'b' => '^r1'}, ha)
- h2 = Oj.load(json, :mode => :object, :circular => true)
+ Oj.load(json, :mode => :object, :circular => true)
assert_equal(h['b'].__id__, h.__id__)
end
@@ -801,7 +801,7 @@ def test_circular
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
ha = Oj.load(json, :mode => :strict)
assert_equal({'^o' => 'Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
- obj2 = Oj.load(json, :mode => :object, :circular => true)
+ Oj.load(json, :mode => :object, :circular => true)
assert_equal(obj.x.__id__, h.__id__)
assert_equal(h['b'].__id__, obj.__id__)
end

0 comments on commit 9fda2e4

Please sign in to comment.