Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Move stats for the method cache into thePy_STAT machinery#100255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
markshannon merged 4 commits intopython:mainfromfaster-cpython:type-cache-stats
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletionsInclude/internal/pycore_typeobject.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,15 +36,9 @@ struct type_cache_entry {
};

#define MCACHE_SIZE_EXP 12
#define MCACHE_STATS 0

struct type_cache {
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
#if MCACHE_STATS
size_t hits;
size_t misses;
size_t collisions;
#endif
};

/* For now we hard-code this to a value for which we are confident
Expand Down
7 changes: 7 additions & 0 deletionsInclude/pystats.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -65,8 +65,15 @@ typedef struct _object_stats {
uint64_tdict_materialized_new_key;
uint64_tdict_materialized_too_big;
uint64_tdict_materialized_str_subclass;
uint64_ttype_cache_hits;
uint64_ttype_cache_misses;
uint64_ttype_cache_dunder_hits;
uint64_ttype_cache_dunder_misses;
uint64_ttype_cache_collisions;
}ObjectStats;

#

typedefstruct_stats {
OpcodeStatsopcode_stats[256];
CallStatscall_stats;
Expand Down
64 changes: 23 additions & 41 deletionsObjects/typeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -327,18 +327,6 @@ static unsigned int
_PyType_ClearCache(PyInterpreterState *interp)
{
struct type_cache *cache = &interp->types.type_cache;
#if MCACHE_STATS
size_t total = cache->hits + cache->collisions + cache->misses;
fprintf(stderr, "-- Method cache hits = %zd (%d%%)\n",
cache->hits, (int) (100.0 * cache->hits / total));
fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n",
cache->misses, (int) (100.0 * cache->misses / total));
fprintf(stderr, "-- Method cache collisions = %zd (%d%%)\n",
cache->collisions, (int) (100.0 * cache->collisions / total));
fprintf(stderr, "-- Method cache size = %zd KiB\n",
sizeof(cache->hashtable) / 1024);
#endif

// Set to None, rather than NULL, so _PyType_Lookup() can
// use Py_SETREF() rather than using slower Py_XSETREF().
type_cache_clear(cache, Py_None);
Expand DownExpand Up@@ -4147,6 +4135,24 @@ find_name_in_mro(PyTypeObject *type, PyObject *name, int *error)
return res;
}

/* Check if the "readied" PyUnicode name
is a double-underscore special name. */
static int
is_dunder_name(PyObject *name)
{
Py_ssize_t length = PyUnicode_GET_LENGTH(name);
int kind = PyUnicode_KIND(name);
/* Special names contain at least "__x__" and are always ASCII. */
if (length > 4 && kind == PyUnicode_1BYTE_KIND) {
const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name);
return (
((characters[length-2] == '_') && (characters[length-1] == '_')) &&
((characters[0] == '_') && (characters[1] == '_'))
);
}
return 0;
}

/* Internal API to look for a name through the MRO.
This returns a borrowed reference, and doesn't set an exception! */
PyObject *
Expand All@@ -4160,12 +4166,13 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
struct type_cache_entry *entry = &cache->hashtable[h];
if (entry->version == type->tp_version_tag &&
entry->name == name) {
#if MCACHE_STATS
cache->hits++;
#endif
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));
return entry->value;
}
OBJECT_STAT_INC_COND(type_cache_misses, !is_dunder_name(name));
OBJECT_STAT_INC_COND(type_cache_dunder_misses, is_dunder_name(name));
Comment on lines +4174 to +4175
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I assume there's no user of these stats who will care that the definition of a "miss" now includes collisions whereas before it excluded collisions? Obviously the same information is derivable either way.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The old stats were only available ifMCACHE_STATS was defined, which could only be done by editing the source or build. There is no configure option. So, I don't see how there can be a user.


/* We may end up clearing live exceptions below, so make sure it's ours. */
assert(!PyErr_Occurred());
Expand DownExpand Up@@ -4193,14 +4200,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
entry->version = type->tp_version_tag;
entry->value = res; /* borrowed */
assert(_PyASCIIObject_CAST(name)->hash != -1);
#if MCACHE_STATS
if (entry->name != Py_None && entry->name != name) {
cache->collisions++;
}
else {
cache->misses++;
}
#endif
OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
Py_SETREF(entry->name, Py_NewRef(name));
}
Expand All@@ -4217,24 +4217,6 @@ _PyType_LookupId(PyTypeObject *type, _Py_Identifier *name)
return _PyType_Lookup(type, oname);
}

/* Check if the "readied" PyUnicode name
is a double-underscore special name. */
static int
is_dunder_name(PyObject *name)
{
Py_ssize_t length = PyUnicode_GET_LENGTH(name);
int kind = PyUnicode_KIND(name);
/* Special names contain at least "__x__" and are always ASCII. */
if (length > 4 && kind == PyUnicode_1BYTE_KIND) {
const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name);
return (
((characters[length-2] == '_') && (characters[length-1] == '_')) &&
((characters[0] == '_') && (characters[1] == '_'))
);
}
return 0;
}

/* This is similar to PyObject_GenericGetAttr(),
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
static PyObject *
Expand Down
5 changes: 5 additions & 0 deletionsPython/specialize.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -187,6 +187,11 @@ print_object_stats(FILE *out, ObjectStats *stats)
fprintf(out, "Object materialize dict (new key): %" PRIu64 "\n", stats->dict_materialized_new_key);
fprintf(out, "Object materialize dict (too big): %" PRIu64 "\n", stats->dict_materialized_too_big);
fprintf(out, "Object materialize dict (str subclass): %" PRIu64 "\n", stats->dict_materialized_str_subclass);
fprintf(out, "Object method cache hits: %" PRIu64 "\n", stats->type_cache_hits);
fprintf(out, "Object method cache misses: %" PRIu64 "\n", stats->type_cache_misses);
fprintf(out, "Object method cache collisions: %" PRIu64 "\n", stats->type_cache_collisions);
fprintf(out, "Object method cache dunder hits: %" PRIu64 "\n", stats->type_cache_dunder_hits);
fprintf(out, "Object method cache dunder misses: %" PRIu64 "\n", stats->type_cache_dunder_misses);
}

static void
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp