AOT/JNI/Vala 的比较

转自:

http://en.wikipedia.org/wiki/AOT_compiler

https://wiki.gnome.org/Vala/FAQ#How_does_the_performance_of_Vala_applications_compare_to_other_applications.3F


An ahead-of-time (AOT) compiler is a compiler that implements ahead-of-time compilation. This refers to the act of compiling an intermediate language, such as Java bytecode, .NET Common Intermediate Language (CIL), or IBM System/38 or IBM System i "Technology Independent Machine Interface" code, into a system-dependent binary.

AOT(ahead-of-time 编译方式),就是将 像Java编译后得到的中间语言文件 变成和系统相关的 native binary。


Most languages with a managed code runtime that can be compiled to an intermediate language take advantage of just-in-time (JIT)[citation needed]. This, briefly, compiles intermediate code into machine codefor a native run while the intermediate code is executing, which may decrease an application's performance. Ahead-of-time compilation eliminates the need for this step by performing the compilation before execution rather than during execution.

JIT(just-in-time),相当于解释型语言,运行的时候,相当于是解释一条语句执行一条语句,即将一条中间的托管的语句翻译成一条机器语句,然后执行这条机器语句。执行效率较低。

相比JIT,AOT在运行之前就将中间托管代码翻译成了机器代码,不存在JIT的“执行期的翻译”步骤,所以执行效率比JIT高。


AOT compilation is mostly beneficial in cases where the interpreter (which is small) is too slow or JIT is too complex or introduces undesirable latencies.[citation needed] In most situations with fully AOT compiled programs and libraries it is possible to drop considerable fraction of runtime environment, thus saving disk space, memory and starting time. Because of this it can be useful in embedded or mobile devices.

AOT主要适用场合是,当解释器(中间语言翻译成机器语言)速度太慢,或JIT过于复杂,或JIT导致严重的滞后的情况。大多数情况下,启用了AOT以后的可执行程序,相比启用AOT之前,可以摒弃很多运行时环境的碎片(fraction of runtime environment),从而节省磁盘/内存空间,缩短启动时间。因此,AOT在嵌入式设备和移动设备中是很有用的。


AOT in most cases produces machine optimized code, just like a 'standard' native compiler. The difference is that AOT transforms the bytecode of an existing virtual machine into machine code. AOT compilers can perform complex and advanced code optimizations which in most cases of JITing will be considered much too costly. On the other hand AOT can't usually perform some optimizations possible in JIT, like runtime profile-guided optimizations, pseudo-constant propagation or indirect/virtual function inlining.

大多数情况下,AOT都可以将二进制代码针对硬件做优化,这时的AOT就很想一个真正的本地编译器,比如gcc。AOT和真正的本地编译器的不同之处在于,AOT是将现有的虚拟机的字节码转成本机二进制码,即操作对象和真正的编译器不同。AOT编译器可以执行高级的复杂的字节码的优化,这些优化对JIT来说,大多数都代价太高。另一方面,通常AOT不能执行一些JIT能做到的优化,比如 runtime profile-guided optimizations, pseudo-constant propagation or indirect/virtual function inlining


How does the performance of Vala applications compare to other applications?

The performance should be pretty similar to GObject/C-based code as there is no Vala-specific runtime library/environment that needs to be loaded. The C compiler can also apply the same optimizations on Vala-generated C code and plain GObject/C code. The Vala compiler uses reference counting in more places than most GObject/C-based applications do. However, Vala allows to fine-tune that easily in performance-critical sections with the weak modifier.

Vala application和其他语言的application的执行效率的比较

Vala application的执行效率类似 GObject/C-based 代码的执行效率,因为Vala没有自己额外的运行时库和运行时环境要加载。C编译器可以对用Vala生成的C代码或GObject/C代码做相同的优化。Vala编译器在大多数情况下比GObject/C-based applications使用更多的引用计数。但是,Vala允许对“性能敏感”的部分通过weak modifier做微调。

### 解决 NoClassDefFoundError: org/springframework/aot/AotDetector 的方法 `java.lang.NoClassDefFoundError: org/springframework/aot/AotDetector` 是一种常见的 Java 运行时错误,表明在程序执行期间无法找到 `org.springframework.aot.AotDetector` 类。此问题通常发生在使用 Spring Boot AOT(Ahead-of-Time)功能时。 #### 原因分析 该错误的主要原因是运行环境缺少必要的类文件或依赖项未正确配置。具体来说,在构建项目时可能引入了某些编译时依赖,但在运行时这些依赖并未被加载到 classpath 中[^1]。 --- #### 解决方案 ##### 1. 检查 Maven 或 Gradle 配置 确保项目的 `pom.xml` 文件中包含了正确的 Spring AOT 相关依赖。以下是推荐的依赖配置: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.x.x</version> <!-- 替换为实际使用的版本 --> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-aop-experimental</artifactId> <version>0.12.0</version> <!-- 版本号需匹配当前 Spring Boot 版本 --> </dependency> ``` 如果正在使用 Gradle,则应更新 `build.gradle` 如下所示: ```gradle implementation 'org.springframework.experimental:spring-aop-experimental:0.12.0' ``` 上述更改可以确保运行时所需的 AOT 功能支持已正确定义并下载至本地仓库[^2]。 --- ##### 2. 启用 AOT 处理器 为了使 AOT 支持生效,还需要启用相应的处理器插件。对于 Maven 用户,可以在 `pom.xml` 中添加如下插件定义: ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <builder>paketobuildpacks/builder-jammy-base:latest</builder> </image> </configuration> <executions> <execution> <goals> <goal>aot-process</goal> </goals> </execution> </executions> </plugin> ``` 而对于 Gradle 用户,应在 `build.gradle` 中加入以下内容: ```gradle plugins { id 'org.springframework.boot' version '3.x.x' apply false } tasks.named('bootBuildImage') { builder = "paketobuildpacks/builder-jammy-base" } ``` 通过以上设置,AOT 工具链将在构建阶段处理应用程序逻辑,并生成优化后的字节码以减少启动时间。 --- ##### 3. 清理旧版缓存与重新部署 即使调整了依赖关系插件配置,仍可能存在残留数据干扰正常工作流程。因此建议清理所有临时目录后再尝试重新打包应用: - **Maven**: 执行命令 `mvn clean install` - **Gradle**: 使用指令 `./gradlew clean build` 完成之后再次测试是否仍然存在相同的异常情况。 --- ##### 4. 调整 JVM 参数 (可选) 部分场景下可能是由于 JVM 加载机制不兼容所引起的问题。此时可以通过显式指定引导类路径来规避潜在冲突风险: ```bash java -cp your-application.jar \ --add-modules java.base \ com.example.YourApplicationMainClass ``` 注意替换占位符为你自己的实际情况即可。 --- ### 总结 通过对 POM/Gradle 文件进行适当修改以及激活对应的 AOT 插件工具集,能够有效缓解此类问题的发生频率;同时保持良好的开发习惯比如定期清除无用资源也有助于提升整体稳定性表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值