forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
6794 lines (6092 loc) · 174 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
hash.c -
$Author$
created at: Mon Nov 22 18:51:18 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/config.h"
#include <errno.h>
#ifdef __APPLE__
# ifdef HAVE_CRT_EXTERNS_H
# include <crt_externs.h>
# else
# include "missing/crt_externs.h"
# endif
#endif
#include "debug_counter.h"
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/bignum.h"
#include "internal/class.h"
#include "internal/cont.h"
#include "internal/error.h"
#include "internal/hash.h"
#include "internal/object.h"
#include "internal/proc.h"
#include "internal/symbol.h"
#include "internal/time.h"
#include "internal/vm.h"
#include "probes.h"
#include "ruby/st.h"
#include "ruby/util.h"
#include "ruby_assert.h"
#include "symbol.h"
#include "transient_heap.h"
#ifndef HASH_DEBUG
#define HASH_DEBUG 0
#endif
#if HASH_DEBUG
#include "gc.h"
#endif
#define HAS_EXTRA_STATES(hash, klass) ( \
((klass = has_extra_methods(rb_obj_class(hash))) != 0) || \
FL_TEST((hash), FL_EXIVAR|RHASH_PROC_DEFAULT) || \
!NIL_P(RHASH_IFNONE(hash)))
#define SET_DEFAULT(hash, ifnone) ( \
FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
RHASH_SET_IFNONE(hash, ifnone))
#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
static inline void
copy_default(struct RHash *hash, const struct RHash *hash2)
{
hash->basic.flags &= ~RHASH_PROC_DEFAULT;
hash->basic.flags |= hash2->basic.flags & RHASH_PROC_DEFAULT;
RHASH_SET_IFNONE(hash, RHASH_IFNONE((VALUE)hash2));
}
static VALUE
has_extra_methods(VALUE klass)
{
const VALUE base = rb_cHash;
VALUE c = klass;
while (c != base) {
if (rb_class_has_methods(c)) return klass;
c = RCLASS_SUPER(c);
}
return 0;
}
static VALUE rb_hash_s_try_convert(VALUE, VALUE);
/*
* Hash WB strategy:
* 1. Check mutate st_* functions
* * st_insert()
* * st_insert2()
* * st_update()
* * st_add_direct()
* 2. Insert WBs
*/
VALUE
rb_hash_freeze(VALUE hash)
{
return rb_obj_freeze(hash);
}
VALUE rb_cHash;
static VALUE envtbl;
static ID id_hash, id_default, id_flatten_bang;
static ID id_hash_iter_lev;
VALUE
rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
{
RB_OBJ_WRITE(hash, (&RHASH(hash)->ifnone), ifnone);
return hash;
}
static int
rb_any_cmp(VALUE a, VALUE b)
{
if (a == b) return 0;
if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
return rb_str_hash_cmp(a, b);
}
if (a == Qundef || b == Qundef) return -1;
if (SYMBOL_P(a) && SYMBOL_P(b)) {
return a != b;
}
return !rb_eql(a, b);
}
static VALUE
hash_recursive(VALUE obj, VALUE arg, int recurse)
{
if (recurse) return INT2FIX(0);
return rb_funcallv(obj, id_hash, 0, 0);
}
VALUE
rb_hash(VALUE obj)
{
VALUE hval = rb_exec_recursive_outer(hash_recursive, obj, 0);
while (!FIXNUM_P(hval)) {
if (RB_TYPE_P(hval, T_BIGNUM)) {
int sign;
unsigned long ul;
sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
INTEGER_PACK_NATIVE_BYTE_ORDER);
ul &= (1UL << (sizeof(long)*CHAR_BIT-1)) - 1;
if (sign < 0)
return LONG2FIX(-(long)ul);
return LONG2FIX((long)ul);
}
hval = rb_to_int(hval);
}
return hval;
}
static long rb_objid_hash(st_index_t index);
static st_index_t
dbl_to_index(double d)
{
union {double d; st_index_t i;} u;
u.d = d;
return u.i;
}
long
rb_dbl_long_hash(double d)
{
/* normalize -0.0 to 0.0 */
if (d == 0.0) d = 0.0;
#if SIZEOF_INT == SIZEOF_VOIDP
return rb_memhash(&d, sizeof(d));
#else
return rb_objid_hash(dbl_to_index(d));
#endif
}
static inline long
any_hash(VALUE a, st_index_t (*other_func)(VALUE))
{
VALUE hval;
st_index_t hnum;
if (SPECIAL_CONST_P(a)) {
if (STATIC_SYM_P(a)) {
hnum = a >> (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
hnum = rb_hash_start(hnum);
goto out;
}
else if (FLONUM_P(a)) {
/* prevent pathological behavior: [Bug #10761] */
goto flt;
}
hnum = rb_objid_hash((st_index_t)a);
}
else if (BUILTIN_TYPE(a) == T_STRING) {
hnum = rb_str_hash(a);
}
else if (BUILTIN_TYPE(a) == T_SYMBOL) {
hnum = RSYMBOL(a)->hashval;
}
else if (BUILTIN_TYPE(a) == T_BIGNUM) {
hval = rb_big_hash(a);
hnum = FIX2LONG(hval);
}
else if (BUILTIN_TYPE(a) == T_FLOAT) {
flt:
hnum = rb_dbl_long_hash(rb_float_value(a));
}
else {
hnum = other_func(a);
}
out:
#if SIZEOF_LONG < SIZEOF_ST_INDEX_T
if (hnum > 0)
hnum &= (unsigned long)-1 >> 2;
else
hnum |= ~((unsigned long)-1 >> 2);
#else
hnum <<= 1;
hnum = RSHIFT(hnum, 1);
#endif
return (long)hnum;
}
static st_index_t
obj_any_hash(VALUE obj)
{
obj = rb_hash(obj);
return FIX2LONG(obj);
}
static st_index_t
rb_any_hash(VALUE a)
{
return any_hash(a, obj_any_hash);
}
/* Here is a hash function for 64-bit key. It is about 5 times faster
(2 times faster when uint128 type is absent) on Haswell than
tailored Spooky or City hash function can be. */
/* Here we two primes with random bit generation. */
static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
static const uint32_t prime2 = 0x830fcab9;
static inline uint64_t
mult_and_mix(uint64_t m1, uint64_t m2)
{
#if defined HAVE_UINT128_T
uint128_t r = (uint128_t) m1 * (uint128_t) m2;
return (uint64_t) (r >> 64) ^ (uint64_t) r;
#else
uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
uint64_t lm1 = m1, lm2 = m2;
uint64_t v64_128 = hm1 * hm2;
uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
uint64_t v1_32 = lm1 * lm2;
return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
#endif
}
static inline uint64_t
key64_hash(uint64_t key, uint32_t seed)
{
return mult_and_mix(key + seed, prime1);
}
/* Should cast down the result for each purpose */
#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
static long
rb_objid_hash(st_index_t index)
{
return (long)st_index_hash(index);
}
static st_index_t
objid_hash(VALUE obj)
{
VALUE object_id = rb_obj_id(obj);
if (!FIXNUM_P(object_id))
object_id = rb_big_hash(object_id);
#if SIZEOF_LONG == SIZEOF_VOIDP
return (st_index_t)st_index_hash((st_index_t)NUM2LONG(object_id));
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
return (st_index_t)st_index_hash((st_index_t)NUM2LL(object_id));
#endif
}
/**
* call-seq:
* obj.hash -> integer
*
* Generates an Integer hash value for this object. This function must have the
* property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
*
* The hash value is used along with #eql? by the Hash class to determine if
* two objects reference the same hash key. Any hash value that exceeds the
* capacity of an Integer will be truncated before being used.
*
* The hash value for an object may not be identical across invocations or
* implementations of Ruby. If you need a stable identifier across Ruby
* invocations and implementations you will need to generate one with a custom
* method.
*--
* \private
*++
*/
VALUE
rb_obj_hash(VALUE obj)
{
long hnum = any_hash(obj, objid_hash);
return ST2FIX(hnum);
}
static const struct st_hash_type objhash = {
rb_any_cmp,
rb_any_hash,
};
#define rb_ident_cmp st_numcmp
static st_index_t
rb_ident_hash(st_data_t n)
{
#ifdef USE_FLONUM /* RUBY */
/*
* - flonum (on 64-bit) is pathologically bad, mix the actual
* float value in, but do not use the float value as-is since
* many integers get interpreted as 2.0 or -2.0 [Bug #10761]
*/
if (FLONUM_P(n)) {
n ^= dbl_to_index(rb_float_value(n));
}
#endif
return (st_index_t)st_index_hash((st_index_t)n);
}
#define identhash rb_hashtype_ident
const struct st_hash_type rb_hashtype_ident = {
rb_ident_cmp,
rb_ident_hash,
};
typedef st_index_t st_hash_t;
/*
* RHASH_AR_TABLE_P(h):
* * as.ar == NULL or
* as.ar points ar_table.
* * as.ar is allocated by transient heap or xmalloc.
*
* !RHASH_AR_TABLE_P(h):
* * as.st points st_table.
*/
#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
#define RHASH_AR_CLEARED_HINT 0xff
typedef struct ar_table_pair_struct {
VALUE key;
VALUE val;
} ar_table_pair;
typedef struct ar_table_struct {
/* 64bit CPU: 8B * 2 * 8 = 128B */
ar_table_pair pairs[RHASH_AR_TABLE_MAX_SIZE];
} ar_table;
size_t
rb_hash_ar_table_size(void)
{
return sizeof(ar_table);
}
static inline st_hash_t
ar_do_hash(st_data_t key)
{
return (st_hash_t)rb_any_hash(key);
}
static inline ar_hint_t
ar_do_hash_hint(st_hash_t hash_value)
{
return (ar_hint_t)hash_value;
}
static inline ar_hint_t
ar_hint(VALUE hash, unsigned int index)
{
return RHASH(hash)->ar_hint.ary[index];
}
static inline void
ar_hint_set_hint(VALUE hash, unsigned int index, ar_hint_t hint)
{
RHASH(hash)->ar_hint.ary[index] = hint;
}
static inline void
ar_hint_set(VALUE hash, unsigned int index, st_hash_t hash_value)
{
ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
}
static inline void
ar_clear_entry(VALUE hash, unsigned int index)
{
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
pair->key = Qundef;
ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
}
static inline int
ar_cleared_entry(VALUE hash, unsigned int index)
{
if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
/* RHASH_AR_CLEARED_HINT is only a hint, not mean cleared entry,
* so you need to check key == Qundef
*/
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
return pair->key == Qundef;
}
else {
return FALSE;
}
}
static inline void
ar_set_entry(VALUE hash, unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
{
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
pair->key = key;
pair->val = val;
ar_hint_set(hash, index, hash_value);
}
#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
RHASH_AR_TABLE_SIZE_RAW(h))
#define RHASH_AR_TABLE_BOUND_RAW(h) \
((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
(RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
#define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
RHASH_AR_TABLE_BOUND_RAW(h))
#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
#if HASH_DEBUG
#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
void
rb_hash_dump(VALUE hash)
{
rb_obj_info_dump(hash);
if (RHASH_AR_TABLE_P(hash)) {
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
fprintf(stderr, " size:%u bound:%u\n",
RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
for (i=0; i<bound; i++) {
st_data_t k, v;
if (!ar_cleared_entry(hash, i)) {
char b1[0x100], b2[0x100];
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
k = pair->key;
v = pair->val;
fprintf(stderr, " %d key:%s val:%s hint:%02x\n", i,
rb_raw_obj_info(b1, 0x100, k),
rb_raw_obj_info(b2, 0x100, v),
ar_hint(hash, i));
n++;
}
else {
fprintf(stderr, " %d empty\n", i);
}
}
}
}
static VALUE
hash_verify_(VALUE hash, const char *file, int line)
{
HASH_ASSERT(RB_TYPE_P(hash, T_HASH));
if (RHASH_AR_TABLE_P(hash)) {
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
for (i=0; i<bound; i++) {
st_data_t k, v;
if (!ar_cleared_entry(hash, i)) {
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
k = pair->key;
v = pair->val;
HASH_ASSERT(k != Qundef);
HASH_ASSERT(v != Qundef);
n++;
}
}
if (n != RHASH_AR_TABLE_SIZE(hash)) {
rb_bug("n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
}
}
else {
HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
}
#if USE_TRANSIENT_HEP
if (RHASH_TRANSIENT_P(hash)) {
volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key; /* read */
HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
}
#endif
return hash;
}
#else
#define hash_verify(h) ((void)0)
#endif
static inline int
RHASH_TABLE_NULL_P(VALUE hash)
{
if (RHASH(hash)->as.ar == NULL) {
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
return TRUE;
}
else {
return FALSE;
}
}
static inline int
RHASH_TABLE_EMPTY_P(VALUE hash)
{
return RHASH_SIZE(hash) == 0;
}
int
rb_hash_ar_table_p(VALUE hash)
{
if (FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG)) {
HASH_ASSERT(RHASH(hash)->as.st != NULL);
return FALSE;
}
else {
return TRUE;
}
}
ar_table *
rb_hash_ar_table(VALUE hash)
{
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
return RHASH(hash)->as.ar;
}
st_table *
rb_hash_st_table(VALUE hash)
{
HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
return RHASH(hash)->as.st;
}
void
rb_hash_st_table_set(VALUE hash, st_table *st)
{
HASH_ASSERT(st != NULL);
FL_SET_RAW((hash), RHASH_ST_TABLE_FLAG);
RHASH(hash)->as.st = st;
}
static void
hash_ar_table_set(VALUE hash, ar_table *ar)
{
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
RHASH(hash)->as.ar = ar;
hash_verify(hash);
}
#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
static inline void
RHASH_AR_TABLE_BOUND_SET(VALUE h, st_index_t n)
{
HASH_ASSERT(RHASH_AR_TABLE_P(h));
HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
}
static inline void
RHASH_AR_TABLE_SIZE_SET(VALUE h, st_index_t n)
{
HASH_ASSERT(RHASH_AR_TABLE_P(h));
HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
}
static inline void
HASH_AR_TABLE_SIZE_ADD(VALUE h, st_index_t n)
{
HASH_ASSERT(RHASH_AR_TABLE_P(h));
RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
hash_verify(h);
}
#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
static inline void
RHASH_AR_TABLE_SIZE_DEC(VALUE h)
{
HASH_ASSERT(RHASH_AR_TABLE_P(h));
int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
if (new_size != 0) {
RHASH_AR_TABLE_SIZE_SET(h, new_size);
}
else {
RHASH_AR_TABLE_SIZE_SET(h, 0);
RHASH_AR_TABLE_BOUND_SET(h, 0);
}
hash_verify(h);
}
static inline void
RHASH_AR_TABLE_CLEAR(VALUE h)
{
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
hash_ar_table_set(h, NULL);
}
static ar_table*
ar_alloc_table(VALUE hash)
{
ar_table *tab = (ar_table*)rb_transient_heap_alloc(hash, sizeof(ar_table));
if (tab != NULL) {
RHASH_SET_TRANSIENT_FLAG(hash);
}
else {
RHASH_UNSET_TRANSIENT_FLAG(hash);
tab = (ar_table*)ruby_xmalloc(sizeof(ar_table));
}
RHASH_AR_TABLE_SIZE_SET(hash, 0);
RHASH_AR_TABLE_BOUND_SET(hash, 0);
hash_ar_table_set(hash, tab);
return tab;
}
NOINLINE(static int ar_equal(VALUE x, VALUE y));
static int
ar_equal(VALUE x, VALUE y)
{
return rb_any_cmp(x, y) == 0;
}
static unsigned
ar_find_entry_hint(VALUE hash, ar_hint_t hint, st_data_t key)
{
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
const ar_hint_t *hints = RHASH(hash)->ar_hint.ary;
/* if table is NULL, then bound also should be 0 */
for (i = 0; i < bound; i++) {
if (hints[i] == hint) {
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
if (ar_equal(key, pair->key)) {
RB_DEBUG_COUNTER_INC(artable_hint_hit);
return i;
}
else {
#if 0
static int pid;
static char fname[256];
static FILE *fp;
if (pid != getpid()) {
snprintf(fname, sizeof(fname), "/tmp/ruby-armiss.%d", pid = getpid());
if ((fp = fopen(fname, "w")) == NULL) rb_bug("fopen");
}
st_hash_t h1 = ar_do_hash(key);
st_hash_t h2 = ar_do_hash(pair->key);
fprintf(fp, "miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
" key :%016lx %s\n"
" pair->key:%016lx %s\n",
h1 == h2, i, hints[i], hint,
h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
#endif
RB_DEBUG_COUNTER_INC(artable_hint_miss);
}
}
}
RB_DEBUG_COUNTER_INC(artable_hint_notfound);
return RHASH_AR_TABLE_MAX_BOUND;
}
static unsigned
ar_find_entry(VALUE hash, st_hash_t hash_value, st_data_t key)
{
ar_hint_t hint = ar_do_hash_hint(hash_value);
return ar_find_entry_hint(hash, hint, key);
}
static inline void
ar_free_and_clear_table(VALUE hash)
{
ar_table *tab = RHASH_AR_TABLE(hash);
if (tab) {
if (RHASH_TRANSIENT_P(hash)) {
RHASH_UNSET_TRANSIENT_FLAG(hash);
}
else {
ruby_xfree(RHASH_AR_TABLE(hash));
}
RHASH_AR_TABLE_CLEAR(hash);
}
HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
}
static void
ar_try_convert_table(VALUE hash)
{
if (!RHASH_AR_TABLE_P(hash)) return;
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
st_table *new_tab;
st_index_t i;
if (size < RHASH_AR_TABLE_MAX_SIZE) {
return;
}
new_tab = st_init_table_with_size(&objhash, size * 2);
for (i = 0; i < RHASH_AR_TABLE_MAX_BOUND; i++) {
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
st_add_direct(new_tab, pair->key, pair->val);
}
ar_free_and_clear_table(hash);
RHASH_ST_TABLE_SET(hash, new_tab);
return;
}
static st_table *
ar_force_convert_table(VALUE hash, const char *file, int line)
{
st_table *new_tab;
if (RHASH_ST_TABLE_P(hash)) {
return RHASH_ST_TABLE(hash);
}
if (RHASH_AR_TABLE(hash)) {
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
#if RHASH_CONVERT_TABLE_DEBUG
rb_obj_info_dump(hash);
fprintf(stderr, "force_convert: %s:%d\n", file, line);
RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
#endif
new_tab = st_init_table_with_size(&objhash, RHASH_AR_TABLE_SIZE(hash));
for (i = 0; i < bound; i++) {
if (ar_cleared_entry(hash, i)) continue;
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
st_add_direct(new_tab, pair->key, pair->val);
}
ar_free_and_clear_table(hash);
}
else {
new_tab = st_init_table(&objhash);
}
RHASH_ST_TABLE_SET(hash, new_tab);
return new_tab;
}
static ar_table *
hash_ar_table(VALUE hash)
{
if (RHASH_TABLE_NULL_P(hash)) {
ar_alloc_table(hash);
}
return RHASH_AR_TABLE(hash);
}
static int
ar_compact_table(VALUE hash)
{
const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
if (size == bound) {
return size;
}
else {
unsigned i, j=0;
ar_table_pair *pairs = RHASH_AR_TABLE(hash)->pairs;
for (i=0; i<bound; i++) {
if (ar_cleared_entry(hash, i)) {
if (j <= i) j = i+1;
for (; j<bound; j++) {
if (!ar_cleared_entry(hash, j)) {
pairs[i] = pairs[j];
ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
ar_clear_entry(hash, j);
j++;
goto found;
}
}
/* non-empty is not found */
goto done;
found:;
}
}
done:
HASH_ASSERT(i<=bound);
RHASH_AR_TABLE_BOUND_SET(hash, size);
hash_verify(hash);
return size;
}
}
static int
ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
{
unsigned bin = RHASH_AR_TABLE_BOUND(hash);
if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
return 1;
}
else {
if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
bin = ar_compact_table(hash);
hash_ar_table(hash);
}
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
ar_set_entry(hash, bin, key, val, hash_value);
RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
RHASH_AR_TABLE_SIZE_INC(hash);
return 0;
}
}
static int
ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
{
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
for (i = 0; i < bound; i++) {
if (ar_cleared_entry(hash, i)) continue;
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
/* pair may be not valid here because of theap */
switch (retval) {
case ST_CONTINUE:
break;
case ST_CHECK:
case ST_STOP:
return 0;
case ST_REPLACE:
if (replace) {
VALUE key = pair->key;
VALUE val = pair->val;
retval = (*replace)(&key, &val, arg, TRUE);
// TODO: pair should be same as pair before.
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
pair->key = key;
pair->val = val;
}
break;
case ST_DELETE:
ar_clear_entry(hash, i);
RHASH_AR_TABLE_SIZE_DEC(hash);
break;
}
}
}
return 0;
}
static int
ar_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
{
return ar_general_foreach(hash, func, replace, arg);
}
struct functor {
st_foreach_callback_func *func;
st_data_t arg;
};
static int
apply_functor(st_data_t k, st_data_t v, st_data_t d, int _)
{
const struct functor *f = (void *)d;
return f->func(k, v, f->arg);
}
static int
ar_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
{
const struct functor f = { func, arg };
return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
}
static int
ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
st_data_t never)
{
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
enum st_retval retval;
st_data_t key;
ar_table_pair *pair;
ar_hint_t hint;
for (i = 0; i < bound; i++) {
if (ar_cleared_entry(hash, i)) continue;
pair = RHASH_AR_TABLE_REF(hash, i);
key = pair->key;
hint = ar_hint(hash, i);
retval = (*func)(key, pair->val, arg, 0);
hash_verify(hash);
switch (retval) {
case ST_CHECK: {
pair = RHASH_AR_TABLE_REF(hash, i);
if (pair->key == never) break;
ret = ar_find_entry_hint(hash, hint, key);
if (ret == RHASH_AR_TABLE_MAX_BOUND) {
retval = (*func)(0, 0, arg, 1);
return 2;
}
}
case ST_CONTINUE:
break;
case ST_STOP:
case ST_REPLACE:
return 0;
case ST_DELETE: {
if (!ar_cleared_entry(hash, i)) {
ar_clear_entry(hash, i);
RHASH_AR_TABLE_SIZE_DEC(hash);
}