Skip to content

Initial naive attempts at optimizations (WIP)#428

Closed
gaearon wants to merge 5 commits intobabel:masterfrom
gaearon:perf
Closed

Initial naive attempts at optimizations (WIP)#428
gaearon wants to merge 5 commits intobabel:masterfrom
gaearon:perf

Conversation

@gaearon
Copy link
Copy Markdown
Member

@gaearon gaearon commented Jan 9, 2015

This is kinda ugly but it's my first stab at optimizing traverse. I'm trying to get rid of closures there.
With these changes, I'm getting average 195ms over average 210ms on my mini benchmark.

Hope this gets some discussion started & maybe we can find more hot spots.

This is what V8 profiler gave me:

 [Bottom up (heavy) profile]:
  Note: percentage shows a share of a particular caller in the total
  amount of its parent calls.
  Callers occupying less than 2.0% are not shown.

   ticks parent  name
  31550   77.2%  /usr/lib/system/libsystem_kernel.dylib

   4808   11.8%  /usr/local/bin/node
   1234   25.7%    LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
   1220   98.9%      LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
   1220  100.0%        LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
   1212   99.3%          LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
   1212  100.0%            LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    518   10.8%    LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/transformation/transformer.js:60
    514   99.2%      LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    514  100.0%        LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    512   99.6%          LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    512  100.0%            LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    323    6.7%    LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:11
    316   97.8%      LazyCompile: *forOwn /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:2094
    264   83.5%        LazyCompile: ~forEach /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:3290
    264  100.0%          LazyCompile: *find /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:8
    264  100.0%            LazyCompile: *Node.needsParens /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:57
     52   16.5%        LazyCompile: *forEach /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:3290
     52  100.0%          LazyCompile: *find /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:8
     51   98.1%            LazyCompile: *Node.needsParens /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:57
      7    2.2%      LazyCompile: ~forOwn /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:2094
      7  100.0%        LazyCompile: ~forEach /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:3290
      6   85.7%          LazyCompile: *find /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:8
      6  100.0%            LazyCompile: *Node.needsParens /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:57
      1   14.3%          LazyCompile: ~find /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:8
      1  100.0%            LazyCompile: *Node.needsParens /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/node/index.js:57
    153    3.2%    LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    153  100.0%      LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    152   99.3%        LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    152  100.0%          LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    150   98.7%            LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    145    3.0%    LazyCompile: FILTER_KEY native runtime.js:398
    142   97.9%      LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    140   98.6%        LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    140  100.0%          LazyCompile: traverse /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:7
    138   98.6%            LazyCompile: *handle /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/traverse/index.js:34
    124    2.6%    LazyCompile: *Buffer.isLast /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:121
     80   64.5%      LazyCompile: *Buffer.push /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:97
     79   98.8%        LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
     45   57.0%          LazyCompile: *exports.Identifier /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/types.js:3
     45  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      5    6.3%          LazyCompile: *exports.ObjectExpression.exports.ObjectPattern /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/types.js:20
      5  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      5    6.3%          LazyCompile: *exports.CallExpression /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/expressions.js:58
      5  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      4    5.1%          LazyCompile: *exports.MemberExpression /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/expressions.js:119
      4  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3    3.8%          LazyCompile: ~print.after /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:196
      3  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3    3.8%          LazyCompile: *exports.BlockStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/base.js:9
      3  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3    3.8%          LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/statements.js:83
      3  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      2    2.5%          LazyCompile: ~exports.Literal /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/types.js:80
      2  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      2    2.5%          LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/types.js:62
      1   50.0%            LazyCompile: ~forEach /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:3290
      1   50.0%            LazyCompile: *forEach /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:3290
     22   17.7%      LazyCompile: *Buffer.removeLast /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:61
     20   90.9%        LazyCompile: *Buffer.newline /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:68
     18   90.0%          LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:81
     18  100.0%            LazyCompile: *times /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:6343
      1    5.0%          LazyCompile: ~<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:81
      1  100.0%            LazyCompile: ~times /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/node_modules/lodash/dist/lodash.js:6343
      1    5.0%          LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
      1  100.0%            LazyCompile: *exports.BlockStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/base.js:9
      2    9.1%        LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
      2  100.0%          LazyCompile: *exports.BlockStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/base.js:9
      2  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      8    6.5%      LazyCompile: *Buffer.space /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:55
      8  100.0%        LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
      4   50.0%          LazyCompile: *exports.ObjectExpression.exports.ObjectPattern /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/types.js:20
      4  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3   37.5%          LazyCompile: *exports.FunctionDeclaration.exports.FunctionExpression /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/methods.js:60
      3  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      1   12.5%          LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/statements.js:83
      1  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      8    6.5%      LazyCompile: *Buffer.semicolon /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:37
      8  100.0%        LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
      5   62.5%          LazyCompile: *exports.ExpressionStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/expressions.js:103
      5  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3   37.5%          LazyCompile: *<anonymous> /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/statements.js:83
      3  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      3    2.4%      LazyCompile: *Buffer.keyword /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/buffer.js:50
      3  100.0%        LazyCompile: *CodeGenerator.(anonymous function) /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:32
      2   66.7%          LazyCompile: *exports.IfStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/statements.js:12
      2  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114
      1   33.3%          LazyCompile: ~exports.SwitchStatement /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generators/statements.js:132
      1  100.0%            LazyCompile: *CodeGenerator.print /Users/dan/Documents/Projects/stampsy-alpha/node_modules/6to5/lib/6to5/generation/generator.js:114

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not cache array's length?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK it's not an issue with V8, although I may be wrong.

I'm mainly following this guide:
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but there are other javascript engines not only v8

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case is running 6to5 in Node via Webpack so that's what I'm optimizing for :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and as you can see here (at the end) bluebird also caches lengths

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon sounds legit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they're caching on purpose. They need to know length to build an array from arguments so it's silly not to use it in the loop if we have it anyway.

arguments is special so caching length there may be beneficial.

@gaearon
Copy link
Copy Markdown
Member Author

gaearon commented Jan 9, 2015

Superseded by #432 and #433.

@gaearon gaearon closed this Jan 9, 2015
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 9, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants