Skip to content

Commit

Permalink
Refactor specialization to use helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mpage committed Oct 31, 2024
1 parent 89834dd commit 781a040
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1530,97 +1530,85 @@ specialize_load_global_lock_held(
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)(instr + 1);
assert(PyUnicode_CheckExact(name));
if (!PyDict_CheckExact(globals)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail;
unspecialize(instr, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
return;
}
PyDictKeysObject * globals_keys = ((PyDictObject *)globals)->ma_keys;
if (!DK_IS_UNICODE(globals_keys)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
goto fail;
unspecialize(instr, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
return;
}
Py_ssize_t index = _PyDictKeys_StringLookup(globals_keys, name);
if (index == DKIX_ERROR) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
goto fail;
unspecialize(instr, SPEC_FAIL_EXPECTED_ERROR);
return;
}
uint8_t specialized_opcode;
_PyLoadGlobalCache new_cache;
PyInterpreterState *interp = _PyInterpreterState_GET();
if (index != DKIX_EMPTY) {
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_RANGE);
return;
}
uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
interp, globals_keys);
if (keys_version == 0) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
return;
}
if (keys_version != (uint16_t)keys_version) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_RANGE);
return;
}
_PyLoadGlobalCache new_cache;
new_cache.index = (uint16_t)index;
new_cache.module_keys_version = (uint16_t)keys_version;
specialized_opcode = LOAD_GLOBAL_MODULE;
goto success;
specialize(instr, LOAD_GLOBAL_MODULE, &new_cache);
return;
}
if (!PyDict_CheckExact(builtins)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
goto fail;
unspecialize(instr, SPEC_FAIL_LOAD_GLOBAL_NON_DICT);
return;
}
PyDictKeysObject * builtin_keys = ((PyDictObject *)builtins)->ma_keys;
if (!DK_IS_UNICODE(builtin_keys)) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
goto fail;
unspecialize(instr, SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT);
return;
}
index = _PyDictKeys_StringLookup(builtin_keys, name);
if (index == DKIX_ERROR) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
goto fail;
unspecialize(instr, SPEC_FAIL_EXPECTED_ERROR);
return;
}
if (index != (uint16_t)index) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_RANGE);
return;
}
uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(
interp, globals_keys);
if (globals_version == 0) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
return;
}
if (globals_version != (uint16_t)globals_version) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_RANGE);
return;
}
uint32_t builtins_version = _PyDictKeys_GetVersionForCurrentState(
interp, builtin_keys);
if (builtins_version == 0) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
return;
}
if (builtins_version > UINT16_MAX) {
SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
unspecialize(instr, SPEC_FAIL_OUT_OF_RANGE);
return;
}
_PyLoadGlobalCache new_cache;
new_cache.index = (uint16_t)index;
new_cache.module_keys_version = (uint16_t)globals_version;
new_cache.builtin_keys_version = (uint16_t)builtins_version;
specialized_opcode = LOAD_GLOBAL_BUILTIN;
goto success;
fail:
SET_OPCODE_OR_RETURN(instr, LOAD_GLOBAL);
STAT_INC(LOAD_GLOBAL, failure);
assert(!PyErr_Occurred());
cache->counter = adaptive_counter_backoff(cache->counter);
return;
success:
SET_OPCODE_OR_RETURN(instr, specialized_opcode);
STAT_INC(LOAD_GLOBAL, success);
*cache = new_cache;
assert(!PyErr_Occurred());
cache->counter = adaptive_counter_cooldown();
specialize(instr, LOAD_GLOBAL_BUILTIN, &new_cache);
}

void
Expand Down

0 comments on commit 781a040

Please sign in to comment.