Skip to content

Commit

Permalink
jsvFindChildFromString enhancement - adds 10% performance on minified…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
gfwilliams committed Oct 6, 2023
1 parent 3132241 commit 00b0501
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Bangle.js: Change order of items in the Recovery Menu (hold BTN at boot) to put more common options first
Tidying up error messages (no trailing '.' or '\n'), making almost-similar error messages the same
Fix issue using `String.concat` with flash strings (fix #2268)
jsvFindChildFromString enhancement - adds 10% performance on minified code

2v19 : Fix Object.values/entries for numeric keys after 2v18 regression (fix #2375)
nRF52: for SD>5 use static buffers for advertising and scan response data (#2367)
Expand Down
53 changes: 34 additions & 19 deletions src/jsvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2927,39 +2927,54 @@ JsVar *jsvSetValueOfName(JsVar *name, JsVar *src) {
JsVar *jsvFindChildFromString(JsVar *parent, const char *name, bool addIfNotFound) {
/* Pull out first 4 bytes, and ensure that everything
* is 0 padded so that we can do a nice speedy check. */
char fastCheck[4];
char fastCheck[4] = {0,0,0,0};
#ifdef SAVE_ON_FLASH // if saving flash, don't include this optimisation
const bool superFastCheck = false;
#else
bool superFastCheck = true;
#endif
fastCheck[0] = name[0];
if (name[0]) {
fastCheck[1] = name[1];
if (name[1]) {
fastCheck[2] = name[2];
if (name[2]) {
fastCheck[3] = name[3];
} else {
fastCheck[3] = 0;
#ifndef SAVE_ON_FLASH
if (name[3])
superFastCheck = name[4]==0; // 4 or less chars
#endif
}
} else {
fastCheck[2] = 0;
fastCheck[3] = 0;
}
} else {
fastCheck[1] = 0;
fastCheck[2] = 0;
fastCheck[3] = 0;
}

assert(jsvHasChildren(parent));
JsVarRef childref = jsvGetFirstChild(parent);
while (childref) {
// Don't Lock here, just use GetAddressOf - to try and speed up the finding
// TODO: We can do this now, but when/if we move to cacheing vars, it'll break
JsVar *child = jsvGetAddressOf(childref);
if (*(int*)fastCheck==*(int*)child->varData.str && // speedy check of first 4 bytes
jsvIsStringEqual(child, name)) {
// found it! unlock parent but leave child locked
return jsvLockAgain(child);
if (!superFastCheck) { // more than 4 chars so we MUST use stringequal
while (childref) {
// Don't Lock here, just use GetAddressOf - to try and speed up the finding
JsVar *child = jsvGetAddressOf(childref);
if (*(int*)fastCheck==*(int*)child->varData.str && // speedy check of first 4 bytes
jsvIsStringEqual(child, name)) {
// found it! unlock parent but leave child locked
return jsvLockAgain(child);
}
childref = jsvGetNextSibling(child);
}
} else { // 4 or less chars, so if 4 chars match, there is no StringExt + length matches, then we're good without jsvIsStringEqual
int charsInName = 0;
while (name[charsInName])
charsInName++;
while (childref) {
JsVar *child = jsvGetAddressOf(childref);
if (*(int*)fastCheck==*(int*)child->varData.str &&
!child->varData.ref.lastChild &&
jsvGetCharactersInVar(child)==charsInName) { // no extra stringexts - so it really is that small
// found it! unlock parent but leave child locked
return jsvLockAgain(child);
}
childref = jsvGetNextSibling(child);
}
childref = jsvGetNextSibling(child);
}

JsVar *child = 0;
Expand Down
1 change: 1 addition & 0 deletions src/jsvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ bool jsvIsStringNumericStrict(const JsVar *var);

// TODO: maybe isName shouldn't include ArrayBufferName?
bool jsvHasCharacterData(const JsVar *v); ///< does the v->data union contain character data?
/// Does this variable use lastChild to point to a StringExt?
bool jsvHasStringExt(const JsVar *v);
/// Does this variable use firstChild/lastChild to point to multiple children
bool jsvHasChildren(const JsVar *v);
Expand Down

0 comments on commit 00b0501

Please sign in to comment.