Skip to content

Commit 67fc05f

Browse files
committed
Add type cache stats for dunders and collisions.
1 parent 0ceb725 commit 67fc05f

File tree

4 files changed

+31
-49
lines changed

4 files changed

+31
-49
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ struct type_cache_entry {
3636
};
3737

3838
#define MCACHE_SIZE_EXP 12
39-
#define MCACHE_STATS 0
4039

4140
struct type_cache {
4241
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
43-
#if MCACHE_STATS
44-
size_t hits;
45-
size_t misses;
46-
size_t collisions;
47-
#endif
4842
};
4943

5044
/* For now we hard-code this to a value for which we are confident

Include/pystats.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ typedef struct _object_stats {
6767
uint64_t dict_materialized_str_subclass;
6868
uint64_t type_cache_hits;
6969
uint64_t type_cache_misses;
70+
uint64_t type_cache_dunder_hits;
71+
uint64_t type_cache_dunder_misses;
72+
uint64_t type_cache_collisions;
7073
} ObjectStats;
7174

75+
#
76+
7277
typedef struct _stats {
7378
OpcodeStats opcode_stats[256];
7479
CallStats call_stats;

Objects/typeobject.c

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,6 @@ static unsigned int
327327
_PyType_ClearCache(PyInterpreterState *interp)
328328
{
329329
struct type_cache *cache = &interp->types.type_cache;
330-
#if MCACHE_STATS
331-
size_t total = cache->hits + cache->collisions + cache->misses;
332-
fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n",
333-
cache->hits, (int) (100.0 * cache->hits / total));
334-
fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n",
335-
cache->misses, (int) (100.0 * cache->misses / total));
336-
fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n",
337-
cache->collisions, (int) (100.0 * cache->collisions / total));
338-
fprintf(stderr, "-- Method cache size = %zd KiB\n",
339-
sizeof(cache->hashtable) / 1024);
340-
#endif
341-
342330
// Set to None, rather than NULL, so _PyType_Lookup() can
343331
// use Py_SETREF() rather than using slower Py_XSETREF().
344332
type_cache_clear(cache, Py_None);
@@ -4147,6 +4135,24 @@ find_name_in_mro(PyTypeObject *type, PyObject *name, int *error)
41474135
return res;
41484136
}
41494137

4138+
/* Check if the "readied" PyUnicode name
4139+
is a double-underscore special name. */
4140+
static int
4141+
is_dunder_name(PyObject *name)
4142+
{
4143+
Py_ssize_t length = PyUnicode_GET_LENGTH(name);
4144+
int kind = PyUnicode_KIND(name);
4145+
/* Special names contain at least "__x__" and are always ASCII. */
4146+
if (length > 4 && kind == PyUnicode_1BYTE_KIND) {
4147+
const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name);
4148+
return (
4149+
((characters[length-2] == '_') && (characters[length-1] == '_')) &&
4150+
((characters[0] == '_') && (characters[1] == '_'))
4151+
);
4152+
}
4153+
return 0;
4154+
}
4155+
41504156
/* Internal API to look for a name through the MRO.
41514157
This returns a borrowed reference, and doesn't set an exception! */
41524158
PyObject *
@@ -4160,14 +4166,13 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
41604166
struct type_cache_entry *entry = &cache->hashtable[h];
41614167
if (entry->version == type->tp_version_tag &&
41624168
entry->name == name) {
4163-
#if MCACHE_STATS
4164-
cache->hits++;
4165-
#endif
41664169
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
4167-
OBJECT_STAT_INC(type_cache_hits);
4170+
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
4171+
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));
41684172
return entry->value;
41694173
}
4170-
OBJECT_STAT_INC(type_cache_misses);
4174+
OBJECT_STAT_INC_COND(type_cache_misses, !is_dunder_name(name));
4175+
OBJECT_STAT_INC_COND(type_cache_dunder_misses, is_dunder_name(name));
41714176

41724177
/* We may end up clearing live exceptions below, so make sure it's ours. */
41734178
assert(!PyErr_Occurred());
@@ -4195,14 +4200,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
41954200
entry->version = type->tp_version_tag;
41964201
entry->value = res; /* borrowed */
41974202
assert(_PyASCIIObject_CAST(name)->hash != -1);
4198-
#if MCACHE_STATS
4199-
if (entry->name != Py_None && entry->name != name) {
4200-
cache->collisions++;
4201-
}
4202-
else {
4203-
cache->misses++;
4204-
}
4205-
#endif
4203+
OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
42064204
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
42074205
Py_SETREF(entry->name, Py_NewRef(name));
42084206
}
@@ -4219,24 +4217,6 @@ _PyType_LookupId(PyTypeObject *type, _Py_Identifier *name)
42194217
return _PyType_Lookup(type, oname);
42204218
}
42214219

4222-
/* Check if the "readied" PyUnicode name
4223-
is a double-underscore special name. */
4224-
static int
4225-
is_dunder_name(PyObject *name)
4226-
{
4227-
Py_ssize_t length = PyUnicode_GET_LENGTH(name);
4228-
int kind = PyUnicode_KIND(name);
4229-
/* Special names contain at least "__x__" and are always ASCII. */
4230-
if (length > 4 && kind == PyUnicode_1BYTE_KIND) {
4231-
const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name);
4232-
return (
4233-
((characters[length-2] == '_') && (characters[length-1] == '_')) &&
4234-
((characters[0] == '_') && (characters[1] == '_'))
4235-
);
4236-
}
4237-
return 0;
4238-
}
4239-
42404220
/* This is similar to PyObject_GenericGetAttr(),
42414221
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
42424222
static PyObject *

Python/specialize.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ print_object_stats(FILE *out, ObjectStats *stats)
189189
fprintf(out, "Object materialize dict (str subclass): %" PRIu64 "\n", stats->dict_materialized_str_subclass);
190190
fprintf(out, "Object type cache hits: %" PRIu64 "\n", stats->type_cache_hits);
191191
fprintf(out, "Object type cache misses: %" PRIu64 "\n", stats->type_cache_misses);
192+
fprintf(out, "Object type cache collisions: %" PRIu64 "\n", stats->type_cache_collisions);
193+
fprintf(out, "Object type cache dunder hits: %" PRIu64 "\n", stats->type_cache_dunder_hits);
194+
fprintf(out, "Object type cache dunder misses: %" PRIu64 "\n", stats->type_cache_dunder_misses);
192195
}
193196

194197
static void

0 commit comments

Comments
 (0)