Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace JS_HasProperty + JS_GetProperty with JS_TryGetProperty #105

Open
bnoordhuis opened this issue Nov 21, 2023 · 2 comments
Open

Replace JS_HasProperty + JS_GetProperty with JS_TryGetProperty #105

bnoordhuis opened this issue Nov 21, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@bnoordhuis
Copy link
Contributor

Problem: the JS_HasProperty + JS_GetProperty pattern shows up quite a bit and it's fairly expensive when the property exists on the prototype chain because it walks the prototype chain twice. Even when the property is on the this object, it still does double work.

Solution: add a JS_TryGetProperty that combines both operations, a la JS_TryGetPropertyInt64.

(JS_TryGetPropertyInt64 could be optimized the same way as well, by the way.)

@bnoordhuis bnoordhuis added the enhancement New feature or request label Nov 21, 2023
@andrieshiemstra
Copy link
Contributor

Hi, I'm looking into this and I'm wondering if we couldn't just check if JS_GetProperty (JS_GetPropertyInternal2) returns JS_UNDEFINED as building a JS_TryGetProperty would basicly be a copy of of JS_GetPropertyInternal2 and return FALSE where JS_GetPropertyInternal2 returns JS_UNDEFINED

so e.g. JS_TryGetPropertyInt64
turn this

JSValue val = JS_UNDEFINED;
    JSAtom prop;
    int present;

    if (likely((uint64_t)idx <= JS_ATOM_MAX_INT)) {
        /* fast path */
        present = JS_HasProperty(ctx, obj, __JS_AtomFromUInt32(idx));
        if (present > 0) {
            val = JS_GetPropertyValue(ctx, obj, js_int32(idx));
            if (unlikely(JS_IsException(val)))
                present = -1;
        }
    } else {

into something like this

JSValue val = JS_UNDEFINED;
    JSAtom prop;
    int present;

    if (likely((uint64_t)idx <= JS_ATOM_MAX_INT)) {
        /* fast path */
        
       val = JS_GetPropertyValue(ctx, obj, js_int32(idx));
       if (JS_VALUE_GET_TAG(val) == JS_TAG_UNDEFINED) {
            present = -1;
       } else if (unlikely(JS_IsException(val))) {
              present = -1;
	} else {
		present = 1;
	}
			
    } else {

@bnoordhuis
Copy link
Contributor Author

check if JS_GetProperty (JS_GetPropertyInternal2) returns JS_UNDEFINED

Assuming I understand your suggestion correctly: JS_UNDEFINED is a legitimate return value (property with value undefined) so that wouldn't work. JS_UNINITIALIZED or some other sentinel that's never a valid property value perhaps could.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants