Skip to content

Commit

Permalink
fix(doc): update android v8 and module document
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Nov 7, 2024
1 parent d6aa6b3 commit 2d2fc4c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/development/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [事件](development/native-event.md)
- [终端能力适配](development/native-adapter.md)
- [数据类型映射](development/type-mapping.md)
- [V8 API](development/v8-api.md)
- [V8初始化参数与API](development/v8-api.md)
- [调试](development/debug.md)
- [曝光上报](development/report.md)
- [技术支持](development/support.md)
Expand Down
65 changes: 33 additions & 32 deletions docs/development/native-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,47 +117,48 @@ public void helloNativeWithPromise(HippyMap hippyMap, Promise promise)
}
```



## 4. 注册Module

然后需要注册这个Module。需要在 `HippyPackage``getNativeModules` 方法中添加这个 Module,这样它才能在JS中被访问到。
需要自定义'APIProvider'类,并实现SDK HippyAPIProvider interface,然后在`getNativeModules` 方法中添加这个 Module,这样它才能在JS中被访问到。

```java
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.HippyPackage;
import com.tencent.mtt.hippy.common.Provider;
import com.tencent.mtt.hippy.example.module.TestModule;

import com.tencent.mtt.hippy.modules.javascriptmodules.HippyJavaScriptModule;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.uimanager.HippyViewController;
public class MyAPIProvider implements HippyAPIProvider {

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Override
public Map<Class<? extends HippyNativeModuleBase>, Provider<? extends HippyNativeModuleBase>> getNativeModules(final HippyEngineContext context)
{
Map<Class<? extends HippyNativeModuleBase>, Provider<? extends HippyNativeModuleBase>> modules = new HashMap<>();

public class ExamplePackages implements HippyPackage
{
@Override
public Map<Class<? extends HippyNativeModuleBase>, Provider<? extends HippyNativeModuleBase>> getNativeModules(final HippyEngineContext context)
//regist the MyModule
modules.put(TestModule.class, new Provider<HippyNativeModuleBase>()
{
Map<Class<? extends HippyNativeModuleBase>, Provider<? extends HippyNativeModuleBase>> modules = new HashMap<>();

// regist the LogModule
modules.put(ToastModule.class, new Provider<HippyNativeModuleBase>()
{
@Override
public HippyNativeModuleBase get()
{
return new TestModule(context);
}
});

return modules;
}
@Override
public HippyNativeModuleBase get()
{
return new TestModule(context);
}
});
}

@Override
public List<Class<? extends HippyJavaScriptModule>> getJavaScriptModules() {return null;}

@Override
public List<Class<? extends HippyViewController>> getControllers() {return null;}
}
```

## 5. 注册APIProvider

在HippyEngine初始化的EngineInitParams参数属性中设置providers。

``` java
List<HippyAPIProvider> providers = new ArrayList<>();
providers.add(new MyAPIProvider());
initParams.providers = providers;
```


## 注意事项

扩展Module中不能同步执行耗时操作,这可能卡住整个引擎通信线程。存在耗时场景,请使用异步线程处理。
Expand Down
24 changes: 24 additions & 0 deletions docs/development/v8-api.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# V8 相关初始化参数

在HippyEngine初始化的时候,EngineInitParams属性里有以下v8相关的属性参数。

## codeCacheTag

code cache 是V8中的一个特性,简单说就是JavaScript代码在执行前,需要进行解析和编译,才能正确执行,解析编译过程是耗时的,V8 暴露了一个方法,可以将编译产物序列化存储下来,下次再执行相同一段代码时,就可以用之前缓存的内容,节省了解析编译的时间,codeCacheTag作为编译内容缓存的key,设置后便会开启v8 code cache能力,建议开发者对该初始化参数进行设置,可以有效降低非首次启动js bundle加载运行耗时。

## v8InitParams

- initialHeapSize代表v8初始Heap size
- maximumHeapSize代表v8最大Heap size

由于v8的内存是自己管理的,使用策略是尽可能使用更多的内存,只有在达到maximumHeapSize 80%左右的时候才会触发gc,未达到之前会一直增长,达到80%触发gc的同时会回调near_heap_limit_callback接口获取内存增量,这里内存增量通过sdk内部接口V8VMInitParam::HeapLimitSlowGrowthStrategy返回,默认内存增长策略是当前max值*2,如果前端申请大内存,扩容后还不满足内存分配就会产生OOM.

在无限滚动列表场景,设置maximumHeapSize可以有效降低v8内存增加速率。

修改v8初始内存参数虽然能减少内存增量,但频繁的内存申请和gc,可能引入以下2个负面影响:

- 首屏性能下降
- OOM率升高

所以v8初始化内存参数的设置需要跟进具体的业务场景设置合适的值,并做完整的测试验证,如果不是内存占用有严格要的求场景不建议设置该初始化参数。

# V8 API

获取 V8 JS 引擎对象,并操作相关方法。
Expand Down

0 comments on commit 2d2fc4c

Please sign in to comment.