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
Don't automatically copy python objects into javascript #1167
Conversation
…oxy.shallowCopyToJavascript and deepCopyToJavascript apis
…ect not hiwire id
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.
Hi @hoodmane, thank you for setting up your PR again, and apologize for the seesaw.
The solution for the failing tests, as mentioned in #1152 is obviously simpler than I thought first.
I was thinking the right approach would be to merge this as-is and then preferably fix the ergonomics problems before the next release, but I could see adding more to this PR.
I'm okay with this approach, anyway, next time, better we'll test twice against current master.
Oh also the docs on type conversions and the changelog should be updated.
It would be nice if you can do that.
Presumably implementing toJSON on pyproxy would make the tests pass more painlessly.
And this as well. When all changes are done and maybe @rth is also okay with this, we can merge again.
Right, well if I add these other changes to this branch and it is decided that they should be merged separately I can always split them off into other PRs. I'm a little bit unsure how selenium handles serialization, the selenium docs are surprisingly vague on the topic. Presumably a little bit of experimentation will make it clear though. |
Okay so I don't really understand how selenium serializes stuff. It definitely doesn't do assert selenium.run_js(
"""
return {
toJSON : () => {
console.log("Running toJSON!");
return [1,2,3];
}
};
"""
) == dict(toJSON={}) Whereas if it was doing
Causes @dalcde You have any idea how this works? I guess maybe the right thing to do is to just avoid returning a |
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.
Hello @hoodmane, thank you for the update, do you think we can give it another try?
Yeah @phorward I'd appreciate it if you could merge this. (I just merged master so this time there definitely won't be any surprise conflicts!) |
In terms of naming those are a bit verbose, particularly if they are to be used often. How about deepCopyToJS or deepCopyJS (or some other shorter name)? |
Also it would be good to document PyProxy and its public methods somewhere in https://pyodide.readthedocs.io/en/latest/usage/api-reference.html as currently these methods only appear in the changelog and nowhere else in the documentation. |
I'm happy with any of these. If you have a preference I can open a PR. Maybe even |
I think I have a local branch with a bunch of docs updates for recent changes and the changes in my various open PRs, my thought was to batch all the docs updates into one PR. |
Let's continue the naming discussion in #1192
Sounds good. |
Hello @hoodmane, thank you for the magnificent APIs (deep/shallowCopyToJavaScript) that would eliminate the heavy cost of big array conversion from python to Javascript. I think the shallowCopy version is designed for memory address reference instead of bulks of content copy. But I find the performance not so good as I've imagined, which is displayed as the following: It takes about 1~7 seconds for shallowCopyToJavascript() to complete the memory address reference and maybe some necessary meta data copy I guess. However, it's not adequate for a realtime computation. Any suggestions for better conversion performance? |
Hi @daoxian. Thanks for reporting this bad behavior. I think it's preferable to open this sort of question as an issue rather than on the PR in general. The handling of memory buffers is currently not good, this is an issue we hope to fix at some point. I think the current behavior is to make a javascript My guess is it would do much better if you transposed the array to have shape Would you look at #1168? That issue is closely related to the poor performance here. If you have an opinion about that discussion it would be helpful. One thing that limits my ability to fix the Buffer handling code is that I don't really know what the use cases look like, so I really appreciate any input you have. Of course if you want to take a stab at improving the buffer conversion code, let me know and I can tell you what I think you'll need to know. |
Indeed, it's much faster after the image transposed to (3, 1080, 1920). |
Could you copy most recent post to #1202 and continue discussion there? |
This is #1152 again. This implements the main changes proposed in #900.
This PR adjusts
js2python
to wrap Pythonlist
anddict
objects inPyProxy
objects instead of copying them into Javascript. This has the advantage that it allows Javascript and Python to share a reference to a JavascriptArray
orobject
. Automatic copying is lossy and inconvenient, it's asymmetric (python2js proxies JavascriptArray
andobject
intoJsProxy
), and it causes difficulties in a wide variety circumstances. For instance, it makes it impossible in general to get therepr
of an object returned fromrunPython
, it requires special case code to handlepyodide.globals
(which is aPyProxy
ofdict
), etc etc.This adds new APIs
deepCopyToJavascript
andshallowCopyToJavascript
asPyProxy
methods, using these recovers the old behaviors.