Merge lp:~sidnei/lazr-js/better-module-config-compression into lp:lazr-js
| Status: | Merged |
|---|---|
| Merged at revision: | not available |
| Proposed branch: | lp:~sidnei/lazr-js/better-module-config-compression |
| Merge into: | lp:lazr-js |
| Prerequisite: | lp:~sidnei/lazr-js/newer-buildout |
| Diff against target: |
301 lines (+128/-72) 2 files modified
src-py/lazr/js/meta.py (+59/-5) src-py/lazr/js/tests/test_meta.py (+69/-67) |
| To merge this branch: | bzr merge lp:~sidnei/lazr-js/better-module-config-compression |
| Related bugs: |
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| Māris Fogels (community) | 2010-03-16 | Approve on 2010-03-16 | |
| Graham Binns (community) | 2010-03-16 | Abstain on 2010-03-16 | |
|
Review via email:
|
|||
Description of the Change
- If a string literal is used more than once in requires/use/after,
declare a constant inside a closure so that minification can
compress the module config better.
| Māris Fogels (mars) wrote : | # |
Hi Sidnei,
This change's functionality looks good. I asked for a few more source code comments in key areas, and had one suggestion about the testing. With the addition source comments, I think you can land it.
I think the code for counting and generating the string literal optimizations is very close to needing a refactoring. If the optimizer was a collection of methods or functions then it would be a lot easier to test their behaviour in isolation. The testing appears to be based on the inputs and outputs of a large piece of sample data because all of the functionality is in a linear script right now, and this makes the tests very difficult to read (and therefore, difficult for someone else to debug). I do not think that this testing approach can be taken much farther beyond what you have now.
Maris
=== modified file 'src-py/
--- src-py/
+++ src-py/
@@ -101,17 +105,48 @@
if self.include_skin and entry.get(
- else:
- entry['skinnable'] = False
modules = {}
+ all_literals = []
for meta in metadata:
name = meta.pop("name")
-
- open(out, "w").write("var %s = %s;" %
- (var_name, simplejson.
+ all_literals.
+ all_literals.
+ all_literals.
+ all_literals.
+
+ # Only optimize string literals if they are used more than
+ # twice, since otherwise the optimization is pointless.
+ literals = [k for k, g in itertools.
+ if len(list(g)) > 1]
+ literals.
So I assume the loop is an easy way to count the literal occurances. Why do
you manually add the css and js literals? Please add a comment explaining
this section.
+
+ # For each string literal we are interested in, generate a
+ # variable declaration for the string literal, to improve
+ # minification.
+ variables_decl = "var %s" % ",\n ".join(
+ ["%s = \"%s\"" % (NAME_RE.sub("_", literal).upper(), literal)
+ for literal in sorted(literals)])
So this creates a string like "var FOO='foo'
It looks like you are correcting the literal names? Please add a comment
demonstrating what this code outputs.
+
+ def literal_sub(match):
+ if match.group(2) in literals:
+ return (match.group(1) +
+ NAME_RE.sub("_", match.group(
+ match.group(3))
+ return match.group(0)
I am not sure what this does. Why are you replacing underscores here? Was that
not done in the last code block? Please add a comment explaining this section.
+
+ modules_decl = LITERAL_RE.sub(l...
- 175. By Sidnei da Silva on 2010-03-16
-
Merged newer-buildout into better-
module- config- compression. - 176. By Sidnei da Silva on 2010-03-16
-
- Add some more comments as recommended by Maris, simplify part of the code.

I'm requesting Maris's review on this because I don't have the necessary knowledge to review it.