Repository navigation
JS_Free*: public vararg macros - #1535
Conversation
useful for dense code with visible towers of frees
why didn't I do that the first time
this also tends to pile up
|
Thoughts @bnoordhuis ? No strong opinion,I don't see myself using these. |
Just now I was editing |
|
You have a very fair point there! Have you measured any performance difference? |
|
I have not measured the performance but with the vararg stack pushing requirement it will indeed always be slower than a normal function, however the body of those functions is the simplest it can be. A vararg macro that applies another subtitution to each of its args is technically possible in C but that'd require me to pollute the qjs header with something like 16 or such linear macros or a few exponential macros... 🤷 |
|
You mean like this, right? #define JS_COUNT_ARGS_(_0, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N
#define JS_COUNT_ARGS(...) JS_COUNT_ARGS_(0, __VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define JS_FreeValueV1(ctx, a) do { JS_FreeValue(ctx, a); } while (0)
#define JS_FreeValueV2(ctx, a, b) do { JS_FreeValue(ctx, a); JS_FreeValue(ctx, b); } while (0)
#define JS_FreeValueV3(ctx, a, b, c) do { JS_FreeValue(ctx, a); JS_FreeValueV2(ctx, b, c); } while (0)
#define JS_FreeValueV4(ctx, a, b, c, d) do { JS_FreeValue(ctx, a); JS_FreeValueV3(ctx, b, c, d); } while (0)
#define JS_FreeValueV5(ctx, a, b, c, d, e) do { JS_FreeValue(ctx, a); JS_FreeValueV4(ctx, b, c, d, e); } while (0)
#define JS_FreeValueV6(ctx, a, b, c, d, e, f) do { JS_FreeValue(ctx, a); JS_FreeValueV5(ctx, b, c, d, e, f); } while (0)
#define JS_FreeValueV7(ctx, a, b, c, d, e, f, g) do { JS_FreeValue(ctx, a); JS_FreeValueV6(ctx, b, c, d, e, f, g); } while (0)
#define JS_FreeValueV8(ctx, a, b, c, d, e, f, g, h) do { JS_FreeValue(ctx, a); } JS_FreeValueV7(ctx, b, c, d, e, f, g, h); } while (0)
#define JS_FreeValueV__(ctx, N, ...) JS_FreeValueV##N(ctx, __VA_ARGS__)
#define JS_FreeValueV_(ctx, N, ...) JS_FreeValueV__(ctx, N, __VA_ARGS__)
#define JS_FreeValueV(ctx, ...) JS_FreeValueV_(ctx, JS_COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)Seems fine to me. The generated code is an ungainly mess of nested do/while statements but no one looks at that, and it's definitely more efficient than a variadic function. What would be interesting is measuring if it's better to expand to direct JS_FreeValue calls, or have JS_FreeValueV1, JS_FreeValueV2, etc. as public API functions that call JS_FreeValue in turn - i.e., more direct calls, or fewer indirect calls. I could see it going either way but I expect that, as JSValue is two words on 64 bits machines, the compiler spills to the stack more and more after JS_FreeValueV3 or JS_FreeValueV4, negating any performance benefits. Maybe you can mix and match: split up in pairs, call JS_FreeValueV2 for each pair, then mop up the remaining JSValue if there is one with JS_FreeValue. Lots of opportunity to measure, tweak and micro-optimize. :-) |
May I suggest an alternative that is more generic and produces more readable C code: I don't particularly like this kind of preprocessor abuse, but we cannot do much better with the C preprocessor. I must admit I did author some other ones in QuickJS. An extra advantage in using this macro is the order of evaluation of the arguments is fixed, left to right, as opposed to unspecified behavior for the variadic function. |
|
If we accept such macros it is true that they come with more benefits and I'd gladly replace the vararg funcs with them.
I particularly like this solution because it seems like a set of macros that can then be applied to a lot of other potential bulk operations. |
bnoordhuis
left a comment
There was a problem hiding this comment.
Can you undo the whitespace changes to quickjs.c and add regression tests to test-api.c?
Trailing blanks should be removed as a separate patch. |
shamelessly adapted from chqrlie's response
I've added a test to what I assume you meant api-test.c, and luckily I could simply amend my previous commit to ignore the whitespace change 👍 |
quickjs-ng 0.16.0 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## What's Changed * Add new project by @qr243vbi in quickjs-ng/quickjs#1529 * Disable standalone executable support under WASM / Emscripten by @saghul in quickjs-ng/quickjs#1528 * Only export API symbols for shared builds by @dannote in quickjs-ng/quickjs#1525 * Update CI configuration to use Visual Studio 18 by @saghul in quickjs-ng/quickjs#1533 * Update test262 by @saghul in quickjs-ng/quickjs#1539 * Fix -Wmaybe-uninitialized warning in js_binary_logic_slow by @sdrsdr in quickjs-ng/quickjs#1530 * Implement Error.prototype.stack accessor proposal by @saghul in quickjs-ng/quickjs#1536 * Enforce immutability on TypedArray write paths by @saghul in quickjs-ng/quickjs#1537 * Fix ArrayBuffer immutable-method semantics by @saghul in quickjs-ng/quickjs#1538 * Implement nonextensible-applies-to-private proposal by @saghul in quickjs-ng/quickjs#1540 * Bump actions/checkout from 6 to 7 by @dependabot[bot] in quickjs-ng/quickjs#1542 * Default to release builds in Meson by @saghul in quickjs-ng/quickjs#1544 * JS_Free*: public vararg macros by @vitawrap in quickjs-ng/quickjs#1535 * Build release binaries with static mimalloc by @saghul in quickjs-ng/quickjs#1545 * Add Unicode license to libunicode-table.h by @bnoordhuis in quickjs-ng/quickjs#1547 * Port Bellard's register-based regexp engine by @saghul in quickjs-ng/quickjs#1548 * Fix build with strict C compilers on x86-32 by @trufae in quickjs-ng/quickjs#1550 * Inline mixed int/float arithmetic and fast-array reads in the interpreter by @saghul in quickjs-ng/quickjs#1549 * Add arena allocator by @saghul in quickjs-ng/quickjs#1551 * Fix `set Iterator.prototype.constructor` for non-object values by @saghul in quickjs-ng/quickjs#1553 * Fix reference leak in `Iterator.prototype.filter` by @saghul in quickjs-ng/quickjs#1561 * Fix Iterator Helper close when the source iterator has no `return` by @saghul in quickjs-ng/quickjs#1558 * Set module internal promise rejection as handled when appropriate by @xim in quickjs-ng/quickjs#1554 * Add a GC-stress CI job by @saghul in quickjs-ng/quickjs#1568 * Fix memory leak in Set.prototype.union / symmetricDifference by @saghul in quickjs-ng/quickjs#1567 * Fix use-after-free in DisposableStack disposal by @saghul in quickjs-ng/quickjs#1566 * Fix type confusion in RegExp.escape with rope strings by @saghul in quickjs-ng/quickjs#1574 * Fix use-after-free from Array `.length` grow + `push` by @saghul in quickjs-ng/quickjs#1563 * Fix reference leak in Iterator.prototype.find by @saghul in quickjs-ng/quickjs#1573 * Fix BigInt.asUintN of negatives when width is limb-aligned by @littledivy in quickjs-ng/quickjs#1575 * Fix missing module exports for array destructuring by @littledivy in quickjs-ng/quickjs#1576 * Fix double free of a detached ArrayBuffer's backing store by @littledivy in quickjs-ng/quickjs#1578 * Fix for-await-of calling return() on normal completion by @littledivy in quickjs-ng/quickjs#1577 * Await async iterator return() when a for-await loop breaks by @littledivy in quickjs-ng/quickjs#1580 * Make Array.fromAsync elements enumerable by @littledivy in quickjs-ng/quickjs#1581 * Report the correct column number for syntax errors by @littledivy in quickjs-ng/quickjs#1582 * Implement CallSite.prototype.isConstructor() by @littledivy in quickjs-ng/quickjs#1584 * Fix crash on cyclic re-export of an imported binding by @littledivy in quickjs-ng/quickjs#1583 * Name the re-exported module when a re-export is missing by @littledivy in quickjs-ng/quickjs#1586 * Report the correct column for an undefined global reference by @littledivy in quickjs-ng/quickjs#1585 * docs: add RongJS to projects by @butterflyfish in quickjs-ng/quickjs#1589 * Limit named group scopes to 256 by @saghul in quickjs-ng/quickjs#1592 * Bump actions/setup-node from 6 to 7 by @dependabot[bot] in quickjs-ng/quickjs#1593 * Harden against hash collisions by @bnoordhuis in quickjs-ng/quickjs#1590 * Add man pages by @suve in quickjs-ng/quickjs#1595 * Return null from CallSite position getters for native frames by @littledivy in quickjs-ng/quickjs#1597 * Fix subarray species constructor arguments by @littledivy in quickjs-ng/quickjs#1600 * Continue the TypedArray lastIndexOf scan after a shrink by @littledivy in quickjs-ng/quickjs#1602 * Copy bytes in increasing order in TypedArray slice by @littledivy in quickjs-ng/quickjs#1601 * Key module identity by specifier and import attributes by @littledivy in quickjs-ng/quickjs#1603 * Add JS_PromiseMarkAsHandled by @littledivy in quickjs-ng/quickjs#1604 * Do not coerce the value on a TypedArray out-of-bounds write with a foreign receiver by @littledivy in quickjs-ng/quickjs#1599 * Add JS_PromiseThen by @littledivy in quickjs-ng/quickjs#1605 * Fix UAF of a suspended coroutine referenced only via a closure by @saghul in quickjs-ng/quickjs#1569 * Fix double free of a CallSite when the backtrace array insertion fails by @saghul in quickjs-ng/quickjs#1614 * Fix use-after-free when creating the promise resolving functions OOMs by @saghul in quickjs-ng/quickjs#1613 * Fix leak in JSON.stringify() when space is a rope by @saghul in quickjs-ng/quickjs#1612 * Fix integer overflow in the TypedArray sort index array allocation by @saghul in quickjs-ng/quickjs#1611 * Fix reference leak in Atomics.store() on an invalidated buffer by @saghul in quickjs-ng/quickjs#1610 * Fix integer overflow in the Proxy ownKeys() property enumeration by @saghul in quickjs-ng/quickjs#1609 * Fix integer multiplication result cast to unsigned long in js_realloc_array() by @Nidrax in quickjs-ng/quickjs#1608 * Drop js_realloc2 and its allocation-slack feedback by @saghul in quickjs-ng/quickjs#1616 * lre: Do not patch a jump offset that could not be emitted by @saghul in quickjs-ng/quickjs#1615 * Reject out-of-range limits in Iterator.prototype.take/drop by @saghul in quickjs-ng/quickjs#1618 * Implement the Iterator.prototype.join() proposal by @saghul in quickjs-ng/quickjs#1620 * Implement the iterator includes proposal by @saghul in quickjs-ng/quickjs#1619 * Support resizable externally managed ArrayBuffers by @saghul in quickjs-ng/quickjs#1594 * Implement the iterator chunking proposal by @saghul in quickjs-ng/quickjs#1621 * Iterator helper fixes by @saghul in quickjs-ng/quickjs#1622 * Iterator.contact fixes by @saghul in quickjs-ng/quickjs#1623 ## New Contributors * @dannote made their first contribution in quickjs-ng/quickjs#1525 * @sdrsdr made their first contribution in quickjs-ng/quickjs#1530 * @vitawrap made their first contribution in quickjs-ng/quickjs#1535 * @xim made their first contribution in quickjs-ng/quickjs#1554 * @butterflyfish made their first contribution in quickjs-ng/quickjs#1589 * @suve made their first contribution in quickjs-ng/quickjs#1595 * @Nidrax made their first contribution in quickjs-ng/quickjs#1608 **Full Changelog**: https://github.com/quickjs-ng/quickjs/compare/v0.15.1...v0.16.0</pre> <p>View the full release notes at <a href="https://github.com/quickjs-ng/quickjs/releases/tag/v0.16.0">https://github.com/quickjs-ng/quickjs/releases/tag/v0.16.0</a>.</p> </details> <hr> See merge request: Harmonybrew/homebrew-core!15602

This introduces 4 new public macros based on a set of generic vararg macros that could also be used for other convenience macros.
The functions to use directly are
JS_FreeValuesJS_FreeValuesRTJS_FreeAtomsJS_FreeAtomsRTnote the "s"
Example usage
JS_FreeValues(ctx, myval1, myval2, someotherval3);Why?
I am editing dense code with tight interop with QJS and i've noticed an annoying pattern of FreeValue/Atom accumulating, especially when dup'ing for calls when the JSValue layout isn't initially linear, or when going down object chains with atoms. I also feel like this could benefit more users than me.