oj 2.18.5 → 3.16.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1452 -0
- data/README.md +53 -221
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +54 -72
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +61 -63
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +38 -67
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +214 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +194 -110
- data/ext/oj/custom.c +1074 -0
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +1276 -2494
- data/ext/oj/dump.h +110 -0
- data/ext/oj/dump_compat.c +897 -0
- data/ext/oj/dump_leaf.c +162 -0
- data/ext/oj/dump_object.c +710 -0
- data/ext/oj/dump_strict.c +399 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +28 -53
- data/ext/oj/err.h +49 -46
- data/ext/oj/extconf.rb +33 -32
- data/ext/oj/fast.c +1082 -1098
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +919 -0
- data/ext/oj/object.c +545 -625
- data/ext/oj/odd.c +158 -168
- data/ext/oj/odd.h +32 -58
- data/ext/oj/oj.c +1727 -2080
- data/ext/oj/oj.h +334 -259
- data/ext/oj/parse.c +974 -753
- data/ext/oj/parse.h +97 -90
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +103 -0
- data/ext/oj/rails.c +1478 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +136 -163
- data/ext/oj/reader.h +76 -112
- data/ext/oj/resolve.c +45 -94
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +144 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +445 -511
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +82 -143
- data/ext/oj/simd.h +10 -0
- data/ext/oj/sparse.c +749 -644
- data/ext/oj/stream_writer.c +329 -0
- data/ext/oj/strict.c +114 -112
- data/ext/oj/string_writer.c +517 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +55 -0
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +75 -72
- data/ext/oj/val_stack.h +94 -127
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +586 -0
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +8 -1
- data/lib/oj/easy_hash.rb +21 -13
- data/lib/oj/error.rb +10 -12
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +165 -26
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +2 -3
- data/lib/oj.rb +3 -31
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +337 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- metadata +126 -163
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/_test_active.rb +0 -76
- data/test/_test_active_mimic.rb +0 -96
- data/test/_test_mimic_rails.rb +0 -126
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -24
- data/test/helper.rb +0 -27
- data/test/io.rb +0 -48
- data/test/isolated/shared.rb +0 -310
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/mod.rb +0 -16
- data/test/perf.rb +0 -107
- data/test/perf_compat.rb +0 -128
- data/test/perf_fast.rb +0 -164
- data/test/perf_file.rb +0 -64
- data/test/perf_object.rb +0 -138
- data/test/perf_saj.rb +0 -109
- data/test/perf_scp.rb +0 -151
- data/test/perf_simple.rb +0 -287
- data/test/perf_strict.rb +0 -128
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/sample/change.rb +0 -14
- data/test/sample/dir.rb +0 -19
- data/test/sample/doc.rb +0 -36
- data/test/sample/file.rb +0 -48
- data/test/sample/group.rb +0 -16
- data/test/sample/hasprops.rb +0 -16
- data/test/sample/layer.rb +0 -12
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -10
- data/test/sample/shape.rb +0 -35
- data/test/sample/text.rb +0 -20
- data/test/sample.rb +0 -55
- data/test/sample_json.rb +0 -37
- data/test/struct.rb +0 -29
- data/test/test_compat.rb +0 -398
- data/test/test_debian.rb +0 -53
- data/test/test_fast.rb +0 -458
- data/test/test_file.rb +0 -245
- data/test/test_gc.rb +0 -49
- data/test/test_hash.rb +0 -29
- data/test/test_object.rb +0 -745
- data/test/test_saj.rb +0 -186
- data/test/test_scp.rb +0 -396
- data/test/test_serializer.rb +0 -59
- data/test/test_strict.rb +0 -254
- data/test/test_various.rb +0 -1383
- data/test/test_writer.rb +0 -308
- data/test/write_timebars.rb +0 -31
data/ext/oj/cache.c
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
// Copyright (c) 2011, 2021 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
3
|
+
|
4
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
5
|
+
#include <pthread.h>
|
6
|
+
#endif
|
7
|
+
#include <stdlib.h>
|
8
|
+
|
9
|
+
#include "cache.h"
|
10
|
+
#include "mem.h"
|
11
|
+
|
12
|
+
// The stdlib calloc, realloc, and free are used instead of the Ruby ALLOC,
|
13
|
+
// ALLOC_N, REALLOC, and xfree since the later could trigger a GC which will
|
14
|
+
// either corrupt memory or if the mark function locks will deadlock.
|
15
|
+
|
16
|
+
#define REHASH_LIMIT 4
|
17
|
+
#define MIN_SHIFT 8
|
18
|
+
#define REUSE_MAX 8192
|
19
|
+
|
20
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
21
|
+
#define CACHE_LOCK(c) pthread_mutex_lock(&((c)->mutex))
|
22
|
+
#define CACHE_UNLOCK(c) pthread_mutex_unlock(&((c)->mutex))
|
23
|
+
#else
|
24
|
+
#define CACHE_LOCK(c) rb_mutex_lock((c)->mutex)
|
25
|
+
#define CACHE_UNLOCK(c) rb_mutex_unlock((c)->mutex)
|
26
|
+
#endif
|
27
|
+
|
28
|
+
// almost the Murmur hash algorithm
|
29
|
+
#define M 0x5bd1e995
|
30
|
+
|
31
|
+
typedef struct _slot {
|
32
|
+
struct _slot *next;
|
33
|
+
VALUE val;
|
34
|
+
uint64_t hash;
|
35
|
+
volatile uint32_t use_cnt;
|
36
|
+
uint8_t klen;
|
37
|
+
char key[CACHE_MAX_KEY];
|
38
|
+
} *Slot;
|
39
|
+
|
40
|
+
typedef struct _cache {
|
41
|
+
volatile Slot *slots;
|
42
|
+
volatile size_t cnt;
|
43
|
+
VALUE (*form)(const char *str, size_t len);
|
44
|
+
uint64_t size;
|
45
|
+
uint64_t mask;
|
46
|
+
VALUE (*intern)(struct _cache *c, const char *key, size_t len);
|
47
|
+
volatile Slot reuse;
|
48
|
+
size_t rcnt;
|
49
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
50
|
+
pthread_mutex_t mutex;
|
51
|
+
#else
|
52
|
+
VALUE mutex;
|
53
|
+
#endif
|
54
|
+
uint8_t xrate;
|
55
|
+
bool mark;
|
56
|
+
} *Cache;
|
57
|
+
|
58
|
+
void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
|
59
|
+
c->form = form;
|
60
|
+
}
|
61
|
+
|
62
|
+
static uint64_t hash_calc(const uint8_t *key, size_t len) {
|
63
|
+
const uint8_t *end = key + len;
|
64
|
+
const uint8_t *endless = key + (len & 0xFFFFFFFC);
|
65
|
+
uint64_t h = (uint64_t)len;
|
66
|
+
uint64_t k;
|
67
|
+
|
68
|
+
while (key < endless) {
|
69
|
+
k = (uint64_t)*key++;
|
70
|
+
k |= (uint64_t)*key++ << 8;
|
71
|
+
k |= (uint64_t)*key++ << 16;
|
72
|
+
k |= (uint64_t)*key++ << 24;
|
73
|
+
|
74
|
+
k *= M;
|
75
|
+
k ^= k >> 24;
|
76
|
+
h *= M;
|
77
|
+
h ^= k * M;
|
78
|
+
}
|
79
|
+
if (1 < end - key) {
|
80
|
+
uint16_t k16 = (uint16_t)*key++;
|
81
|
+
|
82
|
+
k16 |= (uint16_t)*key++ << 8;
|
83
|
+
h ^= k16 << 8;
|
84
|
+
}
|
85
|
+
if (key < end) {
|
86
|
+
h ^= *key;
|
87
|
+
}
|
88
|
+
h *= M;
|
89
|
+
h ^= h >> 13;
|
90
|
+
h *= M;
|
91
|
+
h ^= h >> 15;
|
92
|
+
|
93
|
+
return h;
|
94
|
+
}
|
95
|
+
|
96
|
+
static void rehash(Cache c) {
|
97
|
+
uint64_t osize;
|
98
|
+
Slot *end;
|
99
|
+
Slot *sp;
|
100
|
+
|
101
|
+
osize = c->size;
|
102
|
+
c->size = osize * 4;
|
103
|
+
c->mask = c->size - 1;
|
104
|
+
c->slots = OJ_REALLOC((void *)c->slots, sizeof(Slot) * c->size);
|
105
|
+
memset((Slot *)c->slots + osize, 0, sizeof(Slot) * osize * 3);
|
106
|
+
end = (Slot *)c->slots + osize;
|
107
|
+
for (sp = (Slot *)c->slots; sp < end; sp++) {
|
108
|
+
Slot s = *sp;
|
109
|
+
Slot next = NULL;
|
110
|
+
|
111
|
+
*sp = NULL;
|
112
|
+
for (; NULL != s; s = next) {
|
113
|
+
uint64_t h = s->hash & c->mask;
|
114
|
+
Slot *bucket = (Slot *)c->slots + h;
|
115
|
+
|
116
|
+
next = s->next;
|
117
|
+
s->next = *bucket;
|
118
|
+
*bucket = s;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
static VALUE lockless_intern(Cache c, const char *key, size_t len) {
|
124
|
+
uint64_t h = hash_calc((const uint8_t *)key, len);
|
125
|
+
Slot *bucket = (Slot *)c->slots + (h & c->mask);
|
126
|
+
Slot b;
|
127
|
+
volatile VALUE rkey;
|
128
|
+
|
129
|
+
while (REUSE_MAX < c->rcnt) {
|
130
|
+
if (NULL != (b = c->reuse)) {
|
131
|
+
c->reuse = b->next;
|
132
|
+
OJ_FREE(b);
|
133
|
+
c->rcnt--;
|
134
|
+
} else {
|
135
|
+
// An accounting error occured somewhere so correct it.
|
136
|
+
c->rcnt = 0;
|
137
|
+
}
|
138
|
+
}
|
139
|
+
for (b = *bucket; NULL != b; b = b->next) {
|
140
|
+
if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
|
141
|
+
b->use_cnt += 16;
|
142
|
+
return b->val;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
rkey = c->form(key, len);
|
146
|
+
if (NULL == (b = c->reuse)) {
|
147
|
+
b = OJ_CALLOC(1, sizeof(struct _slot));
|
148
|
+
} else {
|
149
|
+
c->reuse = b->next;
|
150
|
+
c->rcnt--;
|
151
|
+
}
|
152
|
+
b->hash = h;
|
153
|
+
memcpy(b->key, key, len);
|
154
|
+
b->klen = (uint8_t)len;
|
155
|
+
b->key[len] = '\0';
|
156
|
+
b->val = rkey;
|
157
|
+
b->use_cnt = 4;
|
158
|
+
b->next = *bucket;
|
159
|
+
*bucket = b;
|
160
|
+
c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
|
161
|
+
if (REHASH_LIMIT < c->cnt / c->size) {
|
162
|
+
rehash(c);
|
163
|
+
}
|
164
|
+
return rkey;
|
165
|
+
}
|
166
|
+
|
167
|
+
static VALUE locking_intern(Cache c, const char *key, size_t len) {
|
168
|
+
uint64_t h;
|
169
|
+
Slot *bucket;
|
170
|
+
Slot b;
|
171
|
+
uint64_t old_size;
|
172
|
+
volatile VALUE rkey;
|
173
|
+
|
174
|
+
CACHE_LOCK(c);
|
175
|
+
while (REUSE_MAX < c->rcnt) {
|
176
|
+
if (NULL != (b = c->reuse)) {
|
177
|
+
c->reuse = b->next;
|
178
|
+
OJ_FREE(b);
|
179
|
+
c->rcnt--;
|
180
|
+
} else {
|
181
|
+
// An accounting error occured somewhere so correct it.
|
182
|
+
c->rcnt = 0;
|
183
|
+
}
|
184
|
+
}
|
185
|
+
h = hash_calc((const uint8_t *)key, len);
|
186
|
+
bucket = (Slot *)c->slots + (h & c->mask);
|
187
|
+
for (b = *bucket; NULL != b; b = b->next) {
|
188
|
+
if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
|
189
|
+
b->use_cnt += 4;
|
190
|
+
CACHE_UNLOCK(c);
|
191
|
+
|
192
|
+
return b->val;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
old_size = c->size;
|
196
|
+
// The creation of a new value may trigger a GC which be a problem if the
|
197
|
+
// cache is locked so make sure it is unlocked for the key value creation.
|
198
|
+
if (NULL != (b = c->reuse)) {
|
199
|
+
c->reuse = b->next;
|
200
|
+
c->rcnt--;
|
201
|
+
}
|
202
|
+
CACHE_UNLOCK(c);
|
203
|
+
if (NULL == b) {
|
204
|
+
b = OJ_CALLOC(1, sizeof(struct _slot));
|
205
|
+
}
|
206
|
+
rkey = c->form(key, len);
|
207
|
+
b->hash = h;
|
208
|
+
memcpy(b->key, key, len);
|
209
|
+
b->klen = (uint8_t)len;
|
210
|
+
b->key[len] = '\0';
|
211
|
+
b->val = rkey;
|
212
|
+
b->use_cnt = 16;
|
213
|
+
|
214
|
+
// Lock again to add the new entry.
|
215
|
+
CACHE_LOCK(c);
|
216
|
+
if (old_size != c->size) {
|
217
|
+
h = hash_calc((const uint8_t *)key, len);
|
218
|
+
bucket = (Slot *)c->slots + (h & c->mask);
|
219
|
+
}
|
220
|
+
b->next = *bucket;
|
221
|
+
*bucket = b;
|
222
|
+
c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
|
223
|
+
if (REHASH_LIMIT < c->cnt / c->size) {
|
224
|
+
rehash(c);
|
225
|
+
}
|
226
|
+
CACHE_UNLOCK(c);
|
227
|
+
|
228
|
+
return rkey;
|
229
|
+
}
|
230
|
+
|
231
|
+
Cache cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking) {
|
232
|
+
Cache c = OJ_CALLOC(1, sizeof(struct _cache));
|
233
|
+
int shift = 0;
|
234
|
+
|
235
|
+
for (; REHASH_LIMIT < size; size /= 2, shift++) {
|
236
|
+
}
|
237
|
+
if (shift < MIN_SHIFT) {
|
238
|
+
shift = MIN_SHIFT;
|
239
|
+
}
|
240
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
241
|
+
pthread_mutex_init(&c->mutex, NULL);
|
242
|
+
#else
|
243
|
+
c->mutex = rb_mutex_new();
|
244
|
+
#endif
|
245
|
+
c->size = 1 << shift;
|
246
|
+
c->mask = c->size - 1;
|
247
|
+
c->slots = OJ_CALLOC(c->size, sizeof(Slot));
|
248
|
+
c->form = form;
|
249
|
+
c->xrate = 1; // low
|
250
|
+
c->mark = mark;
|
251
|
+
if (locking) {
|
252
|
+
c->intern = locking_intern;
|
253
|
+
} else {
|
254
|
+
c->intern = lockless_intern;
|
255
|
+
}
|
256
|
+
return c;
|
257
|
+
}
|
258
|
+
|
259
|
+
void cache_set_expunge_rate(Cache c, int rate) {
|
260
|
+
c->xrate = (uint8_t)rate;
|
261
|
+
}
|
262
|
+
|
263
|
+
void cache_free(void *data) {
|
264
|
+
Cache c = (Cache)data;
|
265
|
+
uint64_t i;
|
266
|
+
|
267
|
+
for (i = 0; i < c->size; i++) {
|
268
|
+
Slot next;
|
269
|
+
Slot s;
|
270
|
+
|
271
|
+
for (s = c->slots[i]; NULL != s; s = next) {
|
272
|
+
next = s->next;
|
273
|
+
OJ_FREE(s);
|
274
|
+
}
|
275
|
+
}
|
276
|
+
OJ_FREE((void *)c->slots);
|
277
|
+
OJ_FREE(c);
|
278
|
+
}
|
279
|
+
|
280
|
+
void cache_mark(void *data) {
|
281
|
+
Cache c = (Cache)data;
|
282
|
+
uint64_t i;
|
283
|
+
|
284
|
+
#if !HAVE_PTHREAD_MUTEX_INIT
|
285
|
+
rb_gc_mark(c->mutex);
|
286
|
+
#endif
|
287
|
+
if (0 == c->cnt) {
|
288
|
+
return;
|
289
|
+
}
|
290
|
+
for (i = 0; i < c->size; i++) {
|
291
|
+
Slot s;
|
292
|
+
Slot prev = NULL;
|
293
|
+
Slot next;
|
294
|
+
|
295
|
+
for (s = c->slots[i]; NULL != s; s = next) {
|
296
|
+
next = s->next;
|
297
|
+
if (0 == s->use_cnt) {
|
298
|
+
if (NULL == prev) {
|
299
|
+
c->slots[i] = next;
|
300
|
+
} else {
|
301
|
+
prev->next = next;
|
302
|
+
}
|
303
|
+
c->cnt--;
|
304
|
+
s->next = c->reuse;
|
305
|
+
c->reuse = s;
|
306
|
+
c->rcnt++;
|
307
|
+
continue;
|
308
|
+
}
|
309
|
+
switch (c->xrate) {
|
310
|
+
case 0: break;
|
311
|
+
case 2: s->use_cnt -= 2; break;
|
312
|
+
case 3: s->use_cnt /= 2; break;
|
313
|
+
default: s->use_cnt--; break;
|
314
|
+
}
|
315
|
+
if (c->mark) {
|
316
|
+
rb_gc_mark(s->val);
|
317
|
+
}
|
318
|
+
prev = s;
|
319
|
+
}
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
VALUE
|
324
|
+
cache_intern(Cache c, const char *key, size_t len) {
|
325
|
+
if (CACHE_MAX_KEY <= len) {
|
326
|
+
return c->form(key, len);
|
327
|
+
}
|
328
|
+
return c->intern(c, key, len);
|
329
|
+
}
|
data/ext/oj/cache.h
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
// Copyright (c) 2021 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
3
|
+
|
4
|
+
#ifndef CACHE_H
|
5
|
+
#define CACHE_H
|
6
|
+
|
7
|
+
#include <ruby.h>
|
8
|
+
#include <stdbool.h>
|
9
|
+
|
10
|
+
#define CACHE_MAX_KEY 35
|
11
|
+
|
12
|
+
struct _cache;
|
13
|
+
typedef struct _cache *Cache;
|
14
|
+
|
15
|
+
extern struct _cache *cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
|
16
|
+
extern void cache_free(void *data);
|
17
|
+
extern void cache_mark(void *data);
|
18
|
+
extern void cache_set_form(struct _cache *c, VALUE (*form)(const char *str, size_t len));
|
19
|
+
extern VALUE cache_intern(struct _cache *c, const char *key, size_t len);
|
20
|
+
extern void cache_set_expunge_rate(struct _cache *c, int rate);
|
21
|
+
|
22
|
+
#endif /* CACHE_H */
|
data/ext/oj/cache8.c
CHANGED
@@ -1,107 +1,105 @@
|
|
1
|
+
// Copyright (c) 2011 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
3
|
+
|
4
|
+
#include "cache8.h"
|
1
5
|
|
2
|
-
#include <stdlib.h>
|
3
6
|
#include <errno.h>
|
7
|
+
#include <stdarg.h>
|
4
8
|
#include <stdio.h>
|
9
|
+
#include <stdlib.h>
|
5
10
|
#include <string.h>
|
6
|
-
#include <stdarg.h>
|
7
11
|
|
12
|
+
#include "mem.h"
|
8
13
|
#include "ruby.h"
|
9
|
-
#include "cache8.h"
|
10
14
|
|
11
|
-
#define BITS
|
12
|
-
#define MASK
|
13
|
-
#define SLOT_CNT
|
14
|
-
#define DEPTH
|
15
|
+
#define BITS 4
|
16
|
+
#define MASK 0x000000000000000FULL
|
17
|
+
#define SLOT_CNT 16
|
18
|
+
#define DEPTH 16
|
15
19
|
|
16
20
|
typedef union {
|
17
|
-
struct
|
18
|
-
slot_t
|
21
|
+
struct _cache8 *child;
|
22
|
+
slot_t value;
|
19
23
|
} Bucket;
|
20
24
|
|
21
|
-
struct
|
22
|
-
Bucket
|
25
|
+
struct _cache8 {
|
26
|
+
Bucket buckets[SLOT_CNT];
|
23
27
|
};
|
24
28
|
|
25
|
-
static void
|
26
|
-
static void
|
29
|
+
static void cache8_delete(Cache8 cache, int depth);
|
30
|
+
static void slot_print(Cache8 cache, sid_t key, unsigned int depth);
|
27
31
|
|
28
|
-
void
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
*cache = ALLOC(struct _Cache8);
|
32
|
+
void oj_cache8_new(Cache8 *cache) {
|
33
|
+
Bucket *b;
|
34
|
+
int i;
|
35
|
+
|
36
|
+
*cache = OJ_R_ALLOC(struct _cache8);
|
34
37
|
for (i = SLOT_CNT, b = (*cache)->buckets; 0 < i; i--, b++) {
|
35
|
-
|
38
|
+
b->value = 0;
|
36
39
|
}
|
37
40
|
}
|
38
41
|
|
39
|
-
void
|
40
|
-
oj_cache8_delete(Cache8 cache) {
|
42
|
+
void oj_cache8_delete(Cache8 cache) {
|
41
43
|
cache8_delete(cache, 0);
|
42
44
|
}
|
43
45
|
|
44
|
-
static void
|
45
|
-
|
46
|
-
|
47
|
-
unsigned int i;
|
46
|
+
static void cache8_delete(Cache8 cache, int depth) {
|
47
|
+
Bucket *b;
|
48
|
+
unsigned int i;
|
48
49
|
|
49
50
|
for (i = 0, b = cache->buckets; i < SLOT_CNT; i++, b++) {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
if (0 != b->child) {
|
52
|
+
if (DEPTH - 1 != depth) {
|
53
|
+
cache8_delete(b->child, depth + 1);
|
54
|
+
}
|
55
|
+
}
|
55
56
|
}
|
56
|
-
|
57
|
+
OJ_R_FREE(cache);
|
57
58
|
}
|
58
59
|
|
59
|
-
slot_t
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
sid_t
|
64
|
-
|
65
|
-
|
60
|
+
slot_t oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot) {
|
61
|
+
Bucket *b;
|
62
|
+
int i;
|
63
|
+
sid_t k8 = (sid_t)key;
|
64
|
+
sid_t k;
|
65
|
+
|
66
66
|
for (i = 64 - BITS; 0 < i; i -= BITS) {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
k = (k8 >> i) & MASK;
|
68
|
+
b = cache->buckets + k;
|
69
|
+
if (0 == b->child) {
|
70
|
+
oj_cache8_new(&b->child);
|
71
|
+
}
|
72
|
+
cache = b->child;
|
73
73
|
}
|
74
74
|
*slot = &(cache->buckets + (k8 & MASK))->value;
|
75
75
|
|
76
76
|
return **slot;
|
77
77
|
}
|
78
78
|
|
79
|
-
void
|
80
|
-
oj_cache8_print(Cache8 cache) {
|
79
|
+
void oj_cache8_print(Cache8 cache) {
|
81
80
|
/*printf("-------------------------------------------\n"); */
|
82
81
|
slot_print(cache, 0, 0);
|
83
82
|
}
|
84
83
|
|
85
|
-
static void
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
sid_t
|
90
|
-
sid_t k;
|
84
|
+
static void slot_print(Cache8 c, sid_t key, unsigned int depth) {
|
85
|
+
Bucket *b;
|
86
|
+
unsigned int i;
|
87
|
+
sid_t k8 = (sid_t)key;
|
88
|
+
sid_t k;
|
91
89
|
|
92
90
|
for (i = 0, b = c->buckets; i < SLOT_CNT; i++, b++) {
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
if (0 != b->child) {
|
92
|
+
k = (k8 << BITS) | i;
|
93
|
+
/*printf("*** key: 0x%016llx depth: %u i: %u\n", k, depth, i); */
|
94
|
+
if (DEPTH - 1 == depth) {
|
97
95
|
#if IS_WINDOWS
|
98
|
-
|
96
|
+
printf("0x%016lx: %4lu\n", (long unsigned int)k, (long unsigned int)b->value);
|
99
97
|
#else
|
100
|
-
|
98
|
+
printf("0x%016llx: %4llu\n", (long long unsigned int)k, (long long unsigned int)b->value);
|
101
99
|
#endif
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
100
|
+
} else {
|
101
|
+
slot_print(b->child, k, depth + 1);
|
102
|
+
}
|
103
|
+
}
|
106
104
|
}
|
107
105
|
}
|
data/ext/oj/cache8.h
CHANGED
@@ -1,48 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
* All rights reserved.
|
4
|
-
*
|
5
|
-
* Redistribution and use in source and binary forms, with or without
|
6
|
-
* modification, are permitted provided that the following conditions are met:
|
7
|
-
*
|
8
|
-
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
-
* list of conditions and the following disclaimer.
|
10
|
-
*
|
11
|
-
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
-
* this list of conditions and the following disclaimer in the documentation
|
13
|
-
* and/or other materials provided with the distribution.
|
14
|
-
*
|
15
|
-
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
-
* used to endorse or promote products derived from this software without
|
17
|
-
* specific prior written permission.
|
18
|
-
*
|
19
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
-
*/
|
1
|
+
// Copyright (c) 2011 Peter Ohler. All rights reserved.
|
2
|
+
// Licensed under the MIT License. See LICENSE file in the project root for license details.
|
30
3
|
|
31
|
-
#ifndef
|
32
|
-
#define
|
4
|
+
#ifndef OJ_CACHE8_H
|
5
|
+
#define OJ_CACHE8_H
|
33
6
|
|
34
7
|
#include "ruby.h"
|
35
8
|
#include "stdint.h"
|
36
9
|
|
37
|
-
typedef struct
|
38
|
-
typedef uint64_t
|
39
|
-
typedef uint64_t
|
10
|
+
typedef struct _cache8 *Cache8;
|
11
|
+
typedef uint64_t slot_t;
|
12
|
+
typedef uint64_t sid_t;
|
40
13
|
|
41
|
-
extern void
|
42
|
-
extern void
|
14
|
+
extern void oj_cache8_new(Cache8 *cache);
|
15
|
+
extern void oj_cache8_delete(Cache8 cache);
|
43
16
|
|
44
|
-
extern slot_t
|
17
|
+
extern slot_t oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot);
|
45
18
|
|
46
|
-
extern void
|
19
|
+
extern void oj_cache8_print(Cache8 cache);
|
47
20
|
|
48
|
-
#endif /*
|
21
|
+
#endif /* OJ_CACHE8_H */
|