Skip to content

improve keep_fargs & keep_fnames#1476

Closed
alexlamsl wants to merge 1 commit intomishoo:masterfrom
alexlamsl:unused-unreferenced
Closed

improve keep_fargs & keep_fnames#1476
alexlamsl wants to merge 1 commit intomishoo:masterfrom
alexlamsl:unused-unreferenced

Conversation

@alexlamsl
Copy link
Copy Markdown
Collaborator

AST_Symbol.unreferenced() is based on SymbolDef.references array which is populated during AST_Toplevel.figure_out_scope() before Compressor is used.

As a result, there are corner cases which !keep_fnames and !keep_fargs won't be able to give the best solution. As we already do a thorough usage scan in AST_Scope.drop_unused(), we might as well perform the optimisation steps over there instead.

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Feb 9, 2017

This PR looks reasonable.

Can you please make this additional change?

--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1413,7 +1413,7 @@ merge(Compressor.prototype, {
             && !self.uses_with
            ) {
             var in_use = [];
-            var in_use_ids = {}; // avoid expensive linear scans of in_use
+            var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
             var initializations = new Dictionary();
             // pass 1: find out which symbols are directly used in
             // this scope (not in nested scopes).

Lookups may be faster due to no proto chain, and It corrects a potential hasOwnProperty issue.

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

Would you like to move that same-line comment as well?

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

If we use Object.create(null) we can replace those id in in_use_ids with in_use_ids[id] which will provide speed-ups.

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

Side Notes:

  • dropping of function arguments is introduced in fcc0229
  • dropping of function names is from da407d4

@kzc
Copy link
Copy Markdown
Contributor

kzc commented Feb 9, 2017

Would you like to move that same-line comment as well?

The code's pretty dense there. I think the trailing comment is okay in that case.

If we use Object.create(null) we can replace those id in in_use_ids with in_use_ids[id] which will provide speed-ups.

I'm not sure it would be faster without some empirical evidence. It will likely be the same speed. There's a chance it could be slower since a value is being returned. Can we leave it as is since I know it works?

@alexlamsl
Copy link
Copy Markdown
Collaborator Author

test/benchmark.js checks out for 20cbcf3 👍

- utilise in_use_ids instead of unreferenced()
- drop_unused now up-to-date for subsequent passes
@alexlamsl alexlamsl force-pushed the unused-unreferenced branch from 20cbcf3 to cd167d1 Compare February 9, 2017 03:29
This was referenced Feb 9, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this pull request Feb 18, 2017
- utilise in_use_ids instead of unreferenced()
- drop_unused now up-to-date for subsequent passes

closes mishoo#1476
@alexlamsl alexlamsl closed this in b8b133d Feb 23, 2017
@alexlamsl alexlamsl deleted the unused-unreferenced branch February 24, 2017 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants