-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
use += instead of [].join for string concatenation #1977
Conversation
Hello is this really needed ? it would make the release of https://github.com/jadejs/then-jade I would highly prefer if we could wait until 2.0 for this since I hope that 2.0 will provide new mechanism to maintain then-jade more easily |
First of all - please ignore the failing tests, I just want to collect some feedback on pursuing this implementation How about we add a flag for jade-then to add additional calls to some function you can highjack? Couldn't you also monkeypatch into the jade_debug array? |
I simply reacted to the PR with a fear that it would make things more complicated for then-jade. I don't exactly know what impact it would have except that buf.push is heavily used. I know that @ForbesLindesay does not want then-jade to stop the evolution of jade so then-jade can't block performance optimization on this (I agree that nowadays += is faster than .join()) I haven't had time to dig into jade 2.0 yet so maybe there will be ways to override this kind of things with the new architecture. My goal was to do a last 1.x upgrade of then-jade when jade 2.0 is published and then move on to try and use 2.0 for then-jade. If this PR is merged into 1.* I suspect I won't be able to bind then-jade on the last 1.x release. I let you decide what is best. If you merge it, can you publish an interim release with the multi-line javascript block before ? |
Seeing that you're generally in favour of this, I'll keep playing around with it until the tests are green. We'll figure out how to release this once we get there - 2.0.0 might be released by then ;) |
This needs to wait for 2.0.0 as it's not that uncommon for people to manually inspect the buffer array as a way to do things like runtime filters via mixins. It is my expectation that then-jade will maintain its own completely separate copy of the compiler in 2.0.0, so it can do its own thing. |
One reason why Before: var buf = [];
buf.push('a');
buf.push(undefined);
buf.push('b');
buf.join('');
//< 'ab' After: var buf = '';
buf += 'a';
buf += undefined;
buf += 'b';
buf
//< 'aundefinedb' If you want to keep on using |
What is the memory impact on this? As far as I remember, one of the reasons |
@rlidwka I will run some extra tests on memory. @TimothyGu AFAIK, that is handled before, like this: text += "<p>" + (null == (jade_interp = thing.a) ? "" : jade_interp) + "</p><p>" + (null == (jade_interp = thing.b) ? "" : jade_interp) + "</p>"; This 'escaping' of |
@alubbe oh okay then. |
Fixed the failing tests. @rlidwka As far as I can tell, it uses less RAM than before. I re-ran my benchmark suite and saw less RAM and a 15-50% increase in output. /usr/bin/time -v node -e "for(var i = 0; i < 29999; i++){var a = [];for(var j = 0; j < 200; j++) a.push(Math.random().toString());a.join('')}"
/usr/bin/time -v node -e "for(var i = 0; i < 29999; i++){var a = '';for(var j = 0; j < 200; j++) a += Math.random().toString();}" @ForbesLindesay how do you want to proceed with this PR for now? |
@alubbe , I just remember a discussion about This bug was discussed: Quoting:
Also, in your benchmarks resulting string is not used anywhere. It can be freed by GC right away, and I won't be surprised if V8 will just throw away all that for loop because it is doing nothing. If we start to store output to an array, that's what happens:
I don't know about this matter any further, honestly. Just remembered that conversation and thought that it might be relevant here. |
Yes you are right, but after some point in time (I think it was 90 seconds after process launch), v8 starts small, short incremental GC cycles that collect these dangling strings. Meaning that past this point memory usage should be the same. |
Here is a new idea - if we know the length of the array,
or http://jsperf.com/optimized-array-join And within jade templates, we have this information! This should gives us the best of both worlds - faster compilation at lower RAM. |
Never mind, of course we don't know the length of the So we have to discuss whether the RAM impact have any implications on us. I have hit my local laptop with 6000 requests per second for 2 minutes, first against an unclustered express server running jade 1.10.0 and then against the same app using my branch of all optimizations. Here are the results:
The optimized branch also returned 10% more responses than jade 1.10.0, so we do see an improvement, even when we have all the networking and express overhead. Now that I understand the impact += has on memory and that it doesn't seem to affect us, my vote would be to use +=. |
I've also noticed that
is as fast as
but
is slower than
so I have now changed the implementation to actually use |
Moved over to jade-code-gen, closing this PR |
see #1975