How can I compile A independent JS-Hermes Binary Code compiler in android?
Hello,everyone~ According to the instructions of BuildingAndRunning and ReactNativeIntegration in version 0.2.1(because our react-native project base on 0.61.3, the newer heremes could not be use in this version), I get hermes-debug.aar。
Inside the aar, we can get libhermes.so, but I notice it is only a runtime-engine. In our projcet, the server push the js code to the client, so I want to compile the js code to hbc in runtime.
I check the hermes projcet , finnally I find hermesc and CompileJS.cpp.
-
Hermesc seems to be used for commandLine tools but not for android application (how can I get a libhermesc.so).
-
CompileJS seems include in libhermes.so but do not expose so I can not use it in my code.
Can somebody help me to solve these promble , I'd really appreciate it. :)
I try to change add_llvm_library(compileJS STATIC CompileJS.cpp) to add_llvm_library(compileJS SHARED CompileJS.cpp) in hermes/API/hermes/CMakeLists.txt, and change the build target from 'libhermes' to 'CompileJS' . But cmake show me a error:
Execution failed for task ':hermes:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /Users/hanyang.ou/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build hermes-compile/staging/hermes/cmake/debug/armeabi-v7a --target compileJS}
ninja: error: 'hermes-compile/llvm-armeabi-v7a/lib/libLLVMBinaryFormat.a', needed by 'hermes-compile/build_android/hermes/intermediates/cmake/debug/obj/armeabi-v7a/libcompileJS.so', missing and no known rule to make it
I only find this file 'libLLVMBinaryFormat.a' on llvm_build_release but not in llvm-armeabi-v7
The ability to compile JS is already present in the runtime library. You can execute JS source with facebook::jsi::Runtime::evaluateJavaScript(). Also take a look at prepareJavaScript(), and evaluatePreparedJavaScript().
Note that this is only recommended for small amounts of JavaScript.
@tmikov Wow, thank you for your answer!
I have been checked prepareJavaScript 、facebook::jsi::Runtime::evaluateJavaScript() and evaluatePreparedJavaScript fuction and found that prepareJavaScript will retrun HermesPreparedJavaScript object. You also reminded me that is only recommended for small amounts of JavaScript
Unfortunately, in my project, the server will push me a whold bundle(it contains all the rn code). After I get the bundle, the first time I will excute immediately by JSC. In the same time, I want to use hermes compiler to compile these bundle to bytecode and save it to the disk. When the next time I excute the bundle, I will check the bundle if it is bytecode version in the disk, that I will excute it by use hermes to imporve the launch speed!
So...that`s why I want to get a independent compiler for hermes.
@SinLucifer I see. It seems like one solution might be to run the Hermes compiler on the server and send the bytecode bundle to the client. Is it possible to do that?
@tmikov Sorry...is not a good idea because the bytecode bundle size is more huge than js bundle , that is why I want to build a complier in app to save client `s mobile intertnet data.
On the other hand, these is a experiment for testing Hermes launch performance online, we can not push bytecode bundle to all the user (it is possible if Hermes perform good~)
Really thank you again ~
@SinLucifer the bytecode bundle generated by Hermes is typically significantly smaller than the equivalent minified JS source. If you are getting a bytecode bundle larger than the input source, you are most likely invoking the compiler with suboptimal parameters. What options are you using?
You have to keep it mind that even though it is technically possible, Hermes is not designed or intended to perform compilation of the entire bundle on device. It is a highly optimizing compiler, requiring lots of CPU time and memory, intended to run on a server, not on a phone. So, running the full optimizing compiler on a phone is not a use case we are likely to support.
As I said, it is technically possible, but you would have to make your own Hermes Android build, and the results will probably be disappointing.
@tmikov I understand, hermes bundle size is smaller than normal JS bundle, for me bundle size reduced from 2.1mb to 2mb. But there are some concerns:
- Since the compression ratio of bytecode is less, my hermes bundle size still weighs more then normal JS bundle after compression.(784kb vs 430kb)
- Moving to hermes bundle(since it's bytecode), stripped me from the possibility of sending diff updated for my app.
Do you have any suggestions on how can I solve above issues?
PS I'm using following command to generate hermes bundle:
./hermes -emit-binary -out outputfile inputfile -output-source-map -O
@tmikov I understand, hermes bundle size is smaller than normal JS bundle, for me bundle size reduced from 2.1mb to 2mb. But there are some concerns:
- Since the compression ratio of bytecode is less, my hermes bundle size still weighs more then normal JS bundle after compression.(784kb vs 430kb)
- Moving to hermes bundle(since it's bytecode), stripped me from the possibility of sending diff updated for my app.
Do you have any suggestions on how can I solve above issues?
PS I'm using following command to generate hermes bundle:
./hermes -emit-binary -out outputfile inputfile -output-source-map -O
I've mentioned Hermes bundle size is more huge, that is because I forgot strip sourcemap LOL. Bytecode is more compact than text,so it`s compression ratio is less. I think u can send two version bundle to client, first use js, when hermes bundle is ok,use it. Next time, u can make a diff base on hermes bytecode.
Important: the next time you build a hermes bundle, you must build it base on the last hermes bundle, or you will find the diff file so large(build bundle directlly will generate string area again, it`s different every time)
@SinLucifer Can you answer following questions:
- How can I create that diff?
- How can I apply that diff?
@amnbnsl113 Try bsdiff.
@amnbnsl113 We typically see much more than 5% uncompressed bundle decrease compared to minified JS. Hard to tell what the problem is without looking at your input, but you may be hitting some pathological case.
On the other topics, I agree with @SinLucifer's recommendations.
@SinLucifer Thanks for telling me about bsdiff. This looks useful. @tmikov Is there any way I can check if more optimisations are possible. Any article/wiki/notes would be helpful.