Skip to content

Commit c39d307

Browse files
committed
Add DUK_HOBJECT_FLAG_FASTREFS for DECREF/marking
When the flag is set, there is either no subclass C struct for the duk_hobject, or there is a subclass C struct but there are no references needing DECREF/marking in the struct. This allows DECREF and mark-and-sweep to handle duk_hobjects with less overhead for the common cases of plain objects and arrays (and some other less commonly collected structs like duk_hnatfunc). Also change Duktape.Thread.prototype internal class from Thread to Object: with the other changes internal code now assumes that if an object's class is Thread, it has the duk_hthread memory layout which wouldn't be the case for Duktape.Thread.prototype.
1 parent 4380b9a commit c39d307

20 files changed

Lines changed: 97 additions & 50 deletions

src-input/builtins.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,7 @@ objects:
27912791
present_if: DUK_USE_COROUTINE_SUPPORT
27922792

27932793
- id: bi_thread_prototype
2794-
class: Thread
2794+
class: Object
27952795
internal_prototype: bi_object_prototype
27962796
duktape: true
27972797
bidx: true

src-input/duk_api_bytecode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ static duk_uint8_t *duk__load_func(duk_context *ctx, duk_uint8_t *p, duk_uint8_t
459459
DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(&h_fun->obj));
460460
DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(&h_fun->obj));
461461
DUK_ASSERT(!DUK_HOBJECT_HAS_NATFUNC(&h_fun->obj));
462-
DUK_ASSERT(!DUK_HOBJECT_HAS_THREAD(&h_fun->obj));
462+
DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(&h_fun->obj));
463463
DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&h_fun->obj));
464464
DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&h_fun->obj));
465465
DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&h_fun->obj));

src-input/duk_api_stack.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,7 @@ DUK_LOCAL void duk__push_func_from_lightfunc(duk_context *ctx, duk_c_function fu
28522852

28532853
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
28542854
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
2855+
DUK_HOBJECT_FLAG_FASTREFS |
28552856
DUK_HOBJECT_FLAG_NATFUNC |
28562857
DUK_HOBJECT_FLAG_NEWENV |
28572858
DUK_HOBJECT_FLAG_STRICT |
@@ -2902,6 +2903,7 @@ DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t idx) {
29022903
}
29032904
case DUK_TAG_BOOLEAN: {
29042905
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
2906+
DUK_HOBJECT_FLAG_FASTREFS |
29052907
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN);
29062908
proto = DUK_BIDX_BOOLEAN_PROTOTYPE;
29072909
goto create_object;
@@ -2912,10 +2914,12 @@ DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t idx) {
29122914
DUK_ASSERT(h != NULL);
29132915
if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
29142916
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
2917+
DUK_HOBJECT_FLAG_FASTREFS |
29152918
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_SYMBOL);
29162919
proto = DUK_BIDX_SYMBOL_PROTOTYPE;
29172920
} else {
29182921
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
2922+
DUK_HOBJECT_FLAG_FASTREFS |
29192923
DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
29202924
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
29212925
proto = DUK_BIDX_STRING_PROTOTYPE;
@@ -2945,6 +2949,7 @@ DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t idx) {
29452949
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
29462950
case DUK_TAG_POINTER: {
29472951
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
2952+
DUK_HOBJECT_FLAG_FASTREFS |
29482953
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER);
29492954
proto = DUK_BIDX_POINTER_PROTOTYPE;
29502955
goto create_object;
@@ -2973,7 +2978,8 @@ DUK_EXTERNAL void duk_to_object(duk_context *ctx, duk_idx_t idx) {
29732978
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
29742979
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
29752980
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
2976-
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
2981+
DUK_HOBJECT_FLAG_FASTREFS |
2982+
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
29772983
proto = DUK_BIDX_NUMBER_PROTOTYPE;
29782984
goto create_object;
29792985
}
@@ -3376,10 +3382,15 @@ DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_context *ctx, duk_idx_t idx) {
33763382
}
33773383

33783384
DUK_EXTERNAL duk_bool_t duk_is_thread(duk_context *ctx, duk_idx_t idx) {
3385+
duk_hobject *obj;
3386+
33793387
DUK_ASSERT_CTX_VALID(ctx);
3380-
return duk__obj_flag_any_default_false(ctx,
3381-
idx,
3382-
DUK_HOBJECT_FLAG_THREAD);
3388+
3389+
obj = duk_get_hobject(ctx, idx);
3390+
if (obj) {
3391+
return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_THREAD ? 1 : 0);
3392+
}
3393+
return 0;
33833394
}
33843395

33853396
DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_context *ctx, duk_idx_t idx) {
@@ -4016,6 +4027,7 @@ DUK_EXTERNAL duk_idx_t duk_push_object(duk_context *ctx) {
40164027

40174028
(void) duk_push_object_helper(ctx,
40184029
DUK_HOBJECT_FLAG_EXTENSIBLE |
4030+
DUK_HOBJECT_FLAG_FASTREFS |
40194031
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
40204032
DUK_BIDX_OBJECT_PROTOTYPE);
40214033
return duk_get_top_index_unsafe(ctx);
@@ -4031,6 +4043,7 @@ DUK_EXTERNAL duk_idx_t duk_push_array(duk_context *ctx) {
40314043
DUK_ASSERT_CTX_VALID(ctx);
40324044

40334045
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
4046+
DUK_HOBJECT_FLAG_FASTREFS |
40344047
DUK_HOBJECT_FLAG_ARRAY_PART |
40354048
DUK_HOBJECT_FLAG_EXOTIC_ARRAY |
40364049
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY);
@@ -4094,7 +4107,6 @@ DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_context *ctx, duk_uint_t flags) {
40944107

40954108
obj = duk_hthread_alloc(thr,
40964109
DUK_HOBJECT_FLAG_EXTENSIBLE |
4097-
DUK_HOBJECT_FLAG_THREAD |
40984110
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
40994111
DUK_ASSERT(obj != NULL);
41004112
obj->state = DUK_HTHREAD_STATE_INACTIVE;
@@ -4229,6 +4241,7 @@ DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func
42294241

42304242
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
42314243
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
4244+
DUK_HOBJECT_FLAG_FASTREFS |
42324245
DUK_HOBJECT_FLAG_NATFUNC |
42334246
DUK_HOBJECT_FLAG_NEWENV |
42344247
DUK_HOBJECT_FLAG_STRICT |
@@ -4246,6 +4259,7 @@ DUK_INTERNAL void duk_push_c_function_noexotic(duk_context *ctx, duk_c_function
42464259

42474260
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
42484261
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
4262+
DUK_HOBJECT_FLAG_FASTREFS |
42494263
DUK_HOBJECT_FLAG_NATFUNC |
42504264
DUK_HOBJECT_FLAG_NEWENV |
42514265
DUK_HOBJECT_FLAG_STRICT |
@@ -4261,6 +4275,7 @@ DUK_INTERNAL void duk_push_c_function_noconstruct_noexotic(duk_context *ctx, duk
42614275
DUK_ASSERT_CTX_VALID(ctx);
42624276

42634277
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
4278+
DUK_HOBJECT_FLAG_FASTREFS |
42644279
DUK_HOBJECT_FLAG_NATFUNC |
42654280
DUK_HOBJECT_FLAG_NEWENV |
42664281
DUK_HOBJECT_FLAG_STRICT |
@@ -4462,6 +4477,7 @@ DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_context *ctx, duk_errcod
44624477
proto = duk_error_prototype_from_code(thr, err_code);
44634478
(void) duk_push_object_helper_proto(ctx,
44644479
DUK_HOBJECT_FLAG_EXTENSIBLE |
4480+
DUK_HOBJECT_FLAG_FASTREFS |
44654481
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR),
44664482
proto);
44674483

@@ -4712,6 +4728,7 @@ DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_context *ctx, void *ptr) {
47124728
DUK_EXTERNAL duk_idx_t duk_push_bare_object(duk_context *ctx) {
47134729
(void) duk_push_object_helper(ctx,
47144730
DUK_HOBJECT_FLAG_EXTENSIBLE |
4731+
DUK_HOBJECT_FLAG_FASTREFS |
47154732
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
47164733
-1); /* no prototype */
47174734
return duk_get_top_index_unsafe(ctx);

src-input/duk_bi_date.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_context *ctx) {
14211421

14221422
(void) duk_push_object_helper(ctx,
14231423
DUK_HOBJECT_FLAG_EXTENSIBLE |
1424+
DUK_HOBJECT_FLAG_FASTREFS |
14241425
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE),
14251426
DUK_BIDX_DATE_PROTOTYPE);
14261427

src-input/duk_bi_error.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx) {
1717

1818
/* same for both error and each subclass like TypeError */
1919
duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE |
20+
DUK_HOBJECT_FLAG_FASTREFS |
2021
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR);
2122

2223
DUK_UNREF(thr);

src-input/duk_bi_function.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx) {
332332
/* create bound function object */
333333
h_bound = duk_push_object_helper(ctx,
334334
DUK_HOBJECT_FLAG_EXTENSIBLE |
335+
DUK_HOBJECT_FLAG_FASTREFS |
335336
DUK_HOBJECT_FLAG_BOUNDFUNC |
336337
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
337338
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION),

src-input/duk_bi_object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_constructor(duk_context *ctx) {
5050

5151
(void) duk_push_object_helper(ctx,
5252
DUK_HOBJECT_FLAG_EXTENSIBLE |
53+
DUK_HOBJECT_FLAG_FASTREFS |
5354
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
5455
DUK_BIDX_OBJECT_PROTOTYPE);
5556
return 1;
@@ -112,6 +113,7 @@ DUK_INTERNAL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx) {
112113

113114
(void) duk_push_object_helper_proto(ctx,
114115
DUK_HOBJECT_FLAG_EXTENSIBLE |
116+
DUK_HOBJECT_FLAG_FASTREFS |
115117
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
116118
proto);
117119

src-input/duk_bi_pointer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ DUK_INTERNAL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx) {
2424
if (duk_is_constructor_call(ctx)) {
2525
(void) duk_push_object_helper(ctx,
2626
DUK_HOBJECT_FLAG_EXTENSIBLE |
27+
DUK_HOBJECT_FLAG_FASTREFS |
2728
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER),
2829
DUK_BIDX_POINTER_PROTOTYPE);
2930

src-input/duk_bi_proxy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx) {
123123
*/
124124
(void) duk_push_object_helper_proto(ctx,
125125
DUK_HOBJECT_FLAG_EXTENSIBLE |
126+
DUK_HOBJECT_FLAG_FASTREFS |
126127
DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ |
127128
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
128129
NULL);

src-input/duk_bi_string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ DUK_INTERNAL duk_ret_t duk_bi_string_constructor(duk_context *ctx) {
143143
if (duk_is_constructor_call(ctx)) {
144144
/* String object internal value is immutable */
145145
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
146+
DUK_HOBJECT_FLAG_FASTREFS |
146147
DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
147148
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
148149
duk_push_object_helper(ctx, flags, DUK_BIDX_STRING_PROTOTYPE);

0 commit comments

Comments
 (0)