Skip to content

Commit

Permalink
Move commonly used sequences of function calls into functions (eg jsv…
Browse files Browse the repository at this point in the history
…RemoveChild,jsvUnLock -> jsvRemoveChildAndUnLock) - saves ~200b
  • Loading branch information
gfwilliams committed Feb 2, 2024
1 parent 9b748d1 commit 1341bce
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 104 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Fix g.wrapString lockup if wrap width is less than the character width
Fix potential for crash after ReferenceError during function declaration (fix #2457)
STM32: (Original/Pico/WiFi) if USB connected but PC not receiving data, throw away USB data rather than blocking (fix #2446)
Move commonly used sequences of function calls into functions (eg jsvRemoveChild,jsvUnLock -> jsvRemoveChildAndUnLock) - saves ~200b

2v20 : Ensure String.charCodeAt returns NaN for out of bounds chars
Bangle.js2: When rendering overlays, *do not* use the current FG/BG color for 1 bit overlays
Expand Down
3 changes: 1 addition & 2 deletions libs/bluetooth/jswrap_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,8 +1747,7 @@ void jswrap_ble_updateServices(JsVar *data) {
#endif
}
}
jsvUnLock(charValue);
jsvUnLock(charVar);
jsvUnLock2(charValue, charVar);
} else {
JsVar *str = bleUUIDToStr(char_uuid);
jsExceptionHere(JSET_ERROR, "Unable to find service with UUID %v", str);
Expand Down
6 changes: 2 additions & 4 deletions libs/filesystem/jswrap_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,8 @@ void jswrap_file_close(JsVar* parent) {
JsVar *arr = fsGetArray(false);
if (arr) {
JsVar *idx = jsvGetIndexOf(arr, file.fileVar, true);
if (idx) {
jsvRemoveChild(arr, idx);
jsvUnLock(idx);
}
if (idx)
jsvRemoveChildAndUnLock(arr, idx);
jsvUnLock(arr);
}
}
Expand Down
7 changes: 3 additions & 4 deletions libs/graphics/jswrap_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,9 +2083,9 @@ JsVar *jswrap_graphics_getFonts(JsVar *parent) {
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
JsVar *arr = jsvNewEmptyArray();
if (!arr) return 0;
jsvArrayPushAndUnLock(arr, jsvNewFromString("4x6"));
jsvArrayPushString(arr, "4x6");
#ifdef USE_FONT_6X8
jsvArrayPushAndUnLock(arr, jsvNewFromString("6x8"));
jsvArrayPushString(arr, "6x8");
#endif
// vector font is added by below..
// scan for any functions 'setFont*' and add those names
Expand Down Expand Up @@ -2135,8 +2135,7 @@ static void _jswrap_graphics_getFontInfo(JsGraphics *gfx, JsGraphicsFontInfo *in
static void _jswrap_graphics_freeFontInfo(JsGraphicsFontInfo *info) {
#ifndef SAVE_ON_FLASH
if (info->font & JSGRAPHICS_FONTSIZE_CUSTOM_BIT) {
jsvUnLock(info->widths);
jsvUnLock(info->bitmap);
jsvUnLock2(info->widths, info->bitmap);
#ifdef ESPR_PBF_FONTS
if ((info->font & JSGRAPHICS_FONTSIZE_FONT_MASK) == JSGRAPHICS_FONTSIZE_CUSTOM_PBF)
jspbfFontFree(&info->pbfInfo);
Expand Down
7 changes: 2 additions & 5 deletions libs/network/esp8266/jswrap_esp8266_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ void jswrap_wifi_save(JsVar *what) {
//JsVar *arr = jsvNewArray(&o,1);
jswrap_storage_erase(name);
jswrap_storage_write(name,o,0,0);
//jsvUnLock3(arr,name,o);
jsvUnLock2(name,o);

DBGV("< Wifi.save: write completed\n");
Expand Down Expand Up @@ -1070,8 +1069,7 @@ static void dnsFoundCallback(
params[0] = networkGetAddressAsString((uint8_t *)&ipAddr->addr, 4, 10, '.');
}
jsiQueueEvents(NULL, g_jsHostByNameCallback, params, 1);
jsvUnLock(params[0]);
jsvUnLock(g_jsHostByNameCallback);
jsvUnLock2(params[0], g_jsHostByNameCallback);
g_jsHostByNameCallback = NULL;
}
DBGV("<< Wifi.getHostByName CB\n");
Expand Down Expand Up @@ -1547,8 +1545,7 @@ static void scanCB(void *arg, STATUS status) {
params[0] = jsAccessPointArray;
jsiQueueEvents(NULL, g_jsScanCallback, params, 1);

jsvUnLock(jsAccessPointArray);
jsvUnLock(g_jsScanCallback);
jsvUnLock2(jsAccessPointArray, g_jsScanCallback);
g_jsScanCallback = NULL;
DBGV("<< Wifi.scanCB\n");
}
Expand Down
6 changes: 2 additions & 4 deletions libs/network/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ void ssl_freeSocketData(int sckt) {
JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, false);
jsvUnLock(scktVar);
JsVar *sslData = jsvSkipName(sslDataVar);
jsvRemoveChild(ssl, sslDataVar);
jsvUnLock(sslDataVar);
jsvRemoveChildAndUnLock(ssl, sslDataVar);
jsvUnLock(ssl);
SSLSocketData *sd = 0;
if (jsvIsFlatString(sslData)) {
Expand Down Expand Up @@ -523,8 +522,7 @@ bool ssl_newSocketData(int sckt, JsVar *options) {
if (!ssl) return false; // out of memory?
JsVar *scktVar = jsvNewFromInteger(sckt);
JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, true);
jsvUnLock(scktVar);
jsvUnLock(ssl);
jsvUnLock2(scktVar, ssl);
if (!sslDataVar) {
return 0; // out of memory
}
Expand Down
9 changes: 3 additions & 6 deletions libs/network/socketserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ bool socketServerConnectionsIdle(JsNetwork *net) {
_socketConnectionKill(net, connection);
JsVar *connectionName = jsvObjectIteratorGetKey(&it);
jsvObjectIteratorNext(&it);
jsvRemoveChild(arr, connectionName);
jsvUnLock(connectionName);
jsvRemoveChildAndUnLock(arr, connectionName);
} else
jsvObjectIteratorNext(&it);
jsvUnLock2(connection, socket);
Expand Down Expand Up @@ -729,8 +728,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
_socketConnectionKill(net, connection);
JsVar *connectionName = jsvObjectIteratorGetKey(&it);
jsvObjectIteratorNext(&it);
jsvRemoveChild(arr, connectionName);
jsvUnLock(connectionName);
jsvRemoveChildAndUnLock(arr, connectionName);
socketClosed = true;

// fire error event, if there is an error
Expand Down Expand Up @@ -897,8 +895,7 @@ void serverClose(JsNetwork *net, JsVar *server) {
// remove from array
JsVar *idx = jsvGetIndexOf(arr, server, true);
if (idx) {
jsvRemoveChild(arr, idx);
jsvUnLock(idx);
jsvRemoveChildAndUnLock(arr, idx);
} else
jsWarn("Server not found!");
jsvUnLock(arr);
Expand Down
2 changes: 1 addition & 1 deletion libs/trigger/jswrap_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ JsVar* jswrap_trig_getErrorArray() {
if (errors & i) {
const char *s = trigGetErrorString(i);
if (s) {
jsvArrayPushAndUnLock(arr, jsvNewFromString(s));
jsvArrayPushString(arr);
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions scripts/find_common_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

var IGNORE_REGISTERS = true;
var MIN_OCCURANCES = 3;
var MIN_OCCURANCES = 2;
var MIN_SCORE = 20;


Expand All @@ -20,17 +20,17 @@ if (process.argv.length!=3) {
process.exit(1);
}

var lstFile = process.argv[2];
console.log("Loading "+lstFile);
var lst = require("fs").readFileSync(lstFile).toString().split("\n");


function isFunctionHeader(line) {
return line.match(/^\s*[0-9A-Fa-f]{8}\s*<.*:/);
}

function getAssembly(line) {
line = line.trim();
if (line.indexOf("\t")<0) line="";
if (line=="") return "";
if (isFunctionHeader(line)) return line; // function header
if (line.indexOf("\t")<0) return ""; // just code
line = line.substr(line.indexOf("\t")+1);
if (line.indexOf("\t")<0) line="";
if (line.indexOf("\t")<0) return ""; // hex dump of data
line = line.substr(line.indexOf("\t")+1);


Expand All @@ -40,6 +40,10 @@ function getAssembly(line) {
return line;
}

var lstFile = process.argv[2];
console.log("Loading "+lstFile);
var lst = require("fs").readFileSync(lstFile).toString().split("\n").map(getAssembly).filter(l=>l!="");

function scanLines(lst) {
var occurances = {};
var mapCodeToIdx = {};
Expand All @@ -48,8 +52,6 @@ function scanLines(lst) {
var history = [];
console.log("Scanning for use count");
lst.forEach(function(line) {
line = getAssembly(line);

if (line!="") {
var lineIdx = mapCodeToIdx[line];
if (lineIdx===undefined) {
Expand All @@ -62,9 +64,7 @@ function scanLines(lst) {
});
console.log("Scanning for history");
lst.forEach(function(line) {
line = getAssembly(line);

if (line=="") history = [];
if (isFunctionHeader(line)) history = [];
else {
var lineIdx = mapCodeToIdx[line];
if (lineUses[lineIdx]<MIN_OCCURANCES) {
Expand Down
6 changes: 2 additions & 4 deletions src/jsinteractive.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,7 @@ void jsiHistoryAddLine(JsVar *newLine) {
// if it was already in history, remove it - we'll put it back in front
JsVar *alreadyInHistory = jsvGetIndexOf(history, newLine, false/*not exact*/);
if (alreadyInHistory) {
jsvRemoveChild(history, alreadyInHistory);
jsvUnLock(alreadyInHistory);
jsvRemoveChildAndUnLock(history, alreadyInHistory);
}
// put it back in front
jsvArrayPush(history, newLine);
Expand Down Expand Up @@ -2194,8 +2193,7 @@ void jsiIdle() {
JsVar *watchArrayPtr = jsvLock(watchArray);
JsVar *watchNamePtr = jsvGetIndexOf(watchArrayPtr, watchPtr, true);
if (watchNamePtr) {
jsvRemoveChild(watchArrayPtr, watchNamePtr);
jsvUnLock(watchNamePtr);
jsvRemoveChildAndUnLock(watchArrayPtr, watchNamePtr);
}
jsvUnLock(watchArrayPtr);
Pin pin = jshGetPinFromVarAndUnLock(jsvObjectGetChildIfExists(watchPtr, "pin"));
Expand Down
3 changes: 1 addition & 2 deletions src/jsjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,7 @@ JsVar *jsjParseFunction() {
jsvUnLock(stackTrace);
}
}
jsvUnLock(exception);
jsvUnLock(v);
jsvUnLock2(exception, v);
return 0;
}

Expand Down
12 changes: 4 additions & 8 deletions src/jsparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,7 @@ JsVar *jspGetException() {
JsVar *exceptionName = jsvFindChildFromString(execInfo.hiddenRoot, JSPARSE_EXCEPTION_VAR);
if (exceptionName) {
JsVar *exception = jsvSkipName(exceptionName);
jsvRemoveChild(execInfo.hiddenRoot, exceptionName);
jsvUnLock(exceptionName);
jsvRemoveChildAndUnLock(execInfo.hiddenRoot, exceptionName);

JsVar *stack = jspGetStackTrace();
if (stack && jsvHasChildren(exception)) {
Expand All @@ -294,8 +293,7 @@ JsVar *jspGetStackTrace() {
JsVar *stackTraceName = jsvFindChildFromString(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR);
if (stackTraceName) {
JsVar *stackTrace = jsvSkipName(stackTraceName);
jsvRemoveChild(execInfo.hiddenRoot, stackTraceName);
jsvUnLock(stackTraceName);
jsvRemoveChildAndUnLock(execInfo.hiddenRoot, stackTraceName);
return stackTrace;
}
return 0;
Expand Down Expand Up @@ -878,8 +876,7 @@ NO_INLINE JsVar *jspeFunctionCall(JsVar *function, JsVar *functionName, JsVar *t
* functionRoot, so won't get freed. */
returnVar = jsvSkipName(returnVarName);
if (returnVarName) { // could have failed with out of memory
jsvRemoveChild(functionRoot, returnVarName); // remove return value (helps stops circular references, saves RAM)
jsvUnLock(returnVarName);
jsvRemoveChildAndUnLock(functionRoot, returnVarName); // remove return value (helps stops circular references, saves RAM)
}
}
// Store a stack trace if we had an error
Expand Down Expand Up @@ -934,8 +931,7 @@ NO_INLINE JsVar *jspeFunctionCall(JsVar *function, JsVar *functionName, JsVar *t
jsvUnLock(execInfo.scopesVar);
execInfo.scopesVar = oldScopeVar;
}
jsvUnLock(functionCode);
jsvUnLock(functionRoot);
jsvUnLock2(functionCode, functionRoot);
}

jsvUnLock(thisVar);
Expand Down
3 changes: 1 addition & 2 deletions src/jsserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ bool jsserialPopulateUSARTInfo(
}
}
}
jsvUnLock(parity);
jsvUnLock(flow);
jsvUnLock2(parity, flow);
return ok;
}

Expand Down
18 changes: 12 additions & 6 deletions src/jsvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -3124,15 +3124,18 @@ void jsvRemoveChild(JsVar *parent, JsVar *child) {
jsvSetNextSibling(child, 0);
if (wasChild)
jsvUnRef(child);
}

void jsvRemoveChildAndUnLock(JsVar *parent, JsVar *child) {
jsvRemoveChild(parent, child);
jsvUnLock(child);
}

void jsvRemoveAllChildren(JsVar *parent) {
assert(jsvHasChildren(parent));
while (jsvGetFirstChild(parent)) {
JsVar *v = jsvLock(jsvGetFirstChild(parent));
jsvRemoveChild(parent, v);
jsvUnLock(v);
jsvRemoveChildAndUnLock(parent, v);
}
}

Expand Down Expand Up @@ -3230,10 +3233,8 @@ void jsvObjectSetChildAndUnLock(JsVar *obj, const char *name, JsVar *child) {

void jsvObjectRemoveChild(JsVar *obj, const char *name) {
JsVar *child = jsvFindChildFromString(obj, name);
if (child) {
jsvRemoveChild(obj, child);
jsvUnLock(child);
}
if (child)
jsvRemoveChildAndUnLock(obj, child);
}

/** Set the named child of an object, and return the child (so you can choose to unlock it if you want).
Expand Down Expand Up @@ -3523,6 +3524,11 @@ JsVarInt jsvArrayPushAndUnLock(JsVar *arr, JsVar *value) {
return l;
}

/// Adds a new String element to the end of an array, and returns the new length. Same as jsvArrayPushAndUnLock(arr, jsvNewFromString(str))
JsVarInt jsvArrayPushString(JsVar *arr, const char *string) {
return jsvArrayPushAndUnLock(arr, jsvNewFromString(string));
}

// Push 2 integers onto the end of an array
void jsvArrayPush2Int(JsVar *arr, JsVarInt a, JsVarInt b) {
jsvArrayPushAndUnLock(arr, jsvNewFromInteger(a));
Expand Down
3 changes: 3 additions & 0 deletions src/jsvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ JsVar *jsvFindChildFromVar(JsVar *parent, JsVar *childName, bool addIfNotFound);

/// Remove a child - note that the child MUST ACTUALLY BE A CHILD! and should be a name, not a value.
void jsvRemoveChild(JsVar *parent, JsVar *child);
/// See jsvRemoveChild (this just unlocks child after)
void jsvRemoveChildAndUnLock(JsVar *parent, JsVar *child);
void jsvRemoveAllChildren(JsVar *parent);

/// Get the named child of an object. If createChild!=0 then create the child
Expand Down Expand Up @@ -721,6 +723,7 @@ JsVar *jsvGetIndexOf(JsVar *arr, JsVar *value, bool matchExact); ///< Get the in
JsVarInt jsvArrayAddToEnd(JsVar *arr, JsVar *value, JsVarInt initialValue); ///< Adds new elements to the end of an array, and returns the new length. initialValue is the item index when no items are currently in the array.
JsVarInt jsvArrayPush(JsVar *arr, JsVar *value); ///< Adds a new element to the end of an array, and returns the new length
JsVarInt jsvArrayPushAndUnLock(JsVar *arr, JsVar *value); ///< Adds a new element to the end of an array, unlocks it, and returns the new length
JsVarInt jsvArrayPushString(JsVar *arr, const char *string); ///< Adds a new String element to the end of an array, and returns the new length. Same as jsvArrayPushAndUnLock(arr, jsvNewFromString(str))
void jsvArrayPush2Int(JsVar *arr, JsVarInt a, JsVarInt b); ///< Push 2 integers onto the end of an array
void jsvArrayPushAll(JsVar *target, JsVar *source, bool checkDuplicates); ///< Append all values from the source array to the target array
JsVar *jsvArrayPop(JsVar *arr); ///< Removes the last element of an array, and returns that element (or 0 if empty). includes the NAME
Expand Down
3 changes: 1 addition & 2 deletions src/jsvariterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ void jsvObjectIteratorSetValue(JsvObjectIterator *it, JsVar *value) {
void jsvObjectIteratorRemoveAndGotoNext(JsvObjectIterator *it, JsVar *parent) {
if (it->var) {
JsVarRef next = jsvGetNextSibling(it->var);
jsvRemoveChild(parent, it->var);
jsvUnLock(it->var);
jsvRemoveChildAndUnLock(parent, it->var);
it->var = jsvLockSafe(next);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/jswrap_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ JsVar *jswrap_array_splice(JsVar *parent, JsVarInt index, JsVar *howManyVar, JsV
goToNext = false;
JsVar *toRemove = jsvObjectIteratorGetKey(&it);
jsvObjectIteratorNext(&it);
jsvRemoveChild(parent, toRemove);
jsvUnLock(toRemove);
jsvRemoveChildAndUnLock(parent, toRemove);
} else { // we're greater than the amount we need to remove now
needToAdd = true;
goToNext = false;
Expand Down
14 changes: 7 additions & 7 deletions src/jswrap_espruino.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,13 @@ void jswrap_espruino_kickWatchdog() {
JsVar *jswrap_espruino_getErrorFlagArray(JsErrorFlags flags) {
JsVar *arr = jsvNewEmptyArray();
if (!arr) return 0;
if (flags&JSERR_RX_FIFO_FULL) jsvArrayPushAndUnLock(arr, jsvNewFromString("FIFO_FULL"));
if (flags&JSERR_BUFFER_FULL) jsvArrayPushAndUnLock(arr, jsvNewFromString("BUFFER_FULL"));
if (flags&JSERR_CALLBACK) jsvArrayPushAndUnLock(arr, jsvNewFromString("CALLBACK"));
if (flags&JSERR_LOW_MEMORY) jsvArrayPushAndUnLock(arr, jsvNewFromString("LOW_MEMORY"));
if (flags&JSERR_MEMORY) jsvArrayPushAndUnLock(arr, jsvNewFromString("MEMORY"));
if (flags&JSERR_MEMORY_BUSY) jsvArrayPushAndUnLock(arr, jsvNewFromString("MEMORY_BUSY"));
if (flags&JSERR_UART_OVERFLOW) jsvArrayPushAndUnLock(arr, jsvNewFromString("UART_OVERFLOW"));
if (flags&JSERR_RX_FIFO_FULL) jsvArrayPushString(arr, "FIFO_FULL");
if (flags&JSERR_BUFFER_FULL) jsvArrayPushString(arr, "BUFFER_FULL");
if (flags&JSERR_CALLBACK) jsvArrayPushString(arr, "CALLBACK");
if (flags&JSERR_LOW_MEMORY) jsvArrayPushString(arr, "LOW_MEMORY");
if (flags&JSERR_MEMORY) jsvArrayPushString(arr, "MEMORY");
if (flags&JSERR_MEMORY_BUSY) jsvArrayPushString(arr, "MEMORY_BUSY");
if (flags&JSERR_UART_OVERFLOW) jsvArrayPushString(arr, "UART_OVERFLOW");

return arr;
}
Expand Down
Loading

0 comments on commit 1341bce

Please sign in to comment.