public
Description: Full mirror of JRuby Subversion repository
Homepage: http://jruby.codehaus.org/
Clone URL: git://github.com/vvs/jruby.git
JRUBY-3052: Cache constants at their call site (const_node)

git-svn-id: https://svn.codehaus.org/jruby/trunk/jruby@7867 
961051c9-f516-0410-bf72-c9f7e237a7b7
enebo (author)
Sat Oct 11 08:21:49 -0700 2008
commit  9f5833cfcd4baa7badf06f2026b588a1e6171d54
tree    d1a6f0b83f2474b774124b0c1b633ed6531fa242
parent  2926e08846d813867a0ada780ff57c2e228d8a34
...
32
33
34
 
35
36
37
...
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
...
32
33
34
35
36
37
38
...
271
272
273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
275
 
 
 
276
277
278
279
 
 
 
 
 
 
280
281
 
 
 
282
283
284
 
285
0
@@ -32,6 +32,7 @@
0
  ***** END LICENSE BLOCK *****/
0
 package org.jruby;
0
 
0
+import java.util.Collection;
0
 import java.util.List;
0
 import java.util.Map;
0
 
0
@@ -270,43 +271,15 @@ public final class IncludedModuleWrapper extends RubyClass {
0
         // this _is_ legal (when removing an undef)
0
         return delegate.constantTableRemove(name);
0
     }
0
-
0
-    @Override
0
-    protected ConstantTableEntry[] constantTableGetTable() {
0
-        return delegate.constantTableGetTable();
0
-    }
0
-
0
-    @Override
0
-    protected int constantTableGetSize() {
0
-        return delegate.constantTableGetSize();
0
-    }
0
-
0
-    @Override
0
-    protected void constantTableSync(List<Variable<IRubyObject>> vars) {
0
-        // FIXME: legal here? may want UnsupportedOperationException
0
-        delegate.constantTableSync(vars);
0
-    }
0
-
0
-    /**
0
-     * Method to help ease transition to new variables implementation.
0
-     * Will likely be deprecated in the near future.
0
-     */
0
-    @SuppressWarnings("unchecked")
0
+    
0
     @Override
0
-    @Deprecated // born deprecated
0
-    protected Map constantTableGetMap() {
0
-        return delegate.constantTableGetMap();
0
+    @Deprecated
0
+    public List<String> getStoredConstantNameList() {
0
+        return delegate.getStoredConstantNameList();
0
     }
0
-
0
-    /**
0
-     * Method to help ease transition to new variables implementation.
0
-     * Will likely be deprecated in the near future.
0
-     */
0
-    @SuppressWarnings("unchecked")
0
+    
0
     @Override
0
-    @Deprecated // born deprecated
0
-    protected Map constantTableGetMap(Map map) {
0
-        return delegate.constantTableGetMap(map);
0
+    public Collection<String> getConstantNames() {
0
+        return delegate.getConstantNames();
0
     }
0
-
0
 }
...
104
105
106
 
107
108
109
...
182
183
184
 
185
186
187
...
214
215
216
 
217
218
219
...
1302
1303
1304
 
 
 
 
1305
1306
1307
...
104
105
106
107
108
109
110
...
183
184
185
186
187
188
189
...
216
217
218
219
220
221
222
...
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
0
@@ -104,6 +104,7 @@ import org.jruby.runtime.Block;
0
 import org.jruby.runtime.CacheMap;
0
 import org.jruby.runtime.CallSite;
0
 import org.jruby.runtime.CallbackFactory;
0
+import org.jruby.runtime.ConstantCacheMap;
0
 import org.jruby.runtime.DynamicScope;
0
 import org.jruby.runtime.EventHook;
0
 import org.jruby.runtime.GlobalVariable;
0
@@ -182,6 +183,7 @@ public final class Ruby {
0
         config.setError(err);
0
         return newInstance(config);
0
     }
0
+    private ConstantCacheMap constantCacheMap;
0
     
0
     /**
0
      * Create and initialize a new JRuby runtime. The properties of the
0
@@ -214,6 +216,7 @@ public final class Ruby {
0
         this.beanManager.register(new ClassCache(this));
0
         
0
         this.cacheMap = new CacheMap(this);
0
+        this.constantCacheMap = new ConstantCacheMap(this);
0
     }
0
     
0
     /**
0
@@ -1302,6 +1305,10 @@ public final class Ruby {
0
         return cacheMap;
0
     }
0
 
0
+    public ConstantCacheMap getConstantCacheMap() {
0
+        return constantCacheMap;
0
+    }
0
+
0
     /** Getter for property rubyTopSelf.
0
      * @return Value of property rubyTopSelf.
0
      */
...
41
42
43
 
44
45
46
47
48
49
50
51
52
53
54
55
...
88
89
90
 
91
92
93
94
95
...
186
187
188
189
190
191
192
193
194
195
196
197
 
198
199
200
...
500
501
502
 
503
504
505
506
 
 
 
 
 
 
 
 
 
 
507
508
509
...
527
528
529
530
 
531
532
533
...
862
863
864
865
 
866
867
868
869
870
871
872
873
874
875
 
 
 
 
 
 
876
877
878
...
1005
1006
1007
1008
1009
1010
1011
 
 
1012
1013
1014
...
1454
1455
1456
1457
1458
1459
1460
 
 
1461
1462
1463
1464
1465
1466
 
 
 
 
1467
1468
1469
...
1496
1497
1498
1499
1500
1501
1502
 
 
1503
1504
1505
...
1510
1511
1512
1513
1514
 
 
1515
1516
1517
...
1584
1585
1586
1587
1588
1589
1590
1591
 
 
1592
1593
1594
...
1631
1632
1633
1634
1635
1636
1637
1638
 
 
1639
1640
1641
1642
1643
1644
1645
1646
1647
 
 
1648
1649
1650
...
1692
1693
1694
1695
1696
1697
 
1698
1699
1700
...
1723
1724
1725
1726
1727
1728
 
 
1729
1730
1731
...
1761
1762
1763
1764
1765
1766
 
1767
1768
1769
1770
1771
 
 
1772
1773
1774
...
2233
2234
2235
2236
2237
2238
 
2239
2240
2241
2242
 
2243
2244
2245
2246
 
2247
2248
2249
...
2267
2268
2269
2270
 
2271
2272
2273
...
2279
2280
2281
2282
2283
 
 
2284
2285
 
 
2286
2287
2288
2289
 
2290
2291
2292
2293
2294
2295
2296
 
 
2297
2298
2299
 
2300
2301
2302
2303
2304
2305
2306
 
2307
2308
2309
...
2315
2316
2317
2318
2319
2320
2321
 
 
 
 
 
 
 
 
2322
2323
2324
 
2325
2326
2327
...
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
 
 
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
 
 
2359
 
2360
2361
2362
...
2484
2485
2486
2487
2488
2489
2490
2491
2492
 
 
 
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
 
 
 
2503
2504
2505
...
2509
2510
2511
 
 
 
 
 
 
 
 
 
 
 
 
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
 
 
2532
2533
2534
2535
2536
2537
 
 
 
 
 
2538
2539
 
2540
2541
2542
2543
 
2544
2545
2546
2547
2548
2549
2550
2551
 
 
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
 
 
2576
2577
2578
2579
 
 
2580
2581
2582
...
2587
2588
2589
2590
 
 
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
 
 
 
2604
2605
2606
2607
2608
 
 
 
 
 
2609
2610
 
 
2611
2612
2613
...
2615
2616
2617
 
 
 
 
 
 
 
2618
2619
2620
...
2624
2625
2626
2627
2628
 
 
 
 
2629
2630
 
2631
2632
 
2633
2634
2635
...
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
 
2677
2678
2679
...
2759
2760
2761
2762
2763
2764
2765
2766
2767
 
 
 
2768
2769
2770
...
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
 
 
 
 
2788
2789
2790
...
2799
2800
2801
2802
2803
 
2804
2805
2806
2807
2808
 
2809
2810
2811
...
2958
2959
2960
 
 
2961
2962
2963
2964
2965
2966
2967
2968
2969
 
2970
2971
 
2972
2973
2974
2975
2976
2977
2978
2979
2980
 
 
 
 
 
 
 
 
2981
2982
2983
...
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
 
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
 
 
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
 
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
 
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
 
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
 
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
 
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
 
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
 
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
...
41
42
43
44
45
46
 
47
48
49
50
51
 
52
53
54
...
87
88
89
90
91
 
92
93
94
...
185
186
187
 
 
 
 
 
 
 
 
188
189
190
191
192
...
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
...
530
531
532
 
533
534
535
536
...
865
866
867
 
868
869
870
 
 
 
 
 
 
 
 
871
872
873
874
875
876
877
878
879
...
1006
1007
1008
 
 
 
 
1009
1010
1011
1012
1013
...
1453
1454
1455
 
 
 
 
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
...
1497
1498
1499
 
 
 
 
1500
1501
1502
1503
1504
...
1509
1510
1511
 
 
1512
1513
1514
1515
1516
...
1583
1584
1585
 
 
 
 
 
1586
1587
1588
1589
1590
...
1627
1628
1629
 
 
 
 
 
1630
1631
1632
1633
1634
1635
1636
 
 
 
 
1637
1638
1639
1640
1641
...
1683
1684
1685
 
 
 
1686
1687
1688
1689
...
1712
1713
1714
 
 
 
1715
1716
1717
1718
1719
...
1749
1750
1751
 
 
 
1752
1753
1754
1755
1756
 
1757
1758
1759
1760
1761
...
2220
2221
2222
 
 
 
2223
2224
2225
 
 
2226
2227
 
 
 
2228
2229
2230
2231
...
2249
2250
2251
 
2252
2253
2254
2255
...
2261
2262
2263
 
 
2264
2265
2266
 
2267
2268
2269
2270
2271
 
2272
2273
2274
2275
2276
2277
 
 
2278
2279
2280
2281
 
2282
2283
2284
2285
2286
2287
2288
 
2289
2290
2291
2292
...
2298
2299
2300
 
 
 
 
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
 
2311
2312
2313
2314
...
2320
2321
2322
 
 
 
 
 
 
 
 
 
 
 
 
2323
2324
2325
2326
 
 
 
 
 
 
 
 
 
2327
2328
2329
2330
2331
2332
2333
...
2455
2456
2457
 
 
 
 
 
 
2458
2459
2460
2461
2462
2463
2464
 
 
 
 
 
 
2465
2466
2467
2468
2469
2470
...
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
 
 
 
2490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2491
2492
2493
 
 
 
 
 
2494
2495
2496
2497
2498
2499
 
2500
2501
2502
 
 
2503
2504
2505
 
 
 
 
 
 
2506
2507
2508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2509
2510
2511
 
 
 
2512
2513
2514
2515
2516
...
2521
2522
2523
 
2524
2525
2526
2527
2528
2529
2530
2531
 
 
 
 
 
 
 
2532
2533
2534
2535
 
 
 
 
2536
2537
2538
2539
2540
2541
 
2542
2543
2544
2545
2546
...
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
...
2564
2565
2566
 
 
2567
2568
2569
2570
2571
 
2572
2573
 
2574
2575
2576
2577
...
2584
2585
2586
 
 
 
2587
2588
2589
2590
2591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2592
2593
2594
2595
...
2675
2676
2677
 
 
 
 
 
 
2678
2679
2680
2681
2682
2683
...
2691
2692
2693
 
 
 
 
 
 
 
2694
2695
2696
2697
2698
2699
2700
...
2709
2710
2711
 
 
2712
2713
2714
2715
2716
 
2717
2718
2719
2720
...
2867
2868
2869
2870
2871
2872
 
 
 
 
 
 
 
 
2873
2874
2875
2876
2877
 
 
 
 
 
 
 
 
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
...
2896
2897
2898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2899
2900
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2901
2902
2903
2904
2905
 
 
 
 
 
 
 
 
2906
2907
2908
2909
 
 
 
 
 
 
 
2910
2911
2912
2913
 
 
 
 
 
 
 
 
2914
2915
2916
2917
 
 
 
 
 
 
 
2918
2919
2920
2921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2922
2923
2924
2925
2926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2927
2928
2929
2930
2931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2932
2933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2934
2935
2936
0
@@ -41,15 +41,14 @@ import java.lang.reflect.Field;
0
 import java.lang.reflect.Method;
0
 import java.lang.reflect.Modifier;
0
 import java.util.ArrayList;
0
+import java.util.Collection;
0
 import java.util.Collections;
0
 import java.util.HashMap;
0
-import java.util.Iterator;
0
 import java.util.HashSet;
0
 import java.util.List;
0
 import java.util.Map;
0
 import java.util.Set;
0
 import java.util.concurrent.ConcurrentHashMap;
0
-import java.util.concurrent.atomic.AtomicInteger;
0
 import java.util.concurrent.locks.ReentrantLock;
0
 
0
 import org.jruby.anno.JRubyMethod;
0
@@ -88,8 +87,8 @@ import org.jruby.exceptions.RaiseException;
0
 import org.jruby.internal.runtime.methods.JavaMethod;
0
 import org.jruby.javasupport.util.RuntimeHelpers;
0
 import org.jruby.runtime.ClassIndex;
0
+import org.jruby.runtime.ConstantCacheMap;
0
 import org.jruby.runtime.MethodFactory;
0
-import org.jruby.runtime.MethodIndex;
0
 import org.jruby.util.collections.WeakHashSet;
0
 
0
 /**
0
@@ -186,15 +185,8 @@ public class RubyModule extends RubyObject {
0
     // write methods are overridden here to use this lock rather than Java
0
     // synchronization for faster concurrent writes for modules/classes.
0
     protected final ReentrantLock variableWriteLock = new ReentrantLock();
0
-    
0
-    protected transient volatile ConstantTableEntry[] constantTable =
0
-        new ConstantTableEntry[CONSTANT_TABLE_DEFAULT_CAPACITY];
0
-
0
-    protected transient int constantTableSize;
0
-
0
-    protected transient int constantTableThreshold = 
0
-        (int)(CONSTANT_TABLE_DEFAULT_CAPACITY * CONSTANT_TABLE_LOAD_FACTOR);
0
 
0
+    private final Map<String, IRubyObject> constants = new ConcurrentHashMap<String, IRubyObject>();
0
     private final Map<String, DynamicMethod> methods = new ConcurrentHashMap<String, DynamicMethod>(12, 0.75f, 1);
0
     private final Map<String, CacheEntry> cachedMethods = new ConcurrentHashMap<String, CacheEntry>(12, 0.75f, 1);
0
     
0
@@ -500,10 +492,21 @@ public class RubyModule extends RubyObject {
0
 
0
         infectBy(module);
0
 
0
+        flushConstants();
0
         doIncludeModule(module);
0
         invalidateCacheDescendants();
0
     }
0
 
0
+    private void flushConstants() {
0
+        ConstantCacheMap map = getRuntime().getConstantCacheMap();
0
+
0
+        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
+            for (String name : p.getConstantNames()) {
0
+                map.remove(name);
0
+            }
0
+        }
0
+    }
0
+
0
     public void defineMethod(String name, Callback method) {
0
         Visibility visibility = name.equals("initialize") ?
0
                 PRIVATE : PUBLIC;
0
@@ -527,7 +530,7 @@ public class RubyModule extends RubyObject {
0
     public void defineAnnotatedConstants(Class clazz) {
0
         Field[] declaredFields = clazz.getDeclaredFields();
0
         for (Field field : declaredFields) {
0
-            if(Modifier.isStatic(field.getModifiers())) {
0
+            if (Modifier.isStatic(field.getModifiers())) {
0
                 defineAnnotatedConstant(field);
0
             }
0
         }
0
@@ -862,17 +865,15 @@ public class RubyModule extends RubyObject {
0
             callMethod(context, "method_undefined", runtime.newSymbol(name));
0
         }
0
     }
0
-    
0
+
0
     @JRubyMethod(name = "include?", required = 1)
0
     public IRubyObject include_p(ThreadContext context, IRubyObject arg) {
0
-        if (!arg.isModule()) {
0
-            throw context.getRuntime().newTypeError(arg, context.getRuntime().getModule());
0
-        }
0
-        
0
-        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-            if ((p instanceof IncludedModuleWrapper) && ((IncludedModuleWrapper) p).getNonIncludedClass() == arg) {
0
-                return context.getRuntime().getTrue();
0
-            }
0
+        if (!arg.isModule()) throw context.getRuntime().newTypeError(arg, context.getRuntime().getModule());
0
+        RubyModule moduleToCompare = (RubyModule) arg;
0
+
0
+        // See if module is in chain...Cannot match against itself so start at superClass.
0
+        for (RubyModule p = getSuperClass(); p != null; p = p.getSuperClass()) {
0
+            if (p.isSame(moduleToCompare)) return context.getRuntime().getTrue();
0
         }
0
         
0
         return context.getRuntime().getFalse();
0
@@ -1005,10 +1006,8 @@ public class RubyModule extends RubyObject {
0
      * @return The method, or UndefinedMethod if not found
0
      */
0
     public RubyModule findImplementer(RubyModule clazz) {
0
-        for (RubyModule searchModule = this; searchModule != null; searchModule = searchModule.getSuperClass()) {
0
-            if (searchModule.isSame(clazz)) {
0
-                return searchModule;
0
-            }
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            if (module.isSame(clazz)) return module;
0
         }
0
 
0
         return null;
0
@@ -1454,16 +1453,18 @@ public class RubyModule extends RubyObject {
0
 
0
         if (!getMetaClass().isSingleton()) setMetaClass(originalModule.getSingletonClassClone());
0
         setSuperClass(originalModule.getSuperClass());
0
-
0
-        if (originalModule.hasVariables()){
0
-            syncVariables(originalModule.getVariableList());
0
-        }
0
+        if (originalModule.hasVariables()) syncVariables(originalModule.getVariableList());
0
+        syncConstants(originalModule);
0
 
0
         originalModule.cloneMethods(this);
0
 
0
         return this;
0
     }
0
 
0
+    public void syncConstants(RubyModule other) {
0
+        constants.putAll(other.constants);
0
+    }
0
+
0
     /** rb_mod_included_modules
0
      *
0
      */
0
@@ -1496,10 +1497,8 @@ public class RubyModule extends RubyObject {
0
     public List<IRubyObject> getAncestorList() {
0
         ArrayList<IRubyObject> list = new ArrayList<IRubyObject>();
0
 
0
-        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-            if(!p.isSingleton()) {
0
-                list.add(p.getNonIncludedClass());
0
-            }
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            if(!module.isSingleton()) list.add(module.getNonIncludedClass());
0
         }
0
 
0
         return list;
0
@@ -1510,8 +1509,8 @@ public class RubyModule extends RubyObject {
0
         // when scanning the hierarchy. However the == check may be safe; we should only ever have
0
         // one instance bound to a given type/constant. If it's found to be unsafe, examine ways
0
         // to avoid the == call.
0
-        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-            if (p.getNonIncludedClass() == type) return true;
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            if (module.getNonIncludedClass() == type) return true;
0
         }
0
 
0
         return false;
0
@@ -1584,11 +1583,8 @@ public class RubyModule extends RubyObject {
0
             throw getRuntime().newTypeError("compared with non class/module");
0
         }
0
 
0
-        if (isKindOfModule((RubyModule) obj)) {
0
-            return getRuntime().getTrue();
0
-        } else if (((RubyModule) obj).isKindOfModule(this)) {
0
-            return getRuntime().getFalse();
0
-        }
0
+        if (isKindOfModule((RubyModule) obj)) return getRuntime().getTrue();
0
+        if (((RubyModule) obj).isKindOfModule(this)) return getRuntime().getFalse();
0
 
0
         return getRuntime().getNil();
0
     }
0
@@ -1631,20 +1627,15 @@ public class RubyModule extends RubyObject {
0
 
0
         RubyModule module = (RubyModule) obj;
0
 
0
-        if (module.isKindOfModule(this)) {
0
-            return getRuntime().newFixnum(1);
0
-        } else if (this.isKindOfModule(module)) {
0
-            return getRuntime().newFixnum(-1);
0
-        }
0
+        if (module.isKindOfModule(this)) return getRuntime().newFixnum(1);
0
+        if (this.isKindOfModule(module)) return getRuntime().newFixnum(-1);
0
 
0
         return getRuntime().getNil();
0
     }
0
 
0
     public boolean isKindOfModule(RubyModule type) {
0
-        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-            if (p.isSame(type)) {
0
-                return true;
0
-            }
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            if (module.isSame(type)) return true;
0
         }
0
 
0
         return false;
0
@@ -1692,9 +1683,7 @@ public class RubyModule extends RubyObject {
0
         return getRuntime().getNil();
0
     }
0
 
0
-    /**
0
-     * @deprecated
0
-     */
0
+    @Deprecated
0
     public IRubyObject attr_reader(IRubyObject[] args) {
0
         return attr_reader(getRuntime().getCurrentContext(), args);
0
     }
0
@@ -1723,9 +1712,8 @@ public class RubyModule extends RubyObject {
0
         return context.getRuntime().getNil();
0
     }
0
 
0
-    /**
0
-     * @deprecated
0
-     */
0
+
0
+    @Deprecated
0
     public IRubyObject attr_accessor(IRubyObject[] args) {
0
         return attr_accessor(getRuntime().getCurrentContext(), args);
0
     }
0
@@ -1761,14 +1749,13 @@ public class RubyModule extends RubyObject {
0
 
0
         for (RubyModule type = this; type != null; type = type.getSuperClass()) {
0
             RubyModule realType = type.getNonIncludedClass();
0
-            for (Iterator iter = type.getMethods().entrySet().iterator(); iter.hasNext();) {
0
-                Map.Entry entry = (Map.Entry) iter.next();
0
-                DynamicMethod method = (DynamicMethod) entry.getValue();
0
+            for (Map.Entry entry : type.getMethods().entrySet()) {
0
                 String methodName = (String) entry.getKey();
0
 
0
                 if (! seen.contains(methodName)) {
0
                     seen.add(methodName);
0
-                    
0
+
0
+                    DynamicMethod method = (DynamicMethod) entry.getValue();
0
                     if (method.getImplementationClass() == realType &&
0
                         (!not && method.getVisibility() == visibility || (not && method.getVisibility() != visibility)) &&
0
                         ! method.isUndefined()) {
0
@@ -2233,17 +2220,12 @@ public class RubyModule extends RubyObject {
0
         Set<String> names = new HashSet<String>();
0
 
0
         for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-            for (String name : p.getClassVariableNameList()) {
0
-                names.add(name);
0
-            }
0
+            names.addAll(p.getClassVariableNameList());
0
         }
0
 
0
-        Ruby runtime = context.getRuntime();
0
-        RubyArray ary = runtime.newArray();
0
+        RubyArray ary = context.getRuntime().newArray();
0
 
0
-        for (String name : names) {
0
-            ary.append(runtime.newString(name));
0
-        }
0
+        ary.addAll(names);
0
 
0
         return ary;
0
     }
0
@@ -2267,7 +2249,7 @@ public class RubyModule extends RubyObject {
0
      */
0
     @JRubyMethod(name = "const_get", required = 1)
0
     public IRubyObject const_get(IRubyObject symbol) {
0
-        return fastGetConstant(validateConstant(symbol.asJavaString()).intern());
0
+        return getConstant(validateConstant(symbol.asJavaString()));
0
     }
0
 
0
     /** rb_mod_const_set
0
@@ -2279,31 +2261,32 @@ public class RubyModule extends RubyObject {
0
     }
0
 
0
     @JRubyMethod(name = "remove_const", required = 1, visibility = PRIVATE)
0
-    public IRubyObject remove_const(ThreadContext context, IRubyObject name) {
0
-        String id = validateConstant(name.asJavaString());
0
+    public IRubyObject remove_const(ThreadContext context, IRubyObject rubyName) {
0
+        String name = validateConstant(rubyName.asJavaString());
0
         IRubyObject value;
0
-        if ((value = deleteConstant(id)) != null) {
0
+        if ((value = deleteConstant(name)) != null) {
0
+            getRuntime().getConstantCacheMap().remove(name);
0
             if (value != UNDEF) {
0
                 return value;
0
             }
0
-            context.getRuntime().getLoadService().removeAutoLoadFor(getName() + "::" + id);
0
+            context.getRuntime().getLoadService().removeAutoLoadFor(getName() + "::" + name);
0
             // FIXME: I'm not sure this is right, but the old code returned
0
             // the undef, which definitely isn't right...
0
             return context.getRuntime().getNil();
0
         }
0
 
0
-        if (hasConstantInHierarchy(id)) {
0
-            throw cannotRemoveError(id);
0
+        if (hasConstantInHierarchy(name)) {
0
+            throw cannotRemoveError(name);
0
         }
0
 
0
-        throw context.getRuntime().newNameError("constant " + id + " not defined for " + getName(), id);
0
+        throw context.getRuntime().newNameError("constant " + name + " not defined for " + getName(), name);
0
     }
0
 
0
     private boolean hasConstantInHierarchy(final String name) {
0
         for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
             if (p.hasConstant(name)) {
0
                 return true;
0
-            }
0
+        }
0
         }
0
         return false;
0
     }
0
@@ -2315,13 +2298,17 @@ public class RubyModule extends RubyObject {
0
      * @return Nothing! Absolutely nothing! (though subclasses might choose to return something)
0
      */
0
     @JRubyMethod(name = "const_missing", required = 1, frame = true)
0
-    public IRubyObject const_missing(ThreadContext context, IRubyObject name, Block block) {
0
-        /* Uninitialized constant */
0
-        if (this != context.getRuntime().getObject()) {
0
-            throw context.getRuntime().newNameError("uninitialized constant " + getName() + "::" + name.asJavaString(), "" + getName() + "::" + name.asJavaString());
0
+    public IRubyObject const_missing(ThreadContext context, IRubyObject rubyName, Block block) {
0
+        Ruby runtime = context.getRuntime();
0
+        String name;
0
+        
0
+        if (this != runtime.getObject()) {
0
+            name = getName() + "::" + rubyName.asJavaString();
0
+        } else {
0
+            name = rubyName.asJavaString();
0
         }
0
 
0
-        throw context.getRuntime().newNameError("uninitialized constant " + name.asJavaString(), name.asJavaString());
0
+        throw runtime.newNameError("uninitialized constant " + name, name);
0
     }
0
 
0
     /** rb_mod_constants
0
@@ -2333,30 +2320,14 @@ public class RubyModule extends RubyObject {
0
         RubyArray array = runtime.newArray();
0
         RubyModule objectClass = runtime.getObject();
0
 
0
-        if (getRuntime().getModule() == this) {
0
-
0
-            for (String name : objectClass.getStoredConstantNameList()) {
0
-                array.append(runtime.newString(name));
0
-            }
0
-
0
-        } else if (objectClass == this) {
0
-
0
-            for (String name : getStoredConstantNameList()) {
0
-                array.append(runtime.newString(name));
0
-            }
0
-
0
+        if (getRuntime().getModule() == this || objectClass == this) {
0
+            array.addAll(objectClass.getConstantNames());
0
         } else {
0
             Set<String> names = new HashSet<String>();
0
-            for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
-                if (objectClass != p) {
0
-                    for (String name : p.getStoredConstantNameList()) {
0
-                        names.add(name);
0
-                    }
0
-                }
0
-            }
0
-            for (String name : names) {
0
-                array.append(runtime.newString(name));
0
+            for (RubyModule module = this; module != null && module != objectClass; module = module.getSuperClass()) {
0
+                names.addAll(module.getConstantNames());
0
             }
0
+            array.addAll(names);
0
         }
0
 
0
         return array;
0
@@ -2484,22 +2455,16 @@ public class RubyModule extends RubyObject {
0
     //
0
 
0
     public IRubyObject getConstantAt(String name) {
0
-        IRubyObject value;
0
-        if ((value = fetchConstant(name)) != UNDEF) {
0
-            return value;
0
-        }
0
-        deleteConstant(name);
0
-        return getRuntime().getLoadService().autoload(getName() + "::" + name);
0
+        IRubyObject value = fetchConstant(name);
0
+        
0
+        return value == UNDEF ? resolveUndefConstant(getRuntime(), name) : value;
0
     }
0
 
0
     public IRubyObject fastGetConstantAt(String internedName) {
0
         assert internedName == internedName.intern() : internedName + " is not interned";
0
-        IRubyObject value;
0
-        if ((value = fastFetchConstant(internedName)) != UNDEF) {
0
-            return value;
0
-        }
0
-        deleteConstant(internedName);
0
-        return getRuntime().getLoadService().autoload(getName() + "::" + internedName);
0
+        IRubyObject value = fastFetchConstant(internedName);
0
+
0
+        return value == UNDEF ? resolveUndefConstant(getRuntime(), internedName) : value;
0
     }
0
 
0
     /**
0
@@ -2509,74 +2474,43 @@ public class RubyModule extends RubyObject {
0
      * @return The value for the constant, or null if not found
0
      */
0
     public IRubyObject getConstant(String name) {
0
+        return fastGetConstant(name);
0
+    }
0
+    
0
+    public IRubyObject fastGetConstant(String internedName) {
0
+        IRubyObject value = getConstantNoConstMissing(internedName);
0
+        Ruby runtime = getRuntime();
0
+        
0
+        return value == null ? callMethod(runtime.getCurrentContext(), "const_missing",
0
+                runtime.fastNewSymbol(internedName)) : value;
0
+    }
0
+    
0
+    public IRubyObject getConstantNoConstMissing(String name) {
0
         assert IdUtil.isConstant(name);
0
-        boolean retryForModule = false;
0
-        IRubyObject value;
0
-        RubyModule p = this;
0
 
0
-        retry: while (true) {
0
-            while (p != null) {
0
-                if ((value = p.constantTableFetch(name)) != null) {
0
-                    if (value != UNDEF) {
0
-                        return value;
0
-                    }
0
-                    p.deleteConstant(name);
0
-                    if (getRuntime().getLoadService().autoload(
0
-                            p.getName() + "::" + name) == null) {
0
-                        break;
0
-                    }
0
-                    continue;
0
-                }
0
-                p = p.getSuperClass();
0
-            }
0
+        for (RubyModule p = this; p != null; p = p.getSuperClass()) {
0
+            IRubyObject value = p.getConstantInner(name);
0
 
0
-            if (!retryForModule && !isClass()) {
0
-                retryForModule = true;
0
-                p = getRuntime().getObject();
0
-                continue retry;
0
-            }
0
+            if (value != null) return value == UNDEF ? null : value;
0
+        }
0
+
0
+        if (!isClass()) {
0
+            IRubyObject value = getRuntime().getObject().getConstantInner(name);
0
 
0
-            break;
0
+            return value == UNDEF ? null : value;
0
         }
0
 
0
-        return callMethod(getRuntime().getCurrentContext(),
0
-                "const_missing", getRuntime().newSymbol(name));
0
+        return null;
0
     }
0
     
0
-    public IRubyObject fastGetConstant(String internedName) {
0
-        assert internedName == internedName.intern() : internedName + " is not interned";
0
-        assert IdUtil.isConstant(internedName);
0
-        boolean retryForModule = false;
0
-        IRubyObject value;
0
-        RubyModule p = this;
0
+    protected IRubyObject getConstantInner(String name) {
0
+        IRubyObject value = constantTableFetch(name);
0
 
0
-        retry: while (true) {
0
-            while (p != null) {
0
-                if ((value = p.constantTableFastFetch(internedName)) != null) {
0
-                    if (value != UNDEF) {
0
-                        return value;
0
-                    }
0
-                    p.deleteConstant(internedName);
0
-                    if (getRuntime().getLoadService().autoload(
0
-                            p.getName() + "::" + internedName) == null) {
0
-                        break;
0
-                    }
0
-                    continue;
0
-                }
0
-                p = p.getSuperClass();
0
-            }
0
-
0
-            if (!retryForModule && !isClass()) {
0
-                retryForModule = true;
0
-                p = getRuntime().getObject();
0
-                continue retry;
0
-            }
0
-
0
-            break;
0
+        for (; value == UNDEF; value = constantTableFetch(name)) {
0
+            if (resolveUndefConstant(getRuntime(), name) == null) return UNDEF;
0
         }
0
-
0
-        return callMethod(getRuntime().getCurrentContext(),
0
-                "const_missing", getRuntime().fastNewSymbol(internedName));
0
+        
0
+        return value;
0
     }
0
 
0
     // not actually called anywhere (all known uses call the fast version)
0
@@ -2587,27 +2521,26 @@ public class RubyModule extends RubyObject {
0
     public IRubyObject fastGetConstantFrom(String internedName) {
0
         assert internedName == internedName.intern() : internedName + " is not interned";
0
         assert IdUtil.isConstant(internedName);
0
-        RubyClass objectClass = getRuntime().getObject();
0
+        Ruby runtime = getRuntime();
0
+        RubyClass objectClass = runtime.getObject();
0
         IRubyObject value;
0
 
0
         RubyModule p = this;
0
         
0
         while (p != null) {
0
             if ((value = p.constantTableFastFetch(internedName)) != null) {
0
-                if (value != UNDEF) {
0
-                    if (p == objectClass && this != objectClass) {
0
-                        String badCName = getName() + "::" + internedName;
0
-                        getRuntime().getWarnings().warn(ID.CONSTANT_BAD_REFERENCE, "toplevel constant " + 
0
-                                internedName + " referenced by " + badCName, badCName);
0
-                    }
0
-                    return value;
0
+                if (value == UNDEF) {
0
+                    if (p.resolveUndefConstant(runtime, internedName) == null) break;
0
+                    continue; // Not that is loaded loop around to resolve it next pass
0
                 }
0
-                p.deleteConstant(internedName);
0
-                if (getRuntime().getLoadService().autoload(
0
-                        p.getName() + "::" + internedName) == null) {
0
-                    break;
0
+
0
+                if (p == objectClass && this != objectClass) {
0
+                    String badCName = getName() + "::" + internedName;
0
+                    runtime.getWarnings().warn(ID.CONSTANT_BAD_REFERENCE, "toplevel constant " +
0
+                            internedName + " referenced by " + badCName, badCName);
0
                 }
0
-                continue;
0
+
0
+                return value;
0
             }
0
             p = p.getSuperClass();
0
         }
0
@@ -2615,6 +2548,13 @@ public class RubyModule extends RubyObject {
0
         return callMethod(getRuntime().getCurrentContext(),
0
                 "const_missing", getRuntime().fastNewSymbol(internedName));
0
     }
0
+    
0
+    public IRubyObject resolveUndefConstant(Ruby runtime, String name) {
0
+        deleteConstant(name);
0
+        
0
+        return runtime.getLoadService().autoload(getName() + "::" + name);
0
+    }
0
+    
0
     /**
0
      * Set the named constant on this module. Also, if the value provided is another Module and
0
      * that module has not yet been named, assign it the specified name.
0
@@ -2624,12 +2564,14 @@ public class RubyModule extends RubyObject {
0
      * @return The result of setting the variable.
0
      */
0
     public IRubyObject setConstant(String name, IRubyObject value) {
0
-        IRubyObject oldValue;
0
-        if ((oldValue = fetchConstant(name)) != null) {
0
+        IRubyObject oldValue = fetchConstant(name);
0
+        if (oldValue != null) {
0
+            Ruby runtime = getRuntime();
0
+            runtime.getConstantCacheMap().remove(name);
0
             if (oldValue == UNDEF) {
0
-                getRuntime().getLoadService().removeAutoLoadFor(getName() + "::" + name);
0
+                runtime.getLoadService().removeAutoLoadFor(getName() + "::" + name);
0
             } else {
0
-                getRuntime().getWarnings().warn(ID.CONSTANT_ALREADY_INITIALIZED, "already initialized constant " + name, name);
0
+                runtime.getWarnings().warn(ID.CONSTANT_ALREADY_INITIALIZED, "already initialized constant " + name, name);
0
             }
0
         }
0
 
0
@@ -2642,38 +2584,12 @@ public class RubyModule extends RubyObject {
0
                 module.setBaseName(name);
0
                 module.setParent(this);
0
             }
0
-            /*
0
-            module.setParent(this);
0
-            */
0
         }
0
         return value;
0
     }
0
 
0
     public IRubyObject fastSetConstant(String internedName, IRubyObject value) {
0
-        assert internedName == internedName.intern() : internedName + " is not interned";
0
-        IRubyObject oldValue;
0
-        if ((oldValue = fastFetchConstant(internedName)) != null) {
0
-            if (oldValue == UNDEF) {
0
-                getRuntime().getLoadService().removeAutoLoadFor(getName() + "::" + internedName);
0
-            } else {
0
-                getRuntime().getWarnings().warn(ID.CONSTANT_ALREADY_INITIALIZED, "already initialized constant " + internedName, internedName);
0
-            }
0
-        }
0
-
0
-        fastStoreConstant(internedName, value);
0
-
0
-        // if adding a module under a constant name, set that module's basename to the constant name
0
-        if (value instanceof RubyModule) {
0
-            RubyModule module = (RubyModule)value;
0
-            if (module.getBaseName() == null) {
0
-                module.setBaseName(internedName);
0
-                module.setParent(this);
0
-            }
0
-            /*
0
-            module.setParent(this);
0
-            */
0
-        }
0
-        return value;
0
+        return setConstant(internedName, value);
0
     }
0
     
0
     /** rb_define_const
0
@@ -2759,12 +2675,9 @@ public class RubyModule extends RubyObject {
0
      * @see #setInternalModuleVariable(String, IRubyObject)
0
      */
0
     public boolean hasInternalModuleVariable(final String name) {
0
-        RubyModule module = this;
0
-        do {
0
-            if (module.hasInternalVariable(name)) {
0
-                return true;
0
-            }
0
-        } while ((module = module.getSuperClass()) != null);
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            if (module.hasInternalVariable(name)) return true;
0
+        }
0
 
0
         return false;
0
     }
0
@@ -2778,13 +2691,10 @@ public class RubyModule extends RubyObject {
0
      * @see #setInternalModuleVariable(String, IRubyObject)
0
      */
0
     public IRubyObject searchInternalModuleVariable(final String name) {
0
-        RubyModule module = this;
0
-        IRubyObject value;
0
-        do {
0
-            if ((value = module.getInternalVariable(name)) != null) {
0
-                return value;
0
-            }
0
-        } while ((module = module.getSuperClass()) != null);
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
+            IRubyObject value = module.getInternalVariable(name);
0
+            if (value != null) return value;
0
+        }
0
 
0
         return null;
0
     }
0
@@ -2799,13 +2709,12 @@ public class RubyModule extends RubyObject {
0
      * @see #searchInternalModuleVariable(String)
0
      */
0
     public void setInternalModuleVariable(final String name, final IRubyObject value) {
0
-        RubyModule module = this;
0
-        do {
0
+        for (RubyModule module = this; module != null; module = module.getSuperClass()) {
0
             if (module.hasInternalVariable(name)) {
0
                 module.setInternalVariable(name, value);
0
                 return;
0
             }
0
-        } while ((module = module.getSuperClass()) != null);
0
+        }
0
 
0
         setInternalVariable(name, value);
0
     }
0
@@ -2958,26 +2867,22 @@ public class RubyModule extends RubyObject {
0
         return constantTableRemove(name);
0
     }
0
 
0
+
0
+    @Deprecated
0
     public List<Variable<IRubyObject>> getStoredConstantList() {
0
-        ArrayList<Variable<IRubyObject>> list = new ArrayList<Variable<IRubyObject>>();
0
-        ConstantTableEntry[] table = constantTableGetTable();
0
-        for (int i = table.length; --i >= 0; ) {
0
-            for (ConstantTableEntry e = table[i]; e != null; e = e.next) {
0
-                list.add(e);
0
-            }
0
-        }
0
-        return list;
0
+        return null;
0
     }
0
 
0
+    @Deprecated
0
     public List<String> getStoredConstantNameList() {
0
-        ArrayList<String> list = new ArrayList<String>();
0
-        ConstantTableEntry[] table = constantTableGetTable();
0
-        for (int i = table.length; --i >= 0; ) {
0
-            for (ConstantTableEntry e = table[i]; e != null; e = e.next) {
0
-                list.add(e.name);
0
-            }
0
-        }
0
-        return list;
0
+        return new ArrayList<String>(constants.keySet());
0
+    }
0
+
0
+    /**
0
+     * @return a list of constant names that exists at time this was called
0
+     */
0
+    public Collection<String> getConstantNames() {
0
+        return constants.keySet();
0
     }
0
 
0
     protected static final String ERR_INSECURE_SET_CONSTANT  = "Insecure: can't modify constant";
0
@@ -2991,583 +2896,41 @@ public class RubyModule extends RubyObject {
0
     }
0
 
0
     protected final void ensureConstantsSettable() {
0
-        Ruby runtime = getRuntime();
0
-        
0
-        if (!isFrozen() && (runtime.getSafeLevel() < 4 || isTaint())) {
0
-            return;
0
-        }
0
-        
0
-        if (runtime.getSafeLevel() >= 4 && !isTaint()) {
0
-            throw runtime.newSecurityError(ERR_INSECURE_SET_CONSTANT);
0
-        }
0
-        if (isFrozen()) {
0
-            if (this instanceof RubyModule) {
0
-                throw runtime.newFrozenError(ERR_FROZEN_CONST_TYPE);
0
-            } else {
0
-                throw runtime.newFrozenError("");
0
-            }
0
-        }
0
-    }
0
-
0
-     
0
-    //
0
-    ////////////////// VARIABLE TABLE METHODS ////////////////
0
-    //
0
-    // Overridden to use variableWriteLock in place of synchronization  
0
-    //
0
-
0
-    @Override
0
-    protected IRubyObject variableTableStore(String name, IRubyObject value) {
0
-        int hash = name.hashCode();
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            VariableTableEntry[] table;
0
-            VariableTableEntry e;
0
-            if ((table = variableTable) == null) {
0
-                table =  new VariableTableEntry[VARIABLE_TABLE_DEFAULT_CAPACITY];
0
-                e = new VariableTableEntry(hash, name.intern(), value, null);
0
-                table[hash & (VARIABLE_TABLE_DEFAULT_CAPACITY - 1)] = e;
0
-                variableTableThreshold = (int)(VARIABLE_TABLE_DEFAULT_CAPACITY * VARIABLE_TABLE_LOAD_FACTOR);
0
-                variableTableSize = 1;
0
-                variableTable = table;
0
-                return value;
0
-            }
0
-            int potentialNewSize;
0
-            if ((potentialNewSize = variableTableSize + 1) > variableTableThreshold) {
0
-                table = variableTableRehash();
0
-            }
0
-            int index;
0
-            for (e = table[index = hash & (table.length - 1)]; e != null; e = e.next) {
0
-                if (hash == e.hash && name.equals(e.name)) {
0
-                    e.value = value;
0
-                    return value;
0
-                }
0
-            }
0
-            // external volatile value initialization intended to obviate the need for
0
-            // readValueUnderLock technique used in ConcurrentHashMap. may be a little
0
-            // slower, but better to pay a price on first write rather than all reads.
0
-            e = new VariableTableEntry(hash, name.intern(), value, table[index]);
0
-            table[index] = e;
0
-            variableTableSize = potentialNewSize;
0
-            variableTable = table; // write-volatile
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-        return value;
0
-    }
0
-    
0
-    @Override
0
-    protected IRubyObject variableTableFastStore(String internedName, IRubyObject value) {
0
-        assert internedName == internedName.intern() : internedName + " not interned";
0
-        int hash = internedName.hashCode();
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            VariableTableEntry[] table;
0
-            VariableTableEntry e;
0
-            if ((table = variableTable) == null) {
0
-                table =  new VariableTableEntry[VARIABLE_TABLE_DEFAULT_CAPACITY];
0
-                e = new VariableTableEntry(hash, internedName, value, null);
0
-                table[hash & (VARIABLE_TABLE_DEFAULT_CAPACITY - 1)] = e;
0
-                variableTableThreshold = (int)(VARIABLE_TABLE_DEFAULT_CAPACITY * VARIABLE_TABLE_LOAD_FACTOR);
0
-                variableTableSize = 1;
0
-                variableTable = table;
0
-                return value;
0
-            }
0
-            int potentialNewSize;
0
-            if ((potentialNewSize = variableTableSize + 1) > variableTableThreshold) {
0
-                table = variableTableRehash();
0
-            }
0
-            int index;
0
-            for (e = table[index = hash & (table.length - 1)]; e != null; e = e.next) {
0
-                if (internedName == e.name) {
0
-                    e.value = value;
0
-                    return value;
0
-                }
0
-            }
0
-            // external volatile value initialization intended to obviate the need for
0
-            // readValueUnderLock technique used in ConcurrentHashMap. may be a little
0
-            // slower, but better to pay a price on first write rather than all reads.
0
-            e = new VariableTableEntry(hash, internedName, value, table[index]);
0
-            table[index] = e;
0
-            variableTableSize = potentialNewSize;
0
-            variableTable = table; // write-volatile
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-        return value;
0
-    }
0
-
0
-    @Override   
0
-    protected IRubyObject variableTableRemove(String name) {
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            VariableTableEntry[] table;
0
-            if ((table = variableTable) != null) {
0
-                int hash = name.hashCode();
0
-                int index = hash & (table.length - 1);
0
-                VariableTableEntry first = table[index];
0
-                VariableTableEntry e;
0
-                for (e = first; e != null; e = e.next) {
0
-                    if (hash == e.hash && name.equals(e.name)) {
0
-                        IRubyObject oldValue = e.value;
0
-                        // All entries following removed node can stay
0
-                        // in list, but all preceding ones need to be
0
-                        // cloned.
0
-                        VariableTableEntry newFirst = e.next;
0
-                        for (VariableTableEntry p = first; p != e; p = p.next) {
0
-                            newFirst = new VariableTableEntry(p.hash, p.name, p.value, newFirst);
0
-                        }
0
-                        table[index] = newFirst;
0
-                        variableTableSize--;
0
-                        variableTable = table; // write-volatile 
0
-                        return oldValue;
0
-                    }
0
-                }
0
-            }
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-        return null;
0
-    }
0
-    
0
-    @Override
0
-    protected IRubyObject variableTableReadLocked(VariableTableEntry entry) {
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            return entry.value;
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-    }
0
-    
0
-    @Override
0
-    protected void variableTableSync(List<Variable<IRubyObject>> vars) {
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            variableTableSize = 0;
0
-            variableTableThreshold = (int)(VARIABLE_TABLE_DEFAULT_CAPACITY * VARIABLE_TABLE_LOAD_FACTOR);
0
-            variableTable =  new VariableTableEntry[VARIABLE_TABLE_DEFAULT_CAPACITY];
0
-            for (Variable<IRubyObject> var : vars) {
0
-                assert !var.isConstant() && var.getValue() != null;
0
-                variableTableStore(var.getName(), var.getValue());
0
-            }
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-    }
0
-    
0
-    @Override
0
-    public void syncVariables(List<Variable<IRubyObject>> variables) {
0
-        ArrayList<Variable<IRubyObject>> constants = new ArrayList<Variable<IRubyObject>>(variables.size());
0
-        Variable<IRubyObject> var;
0
-        for (Iterator<Variable<IRubyObject>> iter = variables.iterator(); iter.hasNext(); ) {
0
-            if ((var = iter.next()).isConstant()) {
0
-                constants.add(var);
0
-                iter.remove();
0
-            }
0
-        }
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            variableTableSync(variables);
0
-            constantTableSync(constants);
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-    }
0
-
0
-    @Override
0
-    @SuppressWarnings("unchecked")
0
-    @Deprecated // born deprecated
0
-    public Map getVariableMap() {
0
-        Map map = variableTableGetMap();
0
-        constantTableGetMap(map);
0
-        return map;
0
-    }
0
-
0
-    @Override
0
-    public boolean hasVariables() {
0
-        return variableTableGetSize() > 0 || constantTableGetSize() > 0;
0
-    }
0
-
0
-    @Override
0
-    public int getVariableCount() {
0
-        return variableTableGetSize() + constantTableGetSize();
0
-    }
0
-    
0
-    @Override
0
-    public List<Variable<IRubyObject>> getVariableList() {
0
-        VariableTableEntry[] vtable = variableTableGetTable();
0
-        ConstantTableEntry[] ctable = constantTableGetTable();
0
-        ArrayList<Variable<IRubyObject>> list = new ArrayList<Variable<IRubyObject>>();
0
-        IRubyObject readValue;
0
-        for (int i = vtable.length; --i >= 0; ) {
0
-            for (VariableTableEntry e = vtable[i]; e != null; e = e.next) {
0
-                if ((readValue = e.value) == null) readValue = variableTableReadLocked(e);
0
-                list.add(new VariableEntry<IRubyObject>(e.name, readValue));
0
-            }
0
-        }
0
-        for (int i = ctable.length; --i >= 0; ) {
0
-            for (ConstantTableEntry e = ctable[i]; e != null; e = e.next) {
0
-                list.add(e);
0
-            }
0
-        }
0
-        return list;
0
-    }
0
+        boolean isSecure = getRuntime().getSafeLevel() >= 4 && !isTaint();
0
 
0
-    @Override
0
-    public List<String> getVariableNameList() {
0
-        VariableTableEntry[] vtable = variableTableGetTable();
0
-        ConstantTableEntry[] ctable = constantTableGetTable();
0
-        ArrayList<String> list = new ArrayList<String>();
0
-        for (int i = vtable.length; --i >= 0; ) {
0
-            for (VariableTableEntry e = vtable[i]; e != null; e = e.next) {
0
-                list.add(e.name);
0
-            }
0
-        }
0
-        for (int i = ctable.length; --i >= 0; ) {
0
-            for (ConstantTableEntry e = ctable[i]; e != null; e = e.next) {
0
-                list.add(e.name);
0
-            }
0
-        }
0
-        return list;
0
-    }
0
-    
0
-
0
-    //
0
-    ////////////////// CONSTANT TABLE METHODS, ETC. ////////////////
0
-    //
0
-    
0
-    protected static final int CONSTANT_TABLE_DEFAULT_CAPACITY = 8; // MUST be power of 2!
0
-    protected static final int CONSTANT_TABLE_MAXIMUM_CAPACITY = 1 << 30;
0
-    protected static final float CONSTANT_TABLE_LOAD_FACTOR = 0.75f;
0
-
0
-    protected static final class ConstantTableEntry implements Variable<IRubyObject> {
0
-        final int hash;
0
-        final String name;
0
-        final IRubyObject value;
0
-        final ConstantTableEntry next;
0
-
0
-        // constant table entry values are final; if a constant is redefined, the
0
-        // entry will be removed and replaced with a new entry.
0
-        ConstantTableEntry(
0
-                int hash,
0
-                String name,
0
-                IRubyObject value,
0
-                ConstantTableEntry next) {
0
-            this.hash = hash;
0
-            this.name = name;
0
-            this.value = value;
0
-            this.next = next;
0
-        }
0
-        
0
-        public String getName() {
0
-            return name;
0
-        }
0
-        
0
-        public IRubyObject getValue() {
0
-            return value;
0
-        }
0
-        public final boolean isClassVariable() {
0
-            return false;
0
-        }
0
-        
0
-        public final boolean isConstant() {
0
-            return true;
0
-        }
0
-        
0
-        public final boolean isInstanceVariable() {
0
-            return false;
0
-        }
0
-
0
-        public final boolean isRubyVariable() {
0
-            return true;
0
-        }
0
+        if (isSecure) throw getRuntime().newSecurityError(ERR_INSECURE_SET_CONSTANT);
0
+        if (isFrozen()) throw getRuntime().newFrozenError(ERR_FROZEN_CONST_TYPE);
0
     }
0
 
0
     protected boolean constantTableContains(String name) {
0
-        int hash = name.hashCode();
0
-        ConstantTableEntry[] table;
0
-        for (ConstantTableEntry e = (table = constantTable)[hash & (table.length - 1)]; e != null; e = e.next) {
0
-            if (hash == e.hash && name.equals(e.name)) {
0
-                return true;
0
-            }
0
-        }
0
-        return false;
0
+        return constants.containsKey(name);
0
     }
0
     
0
     protected boolean constantTableFastContains(String internedName) {
0
-        ConstantTableEntry[] table;
0
-        for (ConstantTableEntry e = (table = constantTable)[internedName.hashCode() & (table.length - 1)]; e != null; e = e.next) {
0
-            if (internedName == e.name) {
0
-                return true;
0
-            }
0
-        }
0
-        return false;
0
+        return constants.containsKey(internedName);
0
     }
0
     
0
     protected IRubyObject constantTableFetch(String name) {
0
-        int hash = name.hashCode();
0
-        ConstantTableEntry[] table;
0
-        for (ConstantTableEntry e = (table = constantTable)[hash & (table.length - 1)]; e != null; e = e.next) {
0
-            if (hash == e.hash && name.equals(e.name)) {
0
-                return e.value;
0
-            }
0
-        }
0
-        return null;
0
+        return constants.get(name);
0
     }
0
     
0
     protected IRubyObject constantTableFastFetch(String internedName) {
0
-        ConstantTableEntry[] table;
0
-        for (ConstantTableEntry e = (table = constantTable)[internedName.hashCode() & (table.length - 1)]; e != null; e = e.next) {
0
-            if (internedName == e.name) {
0
-                return e.value;
0
-            }
0
-        }
0
-        return null;
0
+        return constants.get(internedName);
0
     }
0
     
0
     protected IRubyObject constantTableStore(String name, IRubyObject value) {
0
-        int hash = name.hashCode();
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            ConstantTableEntry[] table;
0
-            ConstantTableEntry e;
0
-            ConstantTableEntry first;
0
-            int potentialNewSize;
0
-            if ((potentialNewSize = constantTableSize + 1) > constantTableThreshold) {
0
-                table = constantTableRehash();
0
-            } else {
0
-                table = constantTable;
0
-            }
0
-            int index;
0
-            for (e = first = table[index = hash & (table.length - 1)]; e != null; e = e.next) {
0
-                if (hash == e.hash && name.equals(e.name)) {
0
-                    // if value is unchanged, do nothing
0
-                    if (value == e.value) {
0
-                        return value;
0
-                    }
0
-                    // create new entry, prepend to any trailing entries
0
-                    ConstantTableEntry newFirst = new ConstantTableEntry(e.hash, e.name, value, e.next);
0
-                    // all entries before this one must be cloned
0
-                    for (ConstantTableEntry n = first; n != e; n = n.next) {
0
-                        newFirst = new ConstantTableEntry(n.hash, n.name, n.value, newFirst);
0
-                    }
0
-                    table[index] = newFirst;
0
-                    constantTable = table; // write-volatile
0
-                    return value;
0
-                }
0
-            }
0
-            table[index] = new ConstantTableEntry(hash, name.intern(), value, table[index]);
0
-            constantTableSize = potentialNewSize;
0
-            constantTable = table; // write-volatile
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
+        constants.put(name, value);
0
         return value;
0
     }
0
     
0
     protected IRubyObject constantTableFastStore(String internedName, IRubyObject value) {
0
-        assert internedName == internedName.intern() : internedName + " not interned";
0
-        int hash = internedName.hashCode();
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            ConstantTableEntry[] table;
0
-            ConstantTableEntry e;
0
-            ConstantTableEntry first;
0
-            int potentialNewSize;
0
-            if ((potentialNewSize = constantTableSize + 1) > constantTableThreshold) {
0
-                table = constantTableRehash();
0
-            } else {
0
-                table = constantTable;
0
-            }
0
-            int index;
0
-            for (e = first = table[index = hash & (table.length - 1)]; e != null; e = e.next) {
0
-                if (internedName == e.name) {
0
-                    // if value is unchanged, do nothing
0
-                    if (value == e.value) {
0
-                        return value;
0
-                    }
0
-                    // create new entry, prepend to any trailing entries
0
-                    ConstantTableEntry newFirst = new ConstantTableEntry(e.hash, e.name, value, e.next);
0
-                    // all entries before this one must be cloned
0
-                    for (ConstantTableEntry n = first; n != e; n = n.next) {
0
-                        newFirst = new ConstantTableEntry(n.hash, n.name, n.value, newFirst);
0
-                    }
0
-                    table[index] = newFirst;
0
-                    constantTable = table; // write-volatile
0
-                    return value;
0
-                }
0
-            }
0
-            table[index] = new ConstantTableEntry(hash, internedName, value, table[index]);
0
-            constantTableSize = potentialNewSize;
0
-            constantTable = table; // write-volatile
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
+        constants.put(internedName, value);
0
         return value;
0
     }
0
         
0
     protected IRubyObject constantTableRemove(String name) {
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            ConstantTableEntry[] table;
0
-            if ((table = constantTable) != null) {
0
-                int hash = name.hashCode();
0
-                int index = hash & (table.length - 1);
0
-                ConstantTableEntry first = table[index];
0
-                ConstantTableEntry e;
0
-                for (e = first; e != null; e = e.next) {
0
-                    if (hash == e.hash && name.equals(e.name)) {
0
-                        IRubyObject oldValue = e.value;
0
-                        // All entries following removed node can stay
0
-                        // in list, but all preceding ones need to be
0
-                        // cloned.
0
-                        ConstantTableEntry newFirst = e.next;
0
-                        for (ConstantTableEntry p = first; p != e; p = p.next) {
0
-                            newFirst = new ConstantTableEntry(p.hash, p.name, p.value, newFirst);
0
-                        }
0
-                        table[index] = newFirst;
0
-                        constantTableSize--;
0
-                        constantTable = table; // write-volatile 
0
-                        return oldValue;
0
-                    }
0
-                }
0
-            }
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-        return null;
0
-    }
0
-    
0
-
0
-    protected ConstantTableEntry[] constantTableGetTable() {
0
-        return constantTable;
0
+        return constants.remove(name);
0
     }
0
-    
0
-    protected int constantTableGetSize() {
0
-        if (constantTable != null) {
0
-            return constantTableSize;
0
-        }
0
-        return 0;
0
-    }
0
-    
0
-    protected void constantTableSync(List<Variable<IRubyObject>> vars) {
0
-        ReentrantLock lock;
0
-        (lock = variableWriteLock).lock();
0
-        try {
0
-            constantTableSize = 0;
0
-            constantTableThreshold = (int)(CONSTANT_TABLE_DEFAULT_CAPACITY * CONSTANT_TABLE_LOAD_FACTOR);
0
-            constantTable =  new ConstantTableEntry[CONSTANT_TABLE_DEFAULT_CAPACITY];
0
-            for (Variable<IRubyObject> var : vars) {
0
-                assert var.isConstant() && var.getValue() != null;
0
-                constantTableStore(var.getName(), var.getValue());
0
-            }
0
-        } finally {
0
-            lock.unlock();
0
-        }
0
-    }
0
-    
0
-    // MUST be called from synchronized/locked block!
0
-    // should only be called by constantTableStore/constantTableFastStore
0
-    private final ConstantTableEntry[] constantTableRehash() {
0
-        ConstantTableEntry[] oldTable = constantTable;
0
-        int oldCapacity;
0
-        if ((oldCapacity = oldTable.length) >= CONSTANT_TABLE_MAXIMUM_CAPACITY) {
0
-            return oldTable;
0
-        }
0
-        
0
-        int newCapacity = oldCapacity << 1;
0
-        ConstantTableEntry[] newTable = new ConstantTableEntry[newCapacity];
0
-        constantTableThreshold = (int)(newCapacity * CONSTANT_TABLE_LOAD_FACTOR);
0
-        int sizeMask = newCapacity - 1;
0
-        ConstantTableEntry e;
0
-        for (int i = oldCapacity; --i >= 0; ) {
0
-            // We need to guarantee that any existing reads of old Map can
0
-            //  proceed. So we cannot yet null out each bin.
0
-            e = oldTable[i];
0
-
0
-            if (e != null) {
0
-                ConstantTableEntry next = e.next;
0
-                int idx = e.hash & sizeMask;
0
-
0
-                //  Single node on list
0
-                if (next == null)
0
-                    newTable[idx] = e;
0
-
0
-                else {
0
-                    // Reuse trailing consecutive sequence at same slot
0
-                    ConstantTableEntry lastRun = e;
0
-                    int lastIdx = idx;
0
-                    for (ConstantTableEntry last = next;
0
-                         last != null;
0
-                         last = last.next) {
0
-                        int k = last.hash & sizeMask;
0
-                        if (k != lastIdx) {
0
-                            lastIdx = k;
0
-                            lastRun = last;
0
-                        }
0
-                    }
0
-                    newTable[lastIdx] = lastRun;
0
-
0
-                    // Clone all remaining nodes
0
-                    for (ConstantTableEntry p = e; p != lastRun; p = p.next) {
0
-                        int k = p.hash & sizeMask;
0
-                        ConstantTableEntry m = new ConstantTableEntry(p.hash, p.name, p.value, newTable[k]);
0
-                        newTable[k] = m;
0
-                    }
0
-                }
0
-            }
0
-        }
0
-        constantTable = newTable;
0
-        return newTable;
0
-    }
0
-    
0
-
0
-    /**
0
-     * Method to help ease transition to new variables implementation.
0
-     * Will likely be deprecated in the near future.
0
-     */
0
-    @SuppressWarnings("unchecked")
0
-    protected Map constantTableGetMap() {
0
-        HashMap map = new HashMap();
0
-        ConstantTableEntry[] table;
0
-        if ((table = constantTable) != null) {
0
-            for (int i = table.length; --i >= 0; ) {
0
-                for (ConstantTableEntry e = table[i]; e != null; e = e.next) {
0
-                    map.put(e.name, e.value);
0
-                }
0
-            }
0
-        }
0
-        return map;
0
-    }
0
-    
0
-    /**
0
-     * Method to help ease transition to new variables implementation.
0
-     * Will likely be deprecated in the near future.
0
-     */
0
-    @SuppressWarnings("unchecked")
0
-    protected Map constantTableGetMap(Map map) {
0
-        ConstantTableEntry[] table;
0
-        if ((table = constantTable) != null) {
0
-            for (int i = table.length; --i >= 0; ) {
0
-                for (ConstantTableEntry e = table[i]; e != null; e = e.next) {
0
-                    map.put(e.name, e.value);
0
-                }
0
-            }
0
-        }
0
-        return map;
0
-    }
0
-
0
-    private CacheEntry UNDEFINED_METHOD = new CacheEntry(-1, UndefinedMethod.getInstance());
0
 
0
     public static class CacheEntry {
0
         private int generation;
...
631
632
633
634
635
636
 
 
637
638
639
...
654
655
656
657
658
659
 
 
660
661
662
...
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
 
1541
1542
1543
...
2716
2717
2718
2719
2720
2721
2722
...
2771
2772
2773
 
 
2774
2775
2776
...
631
632
633
 
 
 
634
635
636
637
638
...
653
654
655
 
 
 
656
657
658
659
660
...
1526
1527
1528
 
 
 
 
 
 
 
 
 
 
1529
1530
1531
1532
...
2705
2706
2707
 
2708
2709
2710
...
2759
2760
2761
2762
2763
2764
2765
2766
0
@@ -631,9 +631,8 @@ public class RubyObject implements Cloneable, IRubyObject, Serializable, CoreObj
0
 
0
        clone.setSuperClass(klass.getSuperClass());
0
 
0
-       if (klass.hasVariables()) {
0
-           clone.syncVariables(klass.getVariableList());
0
-       }
0
+       if (klass.hasVariables()) clone.syncVariables(klass.getVariableList());
0
+       clone.syncConstants(klass);
0
 
0
        klass.cloneMethods(clone);
0
 
0
@@ -654,9 +653,8 @@ public class RubyObject implements Cloneable, IRubyObject, Serializable, CoreObj
0
 
0
         original.copySpecialInstanceVariables(clone);
0
 
0
-        if (original.hasVariables()) {
0
-            clone.syncVariables(original.getVariableList());
0
-        }
0
+        if (original.hasVariables()) clone.syncVariables(original.getVariableList());
0
+        if (original instanceof RubyModule) ((RubyModule) clone).syncConstants((RubyModule) original);
0
 
0
         /* FIXME: finalizer should be dupped here */
0
         clone.callMethod(clone.getRuntime().getCurrentContext(), "initialize_copy", original);
0
@@ -1528,16 +1526,7 @@ public class RubyObject implements Cloneable, IRubyObject, Serializable, CoreObj
0
     @JRubyMethod(name = "inspect")
0
     public IRubyObject inspect() {
0
         Ruby runtime = getRuntime();
0
-        if ((!isImmediate()) &&
0
-                // TYPE(obj) == T_OBJECT
0
-                !(this instanceof RubyClass) &&
0
-                this != runtime.getObject() &&
0
-                this != runtime.getModule() &&
0
-                !(this instanceof RubyModule) &&
0
-                // TODO: should have #hasInstanceVariables method, though
0
-                // this will work here:
0
-                hasVariables()) {
0
-
0
+        if ((!isImmediate()) && !(this instanceof RubyModule) && hasVariables()) {
0
             StringBuilder part = new StringBuilder();
0
             String cname = getMetaClass().getRealClass().getName();
0
             part.append("#<").append(cname).append(":0x");
0
@@ -2716,7 +2705,6 @@ public class RubyObject implements Cloneable, IRubyObject, Serializable, CoreObj
0
      * @return the object or null if not found
0
      */
0
     protected IRubyObject variableTableFastFetch(String internedName) {
0
-        assert internedName == internedName.intern() : internedName + " not interned";
0
         VariableTableEntry[] table;
0
         IRubyObject readValue;
0
         if ((table = variableTable) != null) {
0
@@ -2771,6 +2759,8 @@ public class RubyObject implements Cloneable, IRubyObject, Serializable, CoreObj
0
      * needs to be an interned Java String.
0
      */
0
     protected IRubyObject variableTableFastStore(String internedName, IRubyObject value) {
0
+        if (IdUtil.isConstant(internedName)) new Exception().printStackTrace();
0
+
0
         assert internedName == internedName.intern() : internedName + " not interned";
0
         int hash = internedName.hashCode();
0
         synchronized(this) {
...
41
42
43
 
44
45
46
47
48
 
 
 
49
 
50
51
52
...
84
85
86
87
 
 
 
 
 
88
89
90
91
92
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
...
41
42
43
44
45
46
47
48
 
49
50
51
52
53
54
55
56
...
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
0
@@ -41,12 +41,16 @@ import org.jruby.lexer.yacc.ISourcePosition;
0
 import org.jruby.runtime.Block;
0
 import org.jruby.runtime.ThreadContext;
0
 import org.jruby.runtime.builtin.IRubyObject;
0
+import org.jruby.runtime.callsite.ConstantSite;
0
 
0
 /**
0
  * The access to a Constant.
0
  */
0
-public class ConstNode extends Node implements INameNode {
0
+public class ConstNode extends Node implements INameNode, ConstantSite {
0
+    public static volatile int failedCallSites;
0
+
0
     private String name;
0
+    private transient IRubyObject cachedValue = null;
0
     
0
     public ConstNode(ISourcePosition position, String name) {
0
         super(position, NodeType.CONSTNODE);
0
@@ -84,11 +88,36 @@ public class ConstNode extends Node implements INameNode {
0
     
0
     @Override
0
     public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
0
-        return context.getConstant(name);
0
+        IRubyObject value = getValue(context);
0
+
0
+        // We can callsite cache const_missing if we want
0
+        return value != null ? value :
0
+            context.getRubyClass().callMethod(context, "const_missing", runtime.fastNewSymbol(name));
0
     }
0
 
0
     @Override
0
     public String definition(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
0
         return context.getConstantDefined(name) ? "constant" : null;
0
     }
0
+    
0
+    public IRubyObject getValue(ThreadContext context) {
0
+        IRubyObject value = cachedValue; // Store to temp so it does null out on us mid-stream
0
+
0
+         return value == null ? reCache(context, name) : value;
0
+    }
0
+    
0
+    public IRubyObject reCache(ThreadContext context, String name) {
0
+        IRubyObject value = context.getConstant(name);
0
+            
0
+        cachedValue = value;
0
+            
0
+        if (value != null) context.getRuntime().getConstantCacheMap().add(name, this);
0
+        
0
+        return value;
0
+    }
0
+    
0
+    public void invalidate() {
0
+        cachedValue = null;
0
+        failedCallSites++;
0
+    }
0
 }
...
310
311
312
313
 
314
315
316
...
310
311
312
 
313
314
315
316
0
@@ -310,7 +310,7 @@ public abstract class BaseBodyCompiler implements BodyCompiler {
0
     public void retrieveConstant(String name) {
0
         loadThreadContext();
0
         method.ldc(name);
0
-        invokeThreadContext("getConstant", sig(IRubyObject.class, params(String.class)));
0
+        invokeUtilityMethod("getConstant", sig(IRubyObject.class, params(ThreadContext.class, String.class)));
0
     }
0
 
0
     public void retrieveConstantFromModule(String name) {
...
445
446
447
 
 
 
 
 
 
448
449
450
...
445
446
447
448
449
450
451
452
453
454
455
456
0
@@ -445,6 +445,12 @@ public class RuntimeHelpers {
0
         return rubyClass.fastGetClassVar(internedName);
0
     }
0
     
0
+    public static IRubyObject getConstant(ThreadContext context, String internedName) {
0
+        Ruby runtime = context.getRuntime();
0
+
0
+        return context.getCurrentScope().getStaticScope().getConstantWithConstMissing(runtime, internedName, runtime.getObject());
0
+    }
0
+    
0
     public static IRubyObject nullToNil(IRubyObject value, Ruby runtime) {
0
         return value != null ? value : runtime.getNil();
0
     }
...
119
120
121
 
 
 
 
 
 
 
 
122
123
124
125
126
127
 
128
129
130
...
142
143
144
145
146
147
 
148
149
150
...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 
135
136
137
138
...
150
151
152
 
 
 
153
154
155
156
0
@@ -119,12 +119,20 @@ public abstract class StaticScope implements Serializable {
0
         System.arraycopy(names, 0, variableNames, 0, names.length);
0
         variableCaptured = new boolean[variableNames.length];
0
     }
0
+
0
+    /* Note: Only used by compiler until it can use getConstant again or use some other refactoring */
0
+    public IRubyObject getConstantWithConstMissing(Ruby runtime, String internedName, RubyModule object) {
0
+        IRubyObject result = getConstantInner(runtime, internedName, object);
0
+
0
+        // If we could not find the constant from cref..then try getting from inheritence hierarchy
0
+        return result == null ? cref.fastGetConstant(internedName) : result;        
0
+    }
0
     
0
     public IRubyObject getConstant(Ruby runtime, String internedName, RubyModule object) {
0
         IRubyObject result = getConstantInner(runtime, internedName, object);
0
 
0
         // If we could not find the constant from cref..then try getting from inheritence hierarchy
0
-        return result == null ? cref.fastGetConstant(internedName) : result;
0
+        return result == null ? cref.getConstantNoConstMissing(internedName) : result;
0
     }
0
     
0
     private IRubyObject getConstantInner(Ruby runtime, String internedName, RubyModule object) {
0
@@ -142,9 +150,7 @@ public abstract class StaticScope implements Serializable {
0
 
0
     /* Try and unload the autoload specified from internedName */
0
     private IRubyObject getUndefConstant(Ruby runtime, String internedName, RubyModule object) {
0
-        cref.deleteConstant(internedName);
0
-
0
-        if (runtime.getLoadService().autoload(cref.getName() + "::" + internedName) == null) return null;
0
+        if (cref.resolveUndefConstant(runtime, internedName) == null) return null;
0
 
0
         return getConstantInner(runtime, internedName, object);
0
     }
...
37
38
39
 
 
 
 
 
40
...
37
38
39
40
41
42
43
44
45
0
@@ -37,4 +37,9 @@ public class VariableEntry<BaseObjectType> implements Variable<BaseObjectType> {
0
         char c;
0
         return name.length() > 0 && ((c = name.charAt(0)) == '@' || (c <= 'Z' && c >= 'A'));
0
     }
0
+
0
+    @Override
0
+    public String toString() {
0
+        return "Name: " + getName();
0
+    }
0
 }

Comments