Skip to content

Commit 11ef8f2

Browse files
jimmodpgeorge
authored andcommitted
py/map: Add an optional cache of (map+index) to speed up map lookups.
The existing inline bytecode caching optimisation, selected by MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, reserves an extra byte in the bytecode after certain opcodes, which at runtime stores a map index of the likely location of this field when looking up the qstr. This scheme is incompatible with bytecode-in-ROM, and doesn't work with native generated code. It also stores bytecode in .mpy files which is of a different format to when the feature is disabled, making generation of .mpy files more complex. This commit provides an alternative optimisation via an approach that adds a global cache for map offsets, then all mp_map_lookup operations use it. It's less precise than bytecode caching, but allows the cache to be independent and external to the bytecode that is executing. It also works for the native emitter and adds a similar performance boost on top of the gain already provided by the native emitter. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 7b89ad8 commit 11ef8f2

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎py/map.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@
4040
#define DEBUG_printf(...) (void)0
4141
#endif
4242

43+
#if MICROPY_OPT_MAP_LOOKUP_CACHE
44+
// MP_STATE_VM(map_lookup_cache) provides a cache of index to the last known
45+
// position of that index in any map. On a cache hit, this allows
46+
// short-circuiting the full linear search in the case of an ordered map
47+
// (i.e. all builtin modules and objects' locals dicts), and computation of
48+
// the hash (and potentially some linear probing) in the case of a regular
49+
// map. Note the same cache is shared across all maps.
50+
51+
// Gets the index into the cache for this index. Shift down by two to remove
52+
// mp_obj_t tag bits.
53+
#define MAP_CACHE_OFFSET(index) ((((uintptr_t)(index)) >> 2) % MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE)
54+
// Gets the map cache entry for the corresponding index.
55+
#define MAP_CACHE_ENTRY(index) (MP_STATE_VM(map_lookup_cache)[MAP_CACHE_OFFSET(index)])
56+
// Retrieve the mp_obj_t at the location suggested by the cache.
57+
#define MAP_CACHE_GET(map, index) (&(map)->table[MAP_CACHE_ENTRY(index) % (map)->alloc])
58+
// Update the cache for this index.
59+
#define MAP_CACHE_SET(index, pos) MAP_CACHE_ENTRY(index) = (pos) & 0xff;
60+
#else
61+
#define MAP_CACHE_SET(index, pos)
62+
#endif
63+
4364
// This table of sizes is used to control the growth of hash tables.
4465
// The first set of sizes are chosen so the allocation fits exactly in a
4566
// 4-word GC block, and it's not so important for these small values to be
@@ -136,6 +157,18 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
136157
// If the map is a fixed array then we must only be called for a lookup
137158
assert(!map->is_fixed || lookup_kind == MP_MAP_LOOKUP);
138159

160+
#if MICROPY_OPT_MAP_LOOKUP_CACHE
161+
// Try the cache for lookup or add-if-not-found.
162+
if (lookup_kind != MP_MAP_LOOKUP_REMOVE_IF_FOUND && map->alloc) {
163+
mp_map_elem_t *slot = MAP_CACHE_GET(map, index);
164+
// Note: Just comparing key for value equality will have false negatives, but
165+
// these will be handled by the regular path below.
166+
if (slot->key == index) {
167+
return slot;
168+
}
169+
}
170+
#endif
171+
139172
// Work out if we can compare just pointers
140173
bool compare_only_ptrs = map->all_keys_are_qstrs;
141174
if (compare_only_ptrs) {
@@ -172,6 +205,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
172205
elem->value = value;
173206
}
174207
#endif
208+
MAP_CACHE_SET(index, elem - map->table);
175209
return elem;
176210
}
177211
}
@@ -254,6 +288,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
254288
}
255289
// keep slot->value so that caller can access it if needed
256290
}
291+
MAP_CACHE_SET(index, pos);
257292
return slot;
258293
}
259294

‎py/mpconfig.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,20 @@
533533
#define MICROPY_OPT_LOAD_ATTR_FAST_PATH (0)
534534
#endif
535535

536+
// Use extra RAM to cache map lookups by remembering the likely location of
537+
// the index. Avoids the hash computation on unordered maps, and avoids the
538+
// linear search on ordered (especially in-ROM) maps. Can provide a +10-15%
539+
// performance improvement on benchmarks involving lots of attribute access
540+
// or dictionary lookup.
541+
#ifndef MICROPY_OPT_MAP_LOOKUP_CACHE
542+
#define MICROPY_OPT_MAP_LOOKUP_CACHE (0)
543+
#endif
544+
545+
// How much RAM (in bytes) to use for the map lookup cache.
546+
#ifndef MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE
547+
#define MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE (128)
548+
#endif
549+
536550
// Whether to use fast versions of bitwise operations (and, or, xor) when the
537551
// arguments are both positive. Increases Thumb2 code size by about 250 bytes.
538552
#ifndef MICROPY_OPT_MPZ_BITWISE

‎py/mpstate.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ typedef struct _mp_state_vm_t {
231231
// This is a global mutex used to make the VM/runtime thread-safe.
232232
mp_thread_mutex_t gil_mutex;
233233
#endif
234+
235+
#if MICROPY_OPT_MAP_LOOKUP_CACHE
236+
// See mp_map_lookup.
237+
uint8_t map_lookup_cache[MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE];
238+
#endif
234239
} mp_state_vm_t;
235240

236241
// This structure holds state that is specific to a given thread.

0 commit comments

Comments
 (0)