oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -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/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -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
- data/test/activerecord/result_test.rb +32 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +397 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- 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/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/ext/oj/cache.c
ADDED
@@ -0,0 +1,326 @@
|
|
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
|
+
|
11
|
+
// The stdlib calloc, realloc, and free are used instead of the Ruby ALLOC,
|
12
|
+
// ALLOC_N, REALLOC, and xfree since the later could trigger a GC which will
|
13
|
+
// either corrupt memory or if the mark function locks will deadlock.
|
14
|
+
|
15
|
+
#define REHASH_LIMIT 4
|
16
|
+
#define MIN_SHIFT 8
|
17
|
+
#define REUSE_MAX 8192
|
18
|
+
|
19
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
20
|
+
#define CACHE_LOCK(c) pthread_mutex_lock(&((c)->mutex))
|
21
|
+
#define CACHE_UNLOCK(c) pthread_mutex_unlock(&((c)->mutex))
|
22
|
+
#else
|
23
|
+
#define CACHE_LOCK(c) rb_mutex_lock((c)->mutex)
|
24
|
+
#define CACHE_UNLOCK(c) rb_mutex_unlock((c)->mutex)
|
25
|
+
#endif
|
26
|
+
|
27
|
+
// almost the Murmur hash algorithm
|
28
|
+
#define M 0x5bd1e995
|
29
|
+
|
30
|
+
typedef struct _slot {
|
31
|
+
struct _slot * next;
|
32
|
+
VALUE val;
|
33
|
+
uint64_t hash;
|
34
|
+
volatile uint32_t use_cnt;
|
35
|
+
uint8_t klen;
|
36
|
+
char key[CACHE_MAX_KEY];
|
37
|
+
} * Slot;
|
38
|
+
|
39
|
+
typedef struct _cache {
|
40
|
+
volatile Slot * slots;
|
41
|
+
volatile size_t cnt;
|
42
|
+
VALUE (*form)(const char *str, size_t len);
|
43
|
+
uint64_t size;
|
44
|
+
uint64_t mask;
|
45
|
+
VALUE (*intern)(struct _cache *c, const char *key, size_t len);
|
46
|
+
volatile Slot reuse;
|
47
|
+
size_t rcnt;
|
48
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
49
|
+
pthread_mutex_t mutex;
|
50
|
+
#else
|
51
|
+
VALUE mutex;
|
52
|
+
#endif
|
53
|
+
uint8_t xrate;
|
54
|
+
bool mark;
|
55
|
+
} * Cache;
|
56
|
+
|
57
|
+
void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
|
58
|
+
c->form = form;
|
59
|
+
}
|
60
|
+
|
61
|
+
static uint64_t hash_calc(const uint8_t *key, size_t len) {
|
62
|
+
const uint8_t *end = key + len;
|
63
|
+
const uint8_t *endless = key + (len & 0xFFFFFFFC);
|
64
|
+
uint64_t h = (uint64_t)len;
|
65
|
+
uint64_t k;
|
66
|
+
|
67
|
+
while (key < endless) {
|
68
|
+
k = (uint64_t)*key++;
|
69
|
+
k |= (uint64_t)*key++ << 8;
|
70
|
+
k |= (uint64_t)*key++ << 16;
|
71
|
+
k |= (uint64_t)*key++ << 24;
|
72
|
+
|
73
|
+
k *= M;
|
74
|
+
k ^= k >> 24;
|
75
|
+
h *= M;
|
76
|
+
h ^= k * M;
|
77
|
+
}
|
78
|
+
if (1 < end - key) {
|
79
|
+
uint16_t k16 = (uint16_t)*key++;
|
80
|
+
|
81
|
+
k16 |= (uint16_t)*key++ << 8;
|
82
|
+
h ^= k16 << 8;
|
83
|
+
}
|
84
|
+
if (key < end) {
|
85
|
+
h ^= *key;
|
86
|
+
}
|
87
|
+
h *= M;
|
88
|
+
h ^= h >> 13;
|
89
|
+
h *= M;
|
90
|
+
h ^= h >> 15;
|
91
|
+
|
92
|
+
return h;
|
93
|
+
}
|
94
|
+
|
95
|
+
static void rehash(Cache c) {
|
96
|
+
uint64_t osize;
|
97
|
+
Slot * end;
|
98
|
+
Slot * sp;
|
99
|
+
|
100
|
+
osize = c->size;
|
101
|
+
c->size = osize * 4;
|
102
|
+
c->mask = c->size - 1;
|
103
|
+
c->slots = realloc((void *)c->slots, sizeof(Slot) * c->size);
|
104
|
+
memset((Slot *)c->slots + osize, 0, sizeof(Slot) * osize * 3);
|
105
|
+
end = (Slot *)c->slots + osize;
|
106
|
+
for (sp = (Slot *)c->slots; sp < end; sp++) {
|
107
|
+
Slot s = *sp;
|
108
|
+
Slot next = NULL;
|
109
|
+
|
110
|
+
*sp = NULL;
|
111
|
+
for (; NULL != s; s = next) {
|
112
|
+
uint64_t h = s->hash & c->mask;
|
113
|
+
Slot * bucket = (Slot *)c->slots + h;
|
114
|
+
|
115
|
+
next = s->next;
|
116
|
+
s->next = *bucket;
|
117
|
+
*bucket = s;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
static VALUE lockless_intern(Cache c, const char *key, size_t len) {
|
123
|
+
uint64_t h = hash_calc((const uint8_t *)key, len);
|
124
|
+
Slot * bucket = (Slot *)c->slots + (h & c->mask);
|
125
|
+
Slot b;
|
126
|
+
volatile VALUE rkey;
|
127
|
+
|
128
|
+
while (REUSE_MAX < c->rcnt) {
|
129
|
+
if (NULL != (b = c->reuse)) {
|
130
|
+
c->reuse = b->next;
|
131
|
+
free(b);
|
132
|
+
c->rcnt--;
|
133
|
+
} else {
|
134
|
+
// An accounting error occured somewhere so correct it.
|
135
|
+
c->rcnt = 0;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
for (b = *bucket; NULL != b; b = b->next) {
|
139
|
+
if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
|
140
|
+
b->use_cnt += 16;
|
141
|
+
return b->val;
|
142
|
+
}
|
143
|
+
}
|
144
|
+
rkey = c->form(key, len);
|
145
|
+
if (NULL == (b = c->reuse)) {
|
146
|
+
b = calloc(1, sizeof(struct _slot));
|
147
|
+
} else {
|
148
|
+
c->reuse = b->next;
|
149
|
+
c->rcnt--;
|
150
|
+
}
|
151
|
+
b->hash = h;
|
152
|
+
memcpy(b->key, key, len);
|
153
|
+
b->klen = (uint8_t)len;
|
154
|
+
b->key[len] = '\0';
|
155
|
+
b->val = rkey;
|
156
|
+
b->use_cnt = 4;
|
157
|
+
b->next = *bucket;
|
158
|
+
*bucket = b;
|
159
|
+
c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
|
160
|
+
if (REHASH_LIMIT < c->cnt / c->size) {
|
161
|
+
rehash(c);
|
162
|
+
}
|
163
|
+
return rkey;
|
164
|
+
}
|
165
|
+
|
166
|
+
static VALUE locking_intern(Cache c, const char *key, size_t len) {
|
167
|
+
uint64_t h;
|
168
|
+
Slot * bucket;
|
169
|
+
Slot b;
|
170
|
+
uint64_t old_size;
|
171
|
+
volatile VALUE rkey;
|
172
|
+
|
173
|
+
CACHE_LOCK(c);
|
174
|
+
while (REUSE_MAX < c->rcnt) {
|
175
|
+
if (NULL != (b = c->reuse)) {
|
176
|
+
c->reuse = b->next;
|
177
|
+
free(b);
|
178
|
+
c->rcnt--;
|
179
|
+
} else {
|
180
|
+
// An accounting error occured somewhere so correct it.
|
181
|
+
c->rcnt = 0;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
h = hash_calc((const uint8_t *)key, len);
|
185
|
+
bucket = (Slot *)c->slots + (h & c->mask);
|
186
|
+
for (b = *bucket; NULL != b; b = b->next) {
|
187
|
+
if ((uint8_t)len == b->klen && 0 == strncmp(b->key, key, len)) {
|
188
|
+
b->use_cnt += 4;
|
189
|
+
CACHE_UNLOCK(c);
|
190
|
+
|
191
|
+
return b->val;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
old_size = c->size;
|
195
|
+
// The creation of a new value may trigger a GC which be a problem if the
|
196
|
+
// cache is locked so make sure it is unlocked for the key value creation.
|
197
|
+
if (NULL != (b = c->reuse)) {
|
198
|
+
c->reuse = b->next;
|
199
|
+
c->rcnt--;
|
200
|
+
}
|
201
|
+
CACHE_UNLOCK(c);
|
202
|
+
if (NULL == b) {
|
203
|
+
b = calloc(1, sizeof(struct _slot));
|
204
|
+
}
|
205
|
+
rkey = c->form(key, len);
|
206
|
+
b->hash = h;
|
207
|
+
memcpy(b->key, key, len);
|
208
|
+
b->klen = (uint8_t)len;
|
209
|
+
b->key[len] = '\0';
|
210
|
+
b->val = rkey;
|
211
|
+
b->use_cnt = 16;
|
212
|
+
|
213
|
+
// Lock again to add the new entry.
|
214
|
+
CACHE_LOCK(c);
|
215
|
+
if (old_size != c->size) {
|
216
|
+
h = hash_calc((const uint8_t *)key, len);
|
217
|
+
bucket = (Slot *)c->slots + (h & c->mask);
|
218
|
+
}
|
219
|
+
b->next = *bucket;
|
220
|
+
*bucket = b;
|
221
|
+
c->cnt++; // Don't worry about wrapping. Worse case is the entry is removed and recreated.
|
222
|
+
if (REHASH_LIMIT < c->cnt / c->size) {
|
223
|
+
rehash(c);
|
224
|
+
}
|
225
|
+
CACHE_UNLOCK(c);
|
226
|
+
|
227
|
+
return rkey;
|
228
|
+
}
|
229
|
+
|
230
|
+
Cache cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking) {
|
231
|
+
Cache c = calloc(1, sizeof(struct _cache));
|
232
|
+
int shift = 0;
|
233
|
+
|
234
|
+
for (; REHASH_LIMIT < size; size /= 2, shift++) {
|
235
|
+
}
|
236
|
+
if (shift < MIN_SHIFT) {
|
237
|
+
shift = MIN_SHIFT;
|
238
|
+
}
|
239
|
+
#if HAVE_PTHREAD_MUTEX_INIT
|
240
|
+
pthread_mutex_init(&c->mutex, NULL);
|
241
|
+
#else
|
242
|
+
c->mutex = rb_mutex_new();
|
243
|
+
#endif
|
244
|
+
c->size = 1 << shift;
|
245
|
+
c->mask = c->size - 1;
|
246
|
+
c->slots = calloc(c->size, sizeof(Slot));
|
247
|
+
c->form = form;
|
248
|
+
c->xrate = 1; // low
|
249
|
+
c->mark = mark;
|
250
|
+
if (locking) {
|
251
|
+
c->intern = locking_intern;
|
252
|
+
} else {
|
253
|
+
c->intern = lockless_intern;
|
254
|
+
}
|
255
|
+
return c;
|
256
|
+
}
|
257
|
+
|
258
|
+
void cache_set_expunge_rate(Cache c, int rate) {
|
259
|
+
c->xrate = (uint8_t)rate;
|
260
|
+
}
|
261
|
+
|
262
|
+
void cache_free(Cache c) {
|
263
|
+
uint64_t i;
|
264
|
+
|
265
|
+
for (i = 0; i < c->size; i++) {
|
266
|
+
Slot next;
|
267
|
+
Slot s;
|
268
|
+
|
269
|
+
for (s = c->slots[i]; NULL != s; s = next) {
|
270
|
+
next = s->next;
|
271
|
+
free(s);
|
272
|
+
}
|
273
|
+
}
|
274
|
+
free((void *)c->slots);
|
275
|
+
free(c);
|
276
|
+
}
|
277
|
+
|
278
|
+
void cache_mark(Cache c) {
|
279
|
+
uint64_t i;
|
280
|
+
|
281
|
+
#if !HAVE_PTHREAD_MUTEX_INIT
|
282
|
+
rb_gc_mark(c->mutex);
|
283
|
+
#endif
|
284
|
+
if (0 == c->cnt) {
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
for (i = 0; i < c->size; i++) {
|
288
|
+
Slot s;
|
289
|
+
Slot prev = NULL;
|
290
|
+
Slot next;
|
291
|
+
|
292
|
+
for (s = c->slots[i]; NULL != s; s = next) {
|
293
|
+
next = s->next;
|
294
|
+
if (0 == s->use_cnt) {
|
295
|
+
if (NULL == prev) {
|
296
|
+
c->slots[i] = next;
|
297
|
+
} else {
|
298
|
+
prev->next = next;
|
299
|
+
}
|
300
|
+
c->cnt--;
|
301
|
+
s->next = c->reuse;
|
302
|
+
c->reuse = s;
|
303
|
+
c->rcnt++;
|
304
|
+
continue;
|
305
|
+
}
|
306
|
+
switch (c->xrate) {
|
307
|
+
case 0: break;
|
308
|
+
case 2: s->use_cnt -= 2; break;
|
309
|
+
case 3: s->use_cnt /= 2; break;
|
310
|
+
default: s->use_cnt--; break;
|
311
|
+
}
|
312
|
+
if (c->mark) {
|
313
|
+
rb_gc_mark(s->val);
|
314
|
+
}
|
315
|
+
prev = s;
|
316
|
+
}
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
VALUE
|
321
|
+
cache_intern(Cache c, const char *key, size_t len) {
|
322
|
+
if (CACHE_MAX_KEY <= len) {
|
323
|
+
return c->form(key, len);
|
324
|
+
}
|
325
|
+
return c->intern(c, key, len);
|
326
|
+
}
|
data/ext/oj/cache.h
ADDED
@@ -0,0 +1,21 @@
|
|
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
|
+
|
14
|
+
extern struct _cache *cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
|
15
|
+
extern void cache_free(struct _cache *c);
|
16
|
+
extern void cache_mark(struct _cache *c);
|
17
|
+
extern void cache_set_form(struct _cache *c, VALUE (*form)(const char *str, size_t len));
|
18
|
+
extern VALUE cache_intern(struct _cache *c, const char *key, size_t len);
|
19
|
+
extern void cache_set_expunge_rate(struct _cache *c, int rate);
|
20
|
+
|
21
|
+
#endif /* CACHE_H */
|
data/ext/oj/cache8.c
CHANGED
@@ -1,107 +1,104 @@
|
|
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
|
|
8
12
|
#include "ruby.h"
|
9
|
-
#include "cache8.h"
|
10
13
|
|
11
|
-
#define BITS
|
12
|
-
#define MASK
|
13
|
-
#define SLOT_CNT
|
14
|
-
#define DEPTH
|
14
|
+
#define BITS 4
|
15
|
+
#define MASK 0x000000000000000FULL
|
16
|
+
#define SLOT_CNT 16
|
17
|
+
#define DEPTH 16
|
15
18
|
|
16
19
|
typedef union {
|
17
|
-
struct
|
18
|
-
slot_t
|
20
|
+
struct _cache8 *child;
|
21
|
+
slot_t value;
|
19
22
|
} Bucket;
|
20
23
|
|
21
|
-
struct
|
22
|
-
Bucket
|
24
|
+
struct _cache8 {
|
25
|
+
Bucket buckets[SLOT_CNT];
|
23
26
|
};
|
24
27
|
|
25
|
-
static void
|
26
|
-
static void
|
28
|
+
static void cache8_delete(Cache8 cache, int depth);
|
29
|
+
static void slot_print(Cache8 cache, sid_t key, unsigned int depth);
|
30
|
+
|
31
|
+
void oj_cache8_new(Cache8 *cache) {
|
32
|
+
Bucket *b;
|
33
|
+
int i;
|
27
34
|
|
28
|
-
|
29
|
-
oj_cache8_new(Cache8 *cache) {
|
30
|
-
Bucket *b;
|
31
|
-
int i;
|
32
|
-
|
33
|
-
*cache = ALLOC(struct _Cache8);
|
35
|
+
*cache = ALLOC(struct _cache8);
|
34
36
|
for (i = SLOT_CNT, b = (*cache)->buckets; 0 < i; i--, b++) {
|
35
|
-
|
37
|
+
b->value = 0;
|
36
38
|
}
|
37
39
|
}
|
38
40
|
|
39
|
-
void
|
40
|
-
oj_cache8_delete(Cache8 cache) {
|
41
|
-
cache8_delete(cache, 0);
|
42
|
-
}
|
41
|
+
void oj_cache8_delete(Cache8 cache) { cache8_delete(cache, 0); }
|
43
42
|
|
44
|
-
static void
|
45
|
-
|
46
|
-
|
47
|
-
unsigned int i;
|
43
|
+
static void cache8_delete(Cache8 cache, int depth) {
|
44
|
+
Bucket * b;
|
45
|
+
unsigned int i;
|
48
46
|
|
49
47
|
for (i = 0, b = cache->buckets; i < SLOT_CNT; i++, b++) {
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
48
|
+
if (0 != b->child) {
|
49
|
+
if (DEPTH - 1 != depth) {
|
50
|
+
cache8_delete(b->child, depth + 1);
|
51
|
+
}
|
52
|
+
}
|
55
53
|
}
|
56
54
|
xfree(cache);
|
57
55
|
}
|
58
56
|
|
59
|
-
slot_t
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
sid_t
|
64
|
-
|
65
|
-
|
57
|
+
slot_t oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot) {
|
58
|
+
Bucket *b;
|
59
|
+
int i;
|
60
|
+
sid_t k8 = (sid_t)key;
|
61
|
+
sid_t k;
|
62
|
+
|
66
63
|
for (i = 64 - BITS; 0 < i; i -= BITS) {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
64
|
+
k = (k8 >> i) & MASK;
|
65
|
+
b = cache->buckets + k;
|
66
|
+
if (0 == b->child) {
|
67
|
+
oj_cache8_new(&b->child);
|
68
|
+
}
|
69
|
+
cache = b->child;
|
73
70
|
}
|
74
71
|
*slot = &(cache->buckets + (k8 & MASK))->value;
|
75
72
|
|
76
73
|
return **slot;
|
77
74
|
}
|
78
75
|
|
79
|
-
void
|
80
|
-
oj_cache8_print(Cache8 cache) {
|
76
|
+
void oj_cache8_print(Cache8 cache) {
|
81
77
|
/*printf("-------------------------------------------\n"); */
|
82
78
|
slot_print(cache, 0, 0);
|
83
79
|
}
|
84
80
|
|
85
|
-
static void
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
sid_t
|
90
|
-
sid_t k;
|
81
|
+
static void slot_print(Cache8 c, sid_t key, unsigned int depth) {
|
82
|
+
Bucket * b;
|
83
|
+
unsigned int i;
|
84
|
+
sid_t k8 = (sid_t)key;
|
85
|
+
sid_t k;
|
91
86
|
|
92
87
|
for (i = 0, b = c->buckets; i < SLOT_CNT; i++, b++) {
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
88
|
+
if (0 != b->child) {
|
89
|
+
k = (k8 << BITS) | i;
|
90
|
+
/*printf("*** key: 0x%016llx depth: %u i: %u\n", k, depth, i); */
|
91
|
+
if (DEPTH - 1 == depth) {
|
97
92
|
#if IS_WINDOWS
|
98
|
-
|
93
|
+
printf("0x%016lx: %4lu\n", (long unsigned int)k,
|
94
|
+
(long unsigned int)b->value);
|
99
95
|
#else
|
100
|
-
|
96
|
+
printf("0x%016llx: %4llu\n", (long long unsigned int)k,
|
97
|
+
(long long unsigned int)b->value);
|
101
98
|
#endif
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
99
|
+
} else {
|
100
|
+
slot_print(b->child, k, depth + 1);
|
101
|
+
}
|
102
|
+
}
|
106
103
|
}
|
107
104
|
}
|
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 */
|