Skip to content

Commit 6da2741

Browse files
use minsize as default length
1 parent 39f819b commit 6da2741

2 files changed

Lines changed: 15 additions & 25 deletions

File tree

Include/internal/pycore_typecache.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ struct type_cache_entry {
2222
// Per-type attribute lookup cache: speed up attribute and method lookups,
2323
// see _PyTypeCache_Lookup().
2424
struct type_cache {
25-
uint32_t mask; // mask for indexing into hashtable, i.e. size of hashtable is mask + 1
26-
uint32_t version_tag; // initialized from type->tp_version_tag
27-
uint32_t available; // number of available entries in hashtable
28-
uint32_t used; // number of used entries in hashtable
29-
struct type_cache_entry hashtable[]; // hashtable entries, the total size is always power of 2 and at least _Py_TYPECACHE_MINSIZE
25+
uint32_t mask; // mask for indexing into hashtable, i.e. size of hashtable is mask + 1
26+
uint32_t version_tag; // initialized from type->tp_version_tag
27+
uint32_t available; // number of available entries in hashtable
28+
uint32_t used; // number of used entries in hashtable
29+
struct type_cache_entry hashtable[_Py_TYPECACHE_MINSIZE]; // hashtable entries
3030
};
3131

3232
struct _PyTypeCacheLookupResult {

Python/typecache.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,16 @@
1515
#include "pycore_typeobject.h" // _PyStaticType_GetState()
1616

1717

18-
// This is a union because MSVC doesn't support flexible array member in
19-
// the middle of a struct and we use a char array to reserve space for the
20-
// actual hashtable entries of the empty cache.
21-
static union {
22-
struct type_cache cache;
23-
char storage[sizeof(struct type_cache)
24-
+ _Py_TYPECACHE_MINSIZE * sizeof(struct type_cache_entry)];
25-
} empty_cache_storage = {
26-
.cache = {
27-
.mask = _Py_TYPECACHE_MINSIZE - 1,
28-
.available = 0,
29-
.used = 0,
30-
.version_tag = 0,
31-
},
32-
};
3318
// The empty cache is statically allocated and shared across all the types,
3419
// when a type is modified, the cache of type is set to the empty cache
3520
// and when a cache entry is inserted to the empty cache, a new cache is
3621
// allocated for the type and the entry is inserted to the new cache.
37-
#define empty_cache (empty_cache_storage.cache)
22+
static struct type_cache empty_cache = {
23+
.mask = _Py_TYPECACHE_MINSIZE - 1,
24+
.version_tag = 0,
25+
.available = 0,
26+
.used = 0,
27+
};
3828

3929
static inline uint32_t
4030
cache_size(struct type_cache *cache)
@@ -45,7 +35,7 @@ cache_size(struct type_cache *cache)
4535
static inline size_t
4636
cache_nbytes(struct type_cache *cache)
4737
{
48-
return sizeof(struct type_cache)
38+
return offsetof(struct type_cache, hashtable)
4939
+ (size_t)cache_size(cache) * sizeof(struct type_cache_entry);
5040
}
5141

@@ -54,7 +44,7 @@ cache_allocate(uint32_t size)
5444
{
5545
// size must be a power of two
5646
assert((size & (size - 1)) == 0);
57-
size_t nbytes = sizeof(struct type_cache)
47+
size_t nbytes = offsetof(struct type_cache, hashtable)
5848
+ (size_t)size * sizeof(struct type_cache_entry);
5949
struct type_cache *cache = PyMem_Calloc(1, nbytes);
6050
if (cache == NULL) {
@@ -158,7 +148,7 @@ cache_resize(PyTypeObject *type, struct type_cache *cache)
158148
new_size = old_size * 2;
159149
}
160150
if (new_size > _Py_TYPECACHE_MAXSIZE) {
161-
// The cache is too big, don't resize and just return.
151+
// The new size is too big, don't resize and just return.
162152
return -1;
163153
}
164154
struct type_cache *new_cache = cache_allocate(new_size);
@@ -176,7 +166,7 @@ cache_resize(PyTypeObject *type, struct type_cache *cache)
176166
return 0;
177167
}
178168

179-
// Insert a new entry to the type cache. If the cache is full, resize it before inserting the new entry.
169+
// Insert a new entry to the type cache.
180170
// The TYPE_LOCK should be held while calling this function.
181171
void
182172
_PyTypeCache_Insert(PyTypeObject *type, PyObject *name, PyObject *value)

0 commit comments

Comments
 (0)