mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	bpo-46841: Move the cache for LOAD_GLOBAL inline. (GH-31575)
				
					
				
			This commit is contained in:
		
							parent
							
								
									da7d99a4de
								
							
						
					
					
						commit
						4558af5a8f
					
				
					 10 changed files with 284 additions and 232 deletions
				
			
		| 
						 | 
				
			
			@ -59,7 +59,6 @@ static uint8_t adaptive_opcodes[256] = {
 | 
			
		|||
/* The number of cache entries required for a "family" of instructions. */
 | 
			
		||||
static uint8_t cache_requirements[256] = {
 | 
			
		||||
    [LOAD_ATTR] = 1,  // _PyAdaptiveEntry
 | 
			
		||||
    [LOAD_GLOBAL] = 2, /* _PyAdaptiveEntry and _PyLoadGlobalCache */
 | 
			
		||||
    [LOAD_METHOD] = 3, /* _PyAdaptiveEntry, _PyAttrCache and _PyObjectCache */
 | 
			
		||||
    [BINARY_SUBSCR] = 2, /* _PyAdaptiveEntry, _PyObjectCache */
 | 
			
		||||
    [STORE_SUBSCR] = 0,
 | 
			
		||||
| 
						 | 
				
			
			@ -1208,11 +1207,12 @@ _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
 | 
			
		|||
int
 | 
			
		||||
_Py_Specialize_LoadGlobal(
 | 
			
		||||
    PyObject *globals, PyObject *builtins,
 | 
			
		||||
    _Py_CODEUNIT *instr, PyObject *name,
 | 
			
		||||
    SpecializedCacheEntry *cache)
 | 
			
		||||
    _Py_CODEUNIT *instr, PyObject *name)
 | 
			
		||||
{
 | 
			
		||||
    _PyAdaptiveEntry *cache0 = &cache->adaptive;
 | 
			
		||||
    _PyLoadGlobalCache *cache1 = &cache[-1].load_global;
 | 
			
		||||
    assert(_PyOpcode_InlineCacheEntries[LOAD_GLOBAL] ==
 | 
			
		||||
           INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
 | 
			
		||||
    /* Use inline cache */
 | 
			
		||||
    _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
 | 
			
		||||
    assert(PyUnicode_CheckExact(name));
 | 
			
		||||
    if (!PyDict_CheckExact(globals)) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
| 
						 | 
				
			
			@ -1231,8 +1231,8 @@ _Py_Specialize_LoadGlobal(
 | 
			
		|||
        if (keys_version == 0) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
        cache1->module_keys_version = keys_version;
 | 
			
		||||
        cache0->index = (uint16_t)index;
 | 
			
		||||
        cache->index = (uint16_t)index;
 | 
			
		||||
        write32(&cache->module_keys_version, keys_version);
 | 
			
		||||
        *instr = _Py_MAKECODEUNIT(LOAD_GLOBAL_MODULE, _Py_OPARG(*instr));
 | 
			
		||||
        goto success;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -1258,20 +1258,24 @@ _Py_Specialize_LoadGlobal(
 | 
			
		|||
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    cache1->module_keys_version = globals_version;
 | 
			
		||||
    cache1->builtin_keys_version = builtins_version;
 | 
			
		||||
    cache0->index = (uint16_t)index;
 | 
			
		||||
    if (builtins_version > UINT16_MAX) {
 | 
			
		||||
        SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    cache->index = (uint16_t)index;
 | 
			
		||||
    write32(&cache->module_keys_version, globals_version);
 | 
			
		||||
    cache->builtin_keys_version = (uint16_t)builtins_version;
 | 
			
		||||
    *instr = _Py_MAKECODEUNIT(LOAD_GLOBAL_BUILTIN, _Py_OPARG(*instr));
 | 
			
		||||
    goto success;
 | 
			
		||||
fail:
 | 
			
		||||
    STAT_INC(LOAD_GLOBAL, failure);
 | 
			
		||||
    assert(!PyErr_Occurred());
 | 
			
		||||
    cache_backoff(cache0);
 | 
			
		||||
    cache->counter = ADAPTIVE_CACHE_BACKOFF;
 | 
			
		||||
    return 0;
 | 
			
		||||
success:
 | 
			
		||||
    STAT_INC(LOAD_GLOBAL, success);
 | 
			
		||||
    assert(!PyErr_Occurred());
 | 
			
		||||
    cache0->counter = initial_counter_value();
 | 
			
		||||
    cache->counter = initial_counter_value();
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue