@@ -276,7 +276,6 @@ pub struct TypeSpecializationCache {
276276 pub init : PyAtomicRef < Option < PyFunction > > ,
277277 pub getitem : PyAtomicRef < Option < PyFunction > > ,
278278 pub getitem_version : AtomicU32 ,
279- // Serialize cache writes/invalidation similar to CPython's BEGIN_TYPE_LOCK.
280279 write_lock : PyMutex < ( ) > ,
281280 retired : PyRwLock < Vec < PyObjectRef > > ,
282281}
@@ -302,9 +301,6 @@ impl TypeSpecializationCache {
302301 #[ inline]
303302 fn swap_init ( & self , new_init : Option < PyRef < PyFunction > > , vm : Option < & VirtualMachine > ) {
304303 if let Some ( vm) = vm {
305- // Keep replaced refs alive for the currently executing frame, matching
306- // CPython-style "old pointer remains valid during ongoing execution"
307- // without accumulating global retired refs.
308304 self . init . swap_to_temporary_refs ( new_init, vm) ;
309305 return ;
310306 }
@@ -329,8 +325,6 @@ impl TypeSpecializationCache {
329325 #[ inline]
330326 fn invalidate_for_type_modified ( & self ) {
331327 let _guard = self . write_lock . lock ( ) ;
332- // _spec_cache contract: type modification invalidates all cached
333- // specialization functions.
334328 self . swap_init ( None , None ) ;
335329 self . swap_getitem ( None , None ) ;
336330 }
@@ -962,26 +956,25 @@ impl PyType {
962956 if func_version == 0 {
963957 return false ;
964958 }
965- ext. specialization_cache
966- . swap_getitem ( Some ( getitem) , Some ( vm) ) ;
967959 ext. specialization_cache
968960 . getitem_version
969- . store ( func_version, Ordering :: Relaxed ) ;
961+ . store ( func_version, Ordering :: Release ) ;
962+ ext. specialization_cache
963+ . swap_getitem ( Some ( getitem) , Some ( vm) ) ;
970964 true
971965 }
972966
973967 /// Read cached __getitem__ for BINARY_OP_SUBSCR_GETITEM specialization.
974968 pub ( crate ) fn get_cached_getitem_for_specialization ( & self ) -> Option < ( PyRef < PyFunction > , u32 ) > {
975969 let ext = self . heaptype_ext . as_ref ( ) ?;
976- // Match CPython check order: pointer (Acquire) then function version.
977970 let getitem = ext
978971 . specialization_cache
979972 . getitem
980973 . to_owned_ordering ( Ordering :: Acquire ) ?;
981974 let cached_version = ext
982975 . specialization_cache
983976 . getitem_version
984- . load ( Ordering :: Relaxed ) ;
977+ . load ( Ordering :: Acquire ) ;
985978 if cached_version == 0 {
986979 return None ;
987980 }
0 commit comments