@@ -5625,9 +5625,9 @@ get_local_type_cache(PyTypeObject *type, unsigned int assigned_version)
5625
5625
return NULL ;
5626
5626
}
5627
5627
5628
- #define HASH_NAME (name ) (((Py_ssize_t)(name)) >> 6)
5628
+ #define HASH_NAME (name ) (((( Py_ssize_t)(name)) >> 6) % LOCAL_TYPE_CACHE_SIZE )
5629
5629
5630
- static bool
5630
+ static inline bool
5631
5631
try_local_cache_lookup (PyTypeObject * type , PyObject * name , PyObject * * value , unsigned int * version )
5632
5632
{
5633
5633
if (!can_cache_locally (type , name )) {
@@ -5639,9 +5639,15 @@ try_local_cache_lookup(PyTypeObject *type, PyObject *name, PyObject **value, uns
5639
5639
return false;
5640
5640
}
5641
5641
5642
- Py_ssize_t index = HASH_NAME (name ) % LOCAL_TYPE_CACHE_SIZE ;
5642
+ Py_ssize_t index = HASH_NAME (name );
5643
5643
Py_ssize_t cur = index ;
5644
+ #if 0
5645
+ static int probe , count ;
5646
+ #endif
5644
5647
do {
5648
+ #if 0
5649
+ local_cache -> probes [cur ]++ ;
5650
+ #endif
5645
5651
struct local_type_cache_entry * entry = & local_cache -> entries [cur ];
5646
5652
PyObject * entry_name = _Py_atomic_load_ptr_acquire (& entry -> name );
5647
5653
if (entry_name == name ) {
@@ -5656,18 +5662,89 @@ try_local_cache_lookup(PyTypeObject *type, PyObject *name, PyObject **value, uns
5656
5662
if (version ) {
5657
5663
* version = local_cache -> tp_version_tag ;
5658
5664
}
5665
+
5666
+ #if 0
5667
+ if (hits [cur ] - probes [cur ] < -50000 ) {
5668
+ count ++ ;
5669
+ if (count < 50 ) {
5670
+ printf ("misfire: %s %s(%p) %ld %s(%p) %ld %d\n" ,
5671
+ type -> tp_name ,
5672
+
5673
+ PyUnicode_AsUTF8 (name ),
5674
+ name ,
5675
+ cur ,
5676
+
5677
+ PyUnicode_AsUTF8 (local_cache -> entries [index ].name ),
5678
+ local_cache -> entries [index ].name ,
5679
+ index ,
5680
+
5681
+ local_cache -> cache_count );
5682
+
5683
+ static int foo [LOCAL_TYPE_CACHE_SIZE + 1 ];
5684
+ for (int j = 2 ; j < LOCAL_TYPE_CACHE_SIZE + 1 ; j ++ ) {
5685
+ for (int i = 0 ; i < LOCAL_TYPE_CACHE_SIZE + 1 ; i ++ ) {
5686
+ foo [i ] = 0 ;
5687
+ }
5688
+ for (int i = 0 ; i < LOCAL_TYPE_CACHE_SIZE ; i ++ ) {
5689
+ PyObject * name = local_cache -> entries [i ].name ;
5690
+ if (name != NULL ) {
5691
+ Py_ssize_t idx = ((Py_ssize_t )(name )) % j ;
5692
+ foo [idx ] += 1 ;
5693
+ if (name != NULL ) {
5694
+ //printf("%d %s %p\n", i, PyUnicode_AsUTF8(name), name);
5695
+ }
5696
+ }
5697
+ }
5698
+ int collisions = 0 ;
5699
+ for (int i = 0 ; i < LOCAL_TYPE_CACHE_SIZE + 1 ; i ++ ) {
5700
+ if (foo [i ] > 1 ) {
5701
+ collisions += foo [i ];
5702
+ }
5703
+ }
5704
+ printf ("Good match at %d %d\n" , j , collisions );
5705
+ }
5706
+ }
5707
+ }
5708
+ #endif
5709
+
5710
+ #if 0
5711
+ local_cache -> hits [cur ]++ ;
5712
+ static int hit_count ;
5713
+ hit_count ++ ;
5714
+
5715
+ if ((hit_count % 500000 ) == 0 ) {
5716
+ printf ("Avg %d %d\n" , hit_count /LOCAL_TYPE_CACHE_SIZE , probe );
5717
+ for (int i = 0 ;i < LOCAL_TYPE_CACHE_SIZE ; i ++ ) {
5718
+ PyObject * name = local_cache -> entries [i ].name ;
5719
+ printf ("%02x hits: %6d probes: %6d delta: %6d missed: %d %p %s %lx\n" ,
5720
+ i ,
5721
+ local_cache -> hits [i ],
5722
+ local_cache -> probes [i ], local_cache -> probes [i ] - local_cache -> hits [i ], local_cache -> miss [i ],
5723
+ name ,
5724
+ name != NULL ? PyUnicode_AsUTF8 (name ) : "<NULL>" ,
5725
+ name != NULL ? (HASH_NAME (name ) % LOCAL_TYPE_CACHE_SIZE ) : 0 );
5726
+ }
5727
+ }
5728
+ #endif
5659
5729
5660
5730
return true;
5661
5731
}
5662
5732
else if (entry_name == NULL ) {
5663
5733
break ;
5664
5734
}
5735
+ #if 0
5736
+ probe ++ ;
5737
+ #endif
5665
5738
cur = (cur + LOCAL_TYPE_CACHE_PROBE ) % LOCAL_TYPE_CACHE_SIZE ;
5666
5739
} while (cur != index );
5740
+ #if 0
5741
+ local_cache -> miss [index ]++ ;
5742
+ #endif
5667
5743
return false;
5668
5744
}
5669
5745
5670
- static bool
5746
+
5747
+ static inline bool
5671
5748
cache_local_type_lookup (PyTypeObject * type , PyObject * name ,
5672
5749
PyObject * res , unsigned int assigned_version )
5673
5750
{
@@ -5682,7 +5759,7 @@ cache_local_type_lookup(PyTypeObject *type, PyObject *name,
5682
5759
return false;
5683
5760
}
5684
5761
5685
- Py_ssize_t index = HASH_NAME (name ) % LOCAL_TYPE_CACHE_SIZE ;
5762
+ Py_ssize_t index = HASH_NAME (name );
5686
5763
Py_ssize_t cur = index ;
5687
5764
do {
5688
5765
struct local_type_cache_entry * entry = & local_cache -> entries [cur ];
0 commit comments