IT技术研习社,专注互联网技术研究与分享,喜欢的朋友可以点击【关注】;把经验传递给有梦想的人;
对于性能和效率的追求一直是程序开发中永恒不变的宗旨,除了我们自己在 编码 过程中要充分考虑代码的性能和效率, 虚拟机 在编译阶段也会对代码进行优化。本文就从虚拟机层面来看看虚拟机对我们所编写的代码采用了哪些优化手段。
1. 早期优化(编译期优化)
Java 语言的「编译期」其实是一段「不确定」的操作过程。因为它可能是一个前端编译器(如 Javac )把 *.java 文件编译成 *.class 文件的过程;也可能是程序运行期的即时编译器(JIT 编译器,Just In Time Compiler)把 字节码 文件编译成机器码的过程;还可能是静态提前编译器(AOT 编译器,Ahead Of Time Compiler)直接把 *.java 文件编译成本地机器码的过程。
Javac 这类编译器对代码的运行效率几乎没有任何优化措施,虚拟机设计团队把对性能的优化都放到了后端的即时编译器中,这样可以让那些不是由 Javac 产生的 class 文件(如 Groovy、 Kotlin 等语言产生的 class 文件)也能享受到编译器优化带来的好处。但是 Javac 做了很多针对 Java 语言编码过程的优化措施来改善程序员的编码风格、提升编码效率。相当多新生的 Java 语法特性,都是靠编译器的「语法糖」来实现的,而不是依赖虚拟机的底层改进来支持。
Java 中即时编译器在运行期的优化过程对于程序运行来说更重要,而前端编译器在编译期的优化过程对于程序编码来说更加密切。
1.1 Javac 编译器
Javac 编译器的编译过程大致可分为 3 个步骤:
- 解析与填充符号表;
- 插入式注解处理器的注解处理;
- 分析与字节码生成。
这 3 个步骤之间的关系如下图所示:
解析与填充符号表
解析步骤包含了经典程序编译原理中的词法分析和语法分析两个过程;完成词法分析和语法分析之后,下一步就是填充符号表的过程。符号表是由一组符号地址和符号信息构成的表格。在语义分析中,符号表所登记的内容将用于语义检查和产生中间代码。在目标代码生成阶段,当对符号名进行地址分配时,符号表是地址分配的依据。
注解处理器
注解(Annotation)是在 JDK 1.5 中新增的,有了编译器注解处理的标准 API 后,我们的代码就可以干涉编译器的行为,比如在编译期生成 class 文件。
语义分析与字节码生成
语法分析之后,编译器获得了程序代码的抽象语法树表示,语法树能表示一个结构正确的 源程序 的抽象,但无法保证源程序是符合逻辑的。而语义分析的主要任务是对结构上正确的源程序进行上下文有关性质的审查,比如进行类型审查。
字节码生成是 Javac 编译过程的最后一个阶段,字节码生成阶段不仅仅是把前面各个步骤所生成的信息(语法树、符号表)转化成字节码写到磁盘中,编译器还进行了少量的代码添加和转换工作。如前面提到的 () 方法就是在这一阶段添加到语法树中的。
在字节码生成阶段,除了生成构造器以外,还有一些其它的代码替换工作用于优化程序的实现逻辑,如把字符串的加操作替换为 StringBiulder 或 StringBuffer。
完成了对语法树的遍历和调整之后,就会把填充了所需信息的符号表交给 com.sun.tools.javac.jvm.ClassWriter 类,由这个类的 writeClass() 方法输出字节码,最终生成字节码文件,到此为止整个编译过程就结束了。
1.2 Java 语法糖
Java 中提供了有很多语法糖来方便程序开发,虽然语法糖不会提供实质性的功能改进,但是它能提升开发效率、语法的严谨性、减少编码出错的机会。下面我们来了解下语法糖背后我们看不见的东西。
泛型 与类型擦除
泛型顾名思义就是类型泛化,本质是参数化类型的应用,也就是说操作的数据类型被指定为一个参数。这种参数可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口和泛型方法。
在 Java 语言还没有泛型的时候,只能通过 Object 是所有类型的父类和强制类型转换两个特点的配合来实现类型泛化。例如 HashMap 的 get() 方法返回的就是一个 Object 对象,那么只有程序员和运行期的虚拟机才知道这个 Object 到底是个什么类型的对象。在编译期间,编译器无法检查这个 Object 的强制类型转换是否成功,如果仅仅依赖程序员去保障这项操作的正确性,许多 ClassCastException 的风险就会转嫁到程序运行期。
Java 语言中泛型只在程序源码中存在,在编译后的字节码文件中,就已经替换为原来的原生类型,并且在相应的地方插入了强制类型转换的代码。因此对于运行期的 Java 语言来说, ArrayList 与 ArrayList 是同一个类型,所以泛型实际上是 Java 语言的一个语法糖,这种泛型的实现方法称为类型擦除。
自动装箱、拆箱与遍历循环
自动装箱、拆箱与遍历循环是 Java 语言中用得最多的语法糖。这块比较简单,我们直接看代码:
public class SyntaxSugars { public static void main(String[] args){ List<Integer> list = Arrays.asList(1,2,3,4,5); int sum = 0; for(int i : list){ sum += i; } System.out.println("sum = " + sum); } }
自动装箱、拆箱与遍历循环编译之后:
public class SyntaxSugars { public static void main(String[] args) { List list = Arrays.asList(new Integer[]{ Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4), Integer.valueOf(5) }); int sum = 0; for (Iterator iterable = list.iterator(); iterable.hasNext(); ) { int i = ((Integer) iterable.next()).intValue(); sum += i; } System.out.println("sum = " + sum); } }
第一段代码包含了泛型、自动装箱、自动拆箱、遍历循环和变长参数 5 种语法糖,第二段代码则展示了它们在编译后的变化。
条件编译
Java 语言中条件编译的实现也是一颗语法糖,根据布尔常量值的真假,编译器会把分支中不成立的代码块消除。
public static void main(String[] args) { if (true) { System.out.println("block 1"); } else { System.out.println("block 2"); } }
上述代码经过编译后 class 文件的反编译结果:
public static void main(String[] args) { System.out.println("block 1"); }
由于篇幅有限我们这期就讲到这里,下期讲晚期优化,敬请期待。
IT技术研习社,专注互联网技术研究与分享,喜欢的朋友可以点击【关注】;把经验传递给有梦想的人;
Perfectly spoken of course. .
best dissertation service help writing essays
Thanks a lot! Plenty of data.
what should i write my paper about papers writers
Nicely put, Kudos!
help essay writing essay writing service
Wow plenty of helpful material!
vegas online casino empire casino online
You expressed it very well!
order custom essay online [url=https://seoqmail.com/]custom research paper for sale[/url]
Good data. Thank you!
123 write my essay do my essay australia
Awesome write ups, Kudos!
who can write my book for me can you do my essay for me
You actually expressed that really well.
write my essay for me for cheap help me write a thesis statement for free
You actually expressed that very well!
narrative essay writer how do i cite a website in my essay
Thanks, Lots of tips.
i need an essay written for me i am a writer essay sample
Thanks a lot. Helpful information!
please do my essay for me write my essay online for cheap
Truly lots of awesome material!
write my paper for me cheap paper writer cheap
Valuable material. Appreciate it!
paper writing service college essay writers service
You mentioned this perfectly.
help with college essays college application essay help
With thanks, A lot of tips!
best resume writing service reddit best essay writer service
Truly a lot of terrific data.
thesis engineering thesis writing service
Seriously tons of valuable knowledge!
define thesis psychology thesis
Truly a lot of amazing material.
paper writer online lined writing paper
Wow a lot of wonderful advice.
essay writer write my essay paper
Nicely put. With thanks.
do my chemistry homework homework 123
Seriously tons of valuable tips!
write my essay today write my essay today
Thanks. Loads of information!
buy papers for college term paper writing services
Truly loads of beneficial info.
paper write write paper
With thanks. Quite a lot of posts!
last minute essay writing service writing a scholarship essay
You have made the point!
homework help pay someone to do my homework
Terrific content. Many thanks.
fast essay writing service essay writing app
Kudos! A lot of posts.
buy essay papers pay for essay
This is nicely said! !
dissertation assistance phd dissertation
Many thanks. I like this.
phd dissertation writing dissertation paper
Amazing lots of excellent tips!
best essay writing service reddit types of essay writing
Thank you, Awesome information.
online essay writer do my essay for me
You expressed this wonderfully!
pay to write paper order essay
Very good material. Regards.
buy an essay online buy college essays
Excellent advice. Thanks a lot!
pay for papers pay to write paper
Seriously plenty of excellent tips.
writing essays for money writing a good essay
Terrific content. Many thanks!
writing essays essay writter
Great content. Cheers!
hire someone to do my homework pay to do my homework
Awesome write ups. Thanks.
argumentative essay writing essay writing meme
Incredible all kinds of terrific facts!
dissertations writing dissertations
Seriously a lot of wonderful info!
will writing service essay writing practice
Thanks. Terrific stuff!
do my math homework cpm homework
Truly a good deal of awesome knowledge.
pay for research paper where to buy essays online
You actually suggested that exceptionally well!
admission essay service professional essay writing services
With thanks. I value this!
pay for paper paper writing service reviews
You actually revealed it exceptionally well.
paper writer services paper writer online
You explained it terrifically!
essay helper free cheap essay help
Superb write ups. Many thanks!
best college paper writing service best college paper writing service
Cheers! Useful information.
cheap essay writing service top essay writing services
Kudos! Terrific stuff!
essaytyper how to write a college essay
Kudos, I like it!
academic essay writing service legit essay writing services
You actually reported it fantastically.
online proposal research paper writer
Good knowledge. Appreciate it.
research paper writer services research paper writers
Tips effectively utilized..
writing a compare and contrast essay customer service essay
Appreciate it! Loads of facts!
writing proposal write my term paper
You said that well!
write my term paper buy term paper
Amazing information. Cheers!
best dissertation dissertation help services
You mentioned this terrifically.
write my essay for me write my essay for cheap
Info effectively taken!!
best essay writers write a essay for me
Kudos, Numerous postings.
write my paper for me write my research paper for me
Amazing content. Appreciate it!
pay to do my homework pay someone to do my homework
With thanks, Numerous material!
cheap assignment writing service professional essay writers
Thanks a lot! A good amount of content!
proquest dissertations dissertation help
Superb content. Thanks a lot.
paper help paper help
You actually said this well!
essay writers do my essay
Cheers. Lots of info!
custom paper online paper writing service
Thanks! I like it.
pay for research paper paper writing service
Kudos! Quite a lot of info.
best resume writing service 2019 top ten essay writing services
You suggested that fantastically.
best law essay writing service uk richardson writing service
You mentioned it fantastically!
paper writing service college essay writing service uk forum
Really many of helpful data.
term paper write my research paper
dissertation proposal phd thesis
custom essays service
Thank you! I value this.
do my homework for money can you do my homework
Whoa lots of amazing info.
do my math homework for me coursework
You suggested this wonderfully.
pay for an essay pay for papers
research paper writer services elements of a research proposal
finance dissertation
Great information. Cheers.
thesis sentence argumentative thesis statement
Appreciate it. Numerous info!
writing essays service college essay writing service
Lovely stuff. Many thanks!
do my math homework should i do my homework
do my chemistry homework do my homework for me
best custom essay writing service
Excellent data. Thanks!
dissertation paper dissertation meaning
Very good write ups. Kudos!
pay to write paper buy essays
pay someone to write paper pay to write paper
writing a proposal essay
You actually mentioned this well!
free writing assistant assignment help
You actually suggested that terrifically.
do my chemistry homework online coursework
Terrific write ups. Appreciate it.
write a essay for me writing an opinion essay
Useful posts. Thanks.
homework help cpm do my math homework for me
college paper writing service custom handwriting paper
what is a dissertation paper
Thanks a lot. A good amount of content!
top essay writing services writing a descriptive essay
dissertation help services dissertation writing services
who will write my essay
Excellent tips. Regards.
dissertation def writing help
You actually expressed this well.
dissertation proposal dissertation writing
Kudos. I like it.
essay writers online write essay
writing essays write my essays online
economics dissertations
Wonderful advice. Thanks a lot.
help essay essay writing service
Cheers! Numerous data.
cheap essay writing services best rated essay writing service
With thanks! A lot of forum posts!
writing an opinion essay essay writter
termpaper research paper help
how to make a dissertation
Thank you, Plenty of material!
homework help homework help cpm
Awesome postings. Thanks a lot.
paper writing ai essay writer
This is nicely put! .
pay for research paper pay for paper
paper writing service reddit best research paper writing service
writing movie titles in essays
You said this really well.
dissertation definition dissertation def
You actually suggested it fantastically.
pay for papers pay someone to write your paper
Wonderful content, Many thanks!
essay writter write paper for me
best paper writing services best essay writing service reddit
pay for essay cheap
Amazing information. Kudos!
pay for research paper pay to write essay
Many thanks! Valuable stuff!
buy essay writing online essay writting service
With thanks. An abundance of data.
essay writter write my essays online
essay writter paper writer services
academic essay writers
Many thanks! I appreciate this!
cpm homework help homework help cpm
Wonderful forum posts, Appreciate it.
professional essay writers automatic essay writer
Seriously lots of superb tips.
buy college paper paper writing service
do my homework for me xyz homework
graphic organizers for writing essays
Cheers, A good amount of material!
i need help writing an essay essay writing service blog
Whoa quite a lot of useful advice.
thesis statment thesis titles
Superb material. Appreciate it.
dissertation editing dissertation writers
dissertation paper writing a dissertation
how to write literary analysis essay
Fantastic postings, Thanks a lot.
dissertation abstracts dissertation service
You’ve made your stand pretty effectively.!
persuasive essay writer write my paper for me free
Thanks! An abundance of facts.
dissertation writing help dissertation definition
spanish essay writing service best essay writing
write essay service
Good forum posts. With thanks!
phd thesis dissertation writing services
Very good information. Thanks.
write a essay for me online essay writers
Whoa plenty of wonderful facts.
pay for papers pay for papers
dissertation editing dissertation editing
how to write essay about yourself
Whoa lots of great facts!
thesis statment research thesis
Appreciate it! Ample info.
how to write a college essay help with my essay