-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Implement a more efficient vecFromJSArray #11119
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, sorry I let this languish for so long
I think the approach of adding a new API rather than break the old one is a good one. And the name is fine as-is, the length of it makes it harder to misuse.
Also PRs with benchmarks is great to see :)
Nicely done!
OK I can't for the life of me figure out how to resolve the AUTHORS conflict in the web UI. Will take another pass on this later, after seeing how the tests go |
Same issue here, keeps telling me the file is out of date. I can resolve it manually if you want? It seems the failing tests are unrelated to my changes ? Seems like the |
Somehow pushing new commits to my fork branch is not updating the pull request... I think you might need to merge this one manually (or push to emscripten-core/emscripten/pr-11119-merge ? it's weird it exists). |
In general this looks good and useful, thanks @Lectem ! Before landing we should add a test for this. Also, #11158 is still open - @jgravelle-google , it might be good to have that fixed first so we are more confident about the safety of things like this? |
Looks like I screwed this up trying to resolve it, sorry :| @Lectem can you add a test for this? Looks like we currently have tests for |
Sorry I have been quite busy lately, I'll try to add some tests asap. |
@@ -570,19 +571,41 @@ int main() | |||
ensure_js("test_val_throw_(new TypeError('message'))"); | |||
|
|||
// this test should probably go elsewhere as it is not a member of val | |||
test("template<typename T> std::vector<T> vecFromJSArray(val v)"); | |||
test("template<typename T> std::vector<T> vecFromJSArray(const val& v)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the embind code or tests well, but it would be better to add new tests rather than modify existing ones - to make review easier, and to avoid the risk of losing some test coverage. Unless this is actually a necessary change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can split it if you want, I just thought that there was no need to go through EM_ASM
for almost the same setup code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see what @jgravelle-google thinks - maybe I don't know embind enough to tell if these test changes are obviously ok or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping @jgravelle-google to know if the changes are ok or not
The CI errors might be fixed by merging in latest upstream. |
I'm not sure if @jgravelle-google is available to review this. Perhaps @Brion or @chadaustin ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections here!
Co-authored-by: Chad Austin <[email protected]>
To save time I did a rebase and landed this in #12463, so we can close this. Thanks @Lectem and @chadaustin ! |
This change replaces the [`vecFromJSArray()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=vecfromjsarray#_CPPv4N10emscripten10emscripten3val14vecFromJSArrayERK3val) call in `vec_from_val()` with a call to [`convertJSArrayToNumberVector()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=convertjsarraytonumbervector#_CPPv4N10emscripten10emscripten3val28convertJSArrayToNumberVectorERK3val), which reduces the time consumption of Postject from ~30s to ~6s on a Mach-O Node.js binary when run on my x86_64 macOS. Fixes: nodejs#85 Refs: emscripten-core/emscripten#11119 Signed-off-by: Darshan Sen <[email protected]>
* feat: make postject faster This change replaces the [`vecFromJSArray()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=vecfromjsarray#_CPPv4N10emscripten10emscripten3val14vecFromJSArrayERK3val) call in `vec_from_val()` with a call to [`convertJSArrayToNumberVector()`](https://emscripten.org/docs/api_reference/val.h.html?highlight=convertjsarraytonumbervector#_CPPv4N10emscripten10emscripten3val28convertJSArrayToNumberVectorERK3val), which reduces the time consumption of Postject from ~30s to ~6s on a Mach-O Node.js binary when run on my x86_64 macOS. Fixes: #85 Refs: emscripten-core/emscripten#11119 Signed-off-by: Darshan Sen <[email protected]> * fix: increase timeout to pass tests on Windows Refs: https://app.circleci.com/pipelines/github/RaisinTen/postject/7/workflows/4bfbf11d-4796-459d-a339-a28098670f37/jobs/77/tests Signed-off-by: Darshan Sen <[email protected]> --------- Signed-off-by: Darshan Sen <[email protected]>
This is a followup of #5519 and #5655 since they were closed.
It is possible to implement
emscripten::vecFromJSArray
more efficiently for numeric arrays by using the optimized TypedArray.prototype.set function.The main issue with this method is that it will silently fail(or succeed) if elements of the array or not numbers, as it does not do any type checking but instead works as if it called the javascript
Number()
function for each element. (See ToNumber for more details)So instead of simply updating
vecFromJSArray
to use this new implementation and break code (since there's no typechecking anymore) I added a newconvertJSArrayToNumberVector
(name subject to change) and improved performance a tiny bit for vecFromJSArray by:val
parameter by const reference instead of copyI benchmarked locally with node.js (v12.16.3) and here are the timings I got:
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector
vecFromJSArray-Old
vecFromJSArray-New
convertJSArrayToNumberVector