Question 1:The simple i=i*2 seems to have a complex translation. Can't it just be i=i*2 in java script and handle the error when it happens?
Question 2:The whole idea of using dictionaries to store functions seems inefficient for access. Is there an alternative approach using sayprototype.bin() or other approaches to define and access these functions that people have tried?
On Sun, Oct 20, 2013 at 10:24 PM, Sarvi Shanmugham <sarv...@gmail.com> wrote:Question 1:The simple i=i*2 seems to have a complex translation. Can't it just be i=i*2 in java script and handle the error when it happens?Python allows you to overload the multiplication operator. We have to check at runtime if the objects being multiplied have overloaded multiplication.Also, native strings in Python overload the multiplication operator, that's why you can do:print '='*10
Question 2:The whole idea of using dictionaries to store functions seems inefficient for access. Is there an alternative approach using sayprototype.bin() or other approaches to define and access these functions that people have tried?In javascript objects/dictionaries are one and the same, so one can't be more efficient than the other.>> a = {}Object {}>> a.foo = 33>> a['foo']3
If you want to hack on pyjs internals you're definitely going to have to learn JavaScript.
- lex
I get this.But considering Javascript is JITed and highly optimized including unrolling static loops and function calls if I understand right,wouldn't the followingi=p.op_mul(i,2)be faster than the currenti = (typeof ($mul1=i)==typeof ($mul2=2) && typeof $mul1=='number'?$mul1*$mul2:$p['op_mul']($mul1,$mul2));as long as op_mul() can handle numbers as well?And much more readable too?
Yeah, thats sorta why I asked the question. Just a beginnner but this how I understood java script objects and classes
and I thought this entire generated code segmented would have a been much more readable as objects instead of dictionary objects.
And whats with the $ notation. From what I am reading this is just jQuery convention and not required.
I presume we do pyjamas because we love python and hence readable code.Yet the generated Javascript seems less so, though from what I understand there more readable alternatives.So was just trying to understand what the thinking was.
Sarvi:PS: Has this blog post been discussed on the list. Did any changes/action items come out of it?
On Tue, 2013-10-22 at 20:29 -0700, Sarvi Shanmugham wrote:
> I tried to hack a bit to simplify the generated code to see how it can
> be done and this is what I got.
> I left the $ alone for the reasons Lex mentioned. Turned the
> dictionary accesses into object notation and
That looks cleaner, but inhibits the use of closure compiler, since that
will mangle object attributes (at high optimization).
> reduced some of the repetitive code into functions that do the same
> code.
> From what I tested of inlining, JITs should take care of inlining
> these with no performance impact.
> and it was relatively simple, repetitive but sizable changes to the
> translate_proto.py code to do this.
Did you do performance comparisons, on different engines, probably
(arguably) including IE8 for those that still use XP and refuse to
install something different? It used to matter a lot whether you use
function calls or inlined (<test>?<cond1>:<cond2>).
> /* start module: test1 */
> $pyjs.loaded_modules.test1 = function (__mod_name__) {
> if ($pymdecorate(this,'test1',__mod_name__)) return $m
>
>
>
> $m.hello = function(i) {
> return multiply(multiply(multiply(i,i),2),25);
> };
> $pyfdecorate($m.hello,'hello',0,[null,null,['i']]);
>
>
> return this;
> }; /* end test1 */
>
>
>
>
> /* end module: test1 */
> The result would from what I can see would be much simpler, readable
> and a lot less code
> Would something like this be accepted if I work on finishing this?
Depends... on the speed of the result. Since this is only meant to
generate simpler/cleaner/smaller code (which are for _me_ minor issues
compared to speed), this might not be way to go in _my_ opinion.
My guess (when I see this generated code) is that it will run about 5
times slower than what's currently generated (with --strict option). But
please, prove me wrong :-)
- Kees