Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
svaarala committed Jan 31, 2017
1 parent 9fddfde commit 2f76846
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src-input/duk_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
/* Slotcache is used to speeding up object property lookups without
* caching a property value.
*/
#define DUK_HEAP_SLOTCACHE_SIZE 8192
#define DUK_HEAP_SLOTCACHE_SIZE 256

/* helper to insert a (non-string) heap object into heap allocated list */
#define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap),(hdr))
Expand Down Expand Up @@ -533,8 +533,10 @@ DUK_INTERNAL void duk_heap_strtable_dump(duk_heap *heap);
DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h);
DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset);

/* FIXME: duk_small_uint_t? */
DUK_INTERNAL_DECL duk_uint32_t duk_heap_slotcache_lookup(duk_heap *heap, duk_hobject *obj, duk_hstring *key);
DUK_INTERNAL_DECL void duk_heap_slotcache_insert(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_uint32_t slot);
DUK_INTERNAL_DECL duk_uint32_t duk_heap_slotcache_getkey(duk_hobject *obj, duk_hstring *key);

#if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size);
Expand Down
28 changes: 23 additions & 5 deletions src-input/duk_heap_slotcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

#include "duk_internal.h"

/* FIXME: check if it's useful to compute the slotcache key only once in property code
* (live range is not trivial so maybe not).
*/

DUK_LOCAL DUK_ALWAYS_INLINE duk_uint32_t duk__slotcache_compute_key(duk_hobject *obj, duk_hstring *key) {
DUK_ASSERT(obj != NULL);
DUK_ASSERT(key != NULL);

return (((duk_uint32_t) obj) ^ DUK_HSTRING_GET_HASH(key)) & (DUK_HEAP_SLOTCACHE_SIZE - 1);
}

DUK_INTERNAL duk_uint32_t duk_heap_slotcache_getkey(duk_hobject *obj, duk_hstring *key) {
DUK_ASSERT(obj != NULL);
DUK_ASSERT(key != NULL);

return duk__slotcache_compute_key(obj, key);
}

DUK_INTERNAL duk_uint32_t duk_heap_slotcache_lookup(duk_heap *heap, duk_hobject *obj, duk_hstring *key) {
duk_uint32_t idx;
duk_slotcache_entry *ent;
Expand All @@ -14,8 +32,8 @@ DUK_INTERNAL duk_uint32_t duk_heap_slotcache_lookup(duk_heap *heap, duk_hobject

/* FIXME: assert: hashlimit <= 256 */

idx = ((duk_uint32_t) obj) ^ DUK_HSTRING_GET_HASH(key);
ent = heap->slotcache + (idx & (DUK_HEAP_SLOTCACHE_SIZE - 1));
idx = duk__slotcache_compute_key(obj, key);
ent = heap->slotcache + idx;
return ent->slot;
}

Expand All @@ -25,10 +43,10 @@ DUK_INTERNAL void duk_heap_slotcache_insert(duk_heap *heap, duk_hobject *obj, du

/* FIXME: assert: hashlimit <= 256 */
/* FIXME: skip check if hash part present and hashlimit correct */
if (DUK_UNLIKELY(slot >= DUK_UINT16_MAX)) {
if (DUK_UNLIKELY(slot >= DUK_UINT8_MAX)) {
return;
}
idx = ((duk_uint32_t) obj) ^ DUK_HSTRING_GET_HASH(key);
ent = heap->slotcache + (idx & (DUK_HEAP_SLOTCACHE_SIZE - 1));
idx = duk__slotcache_compute_key(obj, key);
ent = heap->slotcache + idx;
ent->slot = (duk_uint16_t) slot;
}
8 changes: 6 additions & 2 deletions src-input/duk_hobject_props.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,9 @@ DUK_INTERNAL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *o
slot = duk_heap_slotcache_lookup(heap, obj, key);
if (slot < DUK_HOBJECT_GET_ENEXT(obj)) {
if (DUK_HOBJECT_E_GET_KEY(heap, obj, slot) == key) {
DUK_D(DUK_DPRINT("slot cache hit: %p %p -> %ld", (void *) obj, (void *) key, (long) slot));
DUK_D(DUK_DPRINT("slot cache hit: %p %p %!O -> %ld (slotcache lookup key %lu)",
(void *) obj, (void *) key, key, (long) slot,
(unsigned long) duk_heap_slotcache_getkey(obj, key)));
*e_idx = slot;
*h_idx = -1;
return;
Expand All @@ -1193,7 +1195,9 @@ DUK_INTERNAL void duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *o
n = DUK_HOBJECT_GET_ENEXT(obj);
for (i = 0; i < n; i++) {
if (h_keys_base[i] == key) {
DUK_D(DUK_DPRINT("slot cache insert: %p %p -> %ld", (void *) obj, (void *) key, (long) i));
DUK_D(DUK_DPRINT("slot cache insert: %p %p %!O -> %ld (slotcache lookup key %lu)",
(void *) obj, (void *) key, key, (long) i,
(unsigned long) duk_heap_slotcache_getkey(obj, key)));
duk_heap_slotcache_insert(heap, obj, key, i);
*e_idx = i;
*h_idx = -1;
Expand Down

0 comments on commit 2f76846

Please sign in to comment.