为V8优化JavaScript代码

Florian Loitsch
{"title":"为V8优化JavaScript代码","authors":"Florian Loitsch","doi":"10.1145/2661103.2661111","DOIUrl":null,"url":null,"abstract":"The performance of programs running in JavaScript engines is notoriously difficult to predict. Indeed, JavaScript is a complex language and, due to time-constraints and limited engineering resources, all popular virtual machines only optimize a subset of the language. Code that runs outside this (non obvious) sweet spot can pay huge performance penalties.\n JavaScript engines generally have at least two modes of operation: one non-optimized, and one optimized. Initially all functions are compiled to run in the non-optimized mode. Heuristics, like statistic profilers or invocation counters, then trigger the expensive recompilation of hot methods. Methods frequently see a performance improvement of an order of magnitude when they run in optimized mode. It is hence crucial that programs spend their time in optimized code.\n There are several ways compilers can do this:\n • avoid statements that cannot be optimized by the JIT. Indeed, V8 still cannot generate optimized code for all JavaScript constructs.\n • avoid bailouts. Optimized code is generated under the assumption that the generated code will run with similar dynamic types as seen before. If that assumption fails, the optimized code must be thrown away.\n • make code monomorphic. Optimized code is more efficient if it specializes for fewer dynamic types. Frequently it is possible to reduce the number of types by duplicating functions.\n Knowing when and where to apply these tips is almost impossible without proper tool-support. In this tutorial I will discuss the listed optimization techniques and present the tools that allow the investigation of V8 generated code. In particular, I will focus on V8s tracing flags, which report when methods are (de)optimized or inlined, Hydrogen traces, which represent V8s intermediate representation, and assembly dumps. During the talk I will concentrate on a Scheme-to-JavaScript compilation, but all talking-points will be of interest to any developer creating JavaScript code. http://floitsch.blogspot.com/2012/03/optimizing-for-v8-introduction.html","PeriodicalId":113092,"journal":{"name":"Scheme and Functional Programming","volume":"81 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2012-09-09","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"0","resultStr":"{\"title\":\"Optimizing JavaScript code for V8\",\"authors\":\"Florian Loitsch\",\"doi\":\"10.1145/2661103.2661111\",\"DOIUrl\":null,\"url\":null,\"abstract\":\"The performance of programs running in JavaScript engines is notoriously difficult to predict. Indeed, JavaScript is a complex language and, due to time-constraints and limited engineering resources, all popular virtual machines only optimize a subset of the language. Code that runs outside this (non obvious) sweet spot can pay huge performance penalties.\\n JavaScript engines generally have at least two modes of operation: one non-optimized, and one optimized. Initially all functions are compiled to run in the non-optimized mode. Heuristics, like statistic profilers or invocation counters, then trigger the expensive recompilation of hot methods. Methods frequently see a performance improvement of an order of magnitude when they run in optimized mode. It is hence crucial that programs spend their time in optimized code.\\n There are several ways compilers can do this:\\n • avoid statements that cannot be optimized by the JIT. Indeed, V8 still cannot generate optimized code for all JavaScript constructs.\\n • avoid bailouts. Optimized code is generated under the assumption that the generated code will run with similar dynamic types as seen before. If that assumption fails, the optimized code must be thrown away.\\n • make code monomorphic. Optimized code is more efficient if it specializes for fewer dynamic types. Frequently it is possible to reduce the number of types by duplicating functions.\\n Knowing when and where to apply these tips is almost impossible without proper tool-support. In this tutorial I will discuss the listed optimization techniques and present the tools that allow the investigation of V8 generated code. In particular, I will focus on V8s tracing flags, which report when methods are (de)optimized or inlined, Hydrogen traces, which represent V8s intermediate representation, and assembly dumps. During the talk I will concentrate on a Scheme-to-JavaScript compilation, but all talking-points will be of interest to any developer creating JavaScript code. http://floitsch.blogspot.com/2012/03/optimizing-for-v8-introduction.html\",\"PeriodicalId\":113092,\"journal\":{\"name\":\"Scheme and Functional Programming\",\"volume\":\"81 1\",\"pages\":\"0\"},\"PeriodicalIF\":0.0000,\"publicationDate\":\"2012-09-09\",\"publicationTypes\":\"Journal Article\",\"fieldsOfStudy\":null,\"isOpenAccess\":false,\"openAccessPdf\":\"\",\"citationCount\":\"0\",\"resultStr\":null,\"platform\":\"Semanticscholar\",\"paperid\":null,\"PeriodicalName\":\"Scheme and Functional Programming\",\"FirstCategoryId\":\"1085\",\"ListUrlMain\":\"https://doi.org/10.1145/2661103.2661111\",\"RegionNum\":0,\"RegionCategory\":null,\"ArticlePicture\":[],\"TitleCN\":null,\"AbstractTextCN\":null,\"PMCID\":null,\"EPubDate\":\"\",\"PubModel\":\"\",\"JCR\":\"\",\"JCRName\":\"\",\"Score\":null,\"Total\":0}","platform":"Semanticscholar","paperid":null,"PeriodicalName":"Scheme and Functional Programming","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/2661103.2661111","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 0

摘要

众所周知,在JavaScript引擎中运行的程序的性能很难预测。实际上,JavaScript是一种复杂的语言,由于时间限制和有限的工程资源,所有流行的虚拟机都只优化了语言的一个子集。在这个(不明显的)最佳点之外运行的代码可能会付出巨大的性能损失。JavaScript引擎通常至少有两种操作模式:一种是未优化的,另一种是优化的。最初,所有函数都被编译为在非优化模式下运行。然后,启发式方法(如统计分析器或调用计数器)会触发昂贵的热方法重新编译。方法在优化模式下运行时,通常会看到一个数量级的性能改进。因此,程序把时间花在优化代码上是至关重要的。编译器有几种方法可以做到这一点:•避免JIT无法优化的语句。事实上,V8仍然不能为所有JavaScript结构生成优化的代码。•避免救助。优化代码是在假设生成的代码将以与前面看到的类似的动态类型运行的情况下生成的。如果这个假设不成立,优化后的代码必须被丢弃。•使代码单态。优化后的代码如果专门用于更少的动态类型,则效率更高。通常可以通过复制函数来减少类型的数量。如果没有适当的工具支持,知道何时何地应用这些技巧几乎是不可能的。在本教程中,我将讨论列出的优化技术,并介绍允许研究V8生成的代码的工具。我将特别关注v8的跟踪标志(它报告方法何时被(反)优化或内联)、Hydrogen跟踪(它表示v8的中间表示)和程序集转储。在演讲中,我将集中讨论Scheme-to-JavaScript编译,但是所有的讨论点都会引起创建JavaScript代码的开发人员的兴趣。http://floitsch.blogspot.com/2012/03/optimizing-for-v8-introduction.html
本文章由计算机程序翻译,如有差异,请以英文原文为准。
Optimizing JavaScript code for V8
The performance of programs running in JavaScript engines is notoriously difficult to predict. Indeed, JavaScript is a complex language and, due to time-constraints and limited engineering resources, all popular virtual machines only optimize a subset of the language. Code that runs outside this (non obvious) sweet spot can pay huge performance penalties. JavaScript engines generally have at least two modes of operation: one non-optimized, and one optimized. Initially all functions are compiled to run in the non-optimized mode. Heuristics, like statistic profilers or invocation counters, then trigger the expensive recompilation of hot methods. Methods frequently see a performance improvement of an order of magnitude when they run in optimized mode. It is hence crucial that programs spend their time in optimized code. There are several ways compilers can do this: • avoid statements that cannot be optimized by the JIT. Indeed, V8 still cannot generate optimized code for all JavaScript constructs. • avoid bailouts. Optimized code is generated under the assumption that the generated code will run with similar dynamic types as seen before. If that assumption fails, the optimized code must be thrown away. • make code monomorphic. Optimized code is more efficient if it specializes for fewer dynamic types. Frequently it is possible to reduce the number of types by duplicating functions. Knowing when and where to apply these tips is almost impossible without proper tool-support. In this tutorial I will discuss the listed optimization techniques and present the tools that allow the investigation of V8 generated code. In particular, I will focus on V8s tracing flags, which report when methods are (de)optimized or inlined, Hydrogen traces, which represent V8s intermediate representation, and assembly dumps. During the talk I will concentrate on a Scheme-to-JavaScript compilation, but all talking-points will be of interest to any developer creating JavaScript code. http://floitsch.blogspot.com/2012/03/optimizing-for-v8-introduction.html
求助全文
通过发布文献求助,成功后即可免费获取论文全文。 去求助
来源期刊
自引率
0.00%
发文量
0
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
确定
请完成安全验证×
copy
已复制链接
快去分享给好友吧!
我知道了
右上角分享
点击右上角分享
0
联系我们:info@booksci.cn Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。 Copyright © 2023 布克学术 All rights reserved.
京ICP备2023020795号-1
ghs 京公网安备 11010802042870号
Book学术文献互助
Book学术文献互助群
群 号:481959085
Book学术官方微信