FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-106485: Dematerialize instance dictionaries when possible by brandtbucher · Pull Request #106539 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Add stats
  • Loading branch information
brandtbucher committed Jun 29, 2023
commit 716cc5a5a8b4e4f919bc42f37df03d5657c3fa0f
1 change: 1 addition & 0 deletions Include/pystats.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef struct _object_stats {
uint64_t dict_materialized_new_key;
uint64_t dict_materialized_too_big;
uint64_t dict_materialized_str_subclass;
uint64_t dict_unmaterialized;
uint64_t type_cache_hits;
uint64_t type_cache_misses;
uint64_t type_cache_dunder_hits;
Expand Down
40 changes: 19 additions & 21 deletions Python/bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1850,27 +1850,25 @@ dummy_func(
res = ep->me_value;
}
if (res == NULL) {
// This is almost never an AttributeError... it's way more
// likely that this __dict__ still shares its keys (for example,
// if it was materialized on request and not heavily modified).
// If that's the case, we probably have an opportunity to do
// something *really* cool: un-materialize it!
PyDictKeysObject *keys = dict->ma_keys;
PyDictValues *values = dict->ma_values;
if (Py_REFCNT(dict) == 1
&& _PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)
&& keys == ((PyHeapTypeObject *)tp)->ht_cached_keys)
{
assert(values);
// Don't try this at home, kids:
dict->ma_keys = NULL;
dict->ma_values = NULL;
Py_DECREF(dict);
_PyDictKeys_DecRef(keys);
_PyDictOrValues_SetValues(dorv, values);
GO_TO_INSTRUCTION(LOAD_ATTR_INSTANCE_VALUE);
}
DEOPT_IF(true, LOAD_ATTR);
// This is almost never an AttributeError. It's way more likely
// that this __dict__ still shares its keys (for example, if it
// was materialized on request and not heavily modified):
DEOPT_IF(!_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE), LOAD_ATTR);
PyHeapTypeObject *ht = (PyHeapTypeObject *)tp;
DEOPT_IF(dict->ma_keys != ht->ht_cached_keys, LOAD_ATTR);
assert(dict->ma_values);
DEOPT_IF(Py_REFCNT(dict) != 1, LOAD_ATTR);
// We have an opportunity to do something *really* cool:
// un-materialize it!
_PyDictKeys_DecRef(dict->ma_keys);
_PyDictOrValues_SetValues(dorv, dict->ma_values);
OBJECT_STAT_INC(dict_unmaterialized);
// Don't try this at home, kids:
dict->ma_keys = NULL;
dict->ma_values = NULL;
Py_DECREF(dict);
// Guess what... our caches are still valid!
GO_TO_INSTRUCTION(LOAD_ATTR_INSTANCE_VALUE);
}
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
Expand Down
Loading

Back | FazBrowse Home | New Git URL