Skip to content

Commit

Permalink
fix global cache error (#482)
Browse files Browse the repository at this point in the history
* fix global cache error

* fix global cache error
  • Loading branch information
biezhihua authored Mar 27, 2024
1 parent 15eed6a commit 2129c0d
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 97 deletions.
57 changes: 27 additions & 30 deletions GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/GXTemplateEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,13 @@ class GXTemplateEngine {
return "GXTemplateItem(bizId='$bizId', templateId='$templateId')"
}

fun key(): String {
return "${bizId}-${templateId}"
/**
* key目前仅给GGXGlobalCache使用了,用于缓存之前计算的Layout结果。
* 增加了width维度,这样可以避免布局变化时错误使用缓存,导致的计算错误。
*/
fun key(size: GXMeasureSize): String {
return "${bizId}-${templateId}-${size.width}"
}


}

internal lateinit var context: Context
Expand Down Expand Up @@ -502,7 +504,7 @@ class GXTemplateEngine {
Log.e("prepareView")
}
try {
if (GXGlobalCache.instance.isExistForPrepareView(gxTemplateItem)) {
if (GXGlobalCache.instance.isExistForPrepareView(gxMeasureSize, gxTemplateItem)) {
return
}
if (GXPropUtils.isTrace() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Expand Down Expand Up @@ -577,9 +579,7 @@ class GXTemplateEngine {
* @param gxMeasureSize The template measure size, it look like a viewport of draw system, use to sure a size of template' view.
* @throws IllegalArgumentException
*/
fun bindData(
gxView: View?, gxTemplateData: GXTemplateData, gxMeasureSize: GXMeasureSize? = null
) {
fun bindData(gxView: View?, gxTemplateData: GXTemplateData, gxMeasureSize: GXMeasureSize? = null) {
if (Log.isLog()) {
Log.e("bindData")
}
Expand Down Expand Up @@ -631,17 +631,14 @@ class GXTemplateEngine {
val gxRootNode = gxTemplateContext.rootNode
if (gxRootNode != null) {

//
gxTemplateContext.reset()
GXGlobalCache.instance.clean()
if (Log.isLog()) {
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=recomputeWhenMeasureSizeChanged")
}

//
val size = Size(gxTemplateContext.size.width, gxTemplateContext.size.height)
GXNodeUtils.computeNodeTreeByPrepareView(gxTemplateContext, gxRootNode, size)
gxRootNode.stretchNode.layoutByPrepareView?.let {
GXGlobalCache.instance.putLayoutForPrepareView(
gxTemplateContext, gxTemplateContext.templateItem, it
)
GXGlobalCache.instance.putLayoutForPrepareView(gxTemplateContext, gxTemplateContext.templateItem, it)
GXNodeUtils.composeGXNodeByCreateView(gxRootNode, it)
}
}
Expand Down Expand Up @@ -694,8 +691,7 @@ class GXTemplateEngine {

val gxHostTemplateContext = gxExtendParams?.gxHostTemplateContext
if (gxHostTemplateContext != null) {
val itemCacheKey =
"${gxExtendParams.gxItemPosition}-${gxExtendParams.gxItemData.hashCode()}"
val itemCacheKey = "${gxExtendParams.gxItemPosition}-${gxExtendParams.gxItemData.hashCode()}"
if (gxHostTemplateContext.isExistNodeForScroll(itemCacheKey)) {
gxTemplateContext.rootNode = gxHostTemplateContext.obtainNodeForScroll(itemCacheKey)
gxTemplateContext.isReuseRootNode = true
Expand Down Expand Up @@ -775,20 +771,14 @@ class GXTemplateEngine {

if (gxTemplateContext.isReuseRootNode) {
if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree reuse root node, skip bindDataOnlyNodeTree"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree reuse root node, skip bindDataOnlyNodeTree")
}
gxTemplateContext.isReuseRootNode = false
return
}

if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree gxMeasureSize=${gxMeasureSize} gxTemplateItem=${gxTemplateContext.templateItem}"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree gxMeasureSize=${gxTemplateContext.size} gxTemplateItem=${gxTemplateContext.templateItem} gxMeasureSize=${gxMeasureSize} ")
}

gxTemplateContext.templateData = gxTemplateData
Expand All @@ -797,13 +787,18 @@ class GXTemplateEngine {
val oldMeasureSize = gxTemplateContext.size
gxTemplateContext.size = gxMeasureSize

if (Log.isLog()) {
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree gxMeasureSize update ${gxTemplateContext.size}")
}

// 判断是否size发生了变化
gxTemplateContext.isMeasureSizeChanged =
oldMeasureSize.width != gxMeasureSize.width || oldMeasureSize.height != gxMeasureSize.height
gxTemplateContext.isMeasureSizeChanged = oldMeasureSize.width != gxMeasureSize.width || oldMeasureSize.height != gxMeasureSize.height

// 如果size发生了变化,需要清除layout缓存,并重新计算
if (gxTemplateContext.isMeasureSizeChanged) {
gxTemplateContext.clearLayout()
gxTemplateContext.reset()
GXGlobalCache.instance.clean()
recomputeWhenMeasureSizeChanged(gxTemplateContext)
}
}
Expand Down Expand Up @@ -838,14 +833,16 @@ class GXTemplateEngine {
}
}

private fun internalBindDataOnlyViewTree(
view: View, gxTemplateData: GXTemplateData, gxMeasureSize: GXMeasureSize? = null
) {
private fun internalBindDataOnlyViewTree(view: View, gxTemplateData: GXTemplateData, gxMeasureSize: GXMeasureSize? = null) {
val gxTemplateContext = GXTemplateContext.getContext(view)
?: throw IllegalArgumentException("Not found templateContext from targetView")

if (gxMeasureSize != null) {
gxTemplateContext.size = gxMeasureSize

if (Log.isLog()) {
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=internalBindDataOnlyNodeTree gxMeasureSize update ${gxTemplateContext.size}")
}
}

gxTemplateContext.templateData = gxTemplateData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ object GXNodeUtils {
val itemVisualTemplateNode = itemTemplatePair.second

// Improve: 如果之前计算过,并且内容高度都一样,那么直接使用缓存计算。在横滑容器数据量较大的情况下,会节省一些时间。
if (GXGlobalCache.instance.isExistForTemplateItem(itemTemplateItem)) {
if (GXGlobalCache.instance.isExistForTemplateItem(gxTemplateContext.size, itemTemplateItem)) {
val itemLayout = GXGlobalCache.instance.getLayoutForTemplateItem(
gxTemplateContext,
itemTemplateItem
Expand Down Expand Up @@ -735,7 +735,7 @@ object GXNodeUtils {
gxTemplateItem, gxMeasureSize, gxItemTemplateInfo, gxVisualTemplateNode
)

if (!GXGlobalCache.instance.isExistForPrepareView(gxTemplateItem)) {
if (!GXGlobalCache.instance.isExistForPrepareView(gxMeasureSize, gxTemplateItem)) {
GXTemplateEngine.instance.render.prepareView(gxItemTemplateContext)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.view.View
import app.visly.stretch.Layout
import com.alibaba.gaiax.context.GXTemplateContext
import com.alibaba.gaiax.render.node.GXNode
import com.alibaba.gaiax.utils.Log

/**
* @suppress
Expand All @@ -39,6 +40,11 @@ open class GXViewTreeUpdate(context: GXTemplateContext, rootNode: GXNode) :

override fun withRootView(context: GXTemplateContext, node: GXNode, layout: Layout): View? {
return node.view?.also {

if (Log.isLog()) {
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=withRootView $layout")
}

GXViewLayoutParamsUtils.updateLayoutParams(it, layout, 0F, 0F)
}
}
Expand Down Expand Up @@ -68,6 +74,11 @@ open class GXViewTreeUpdate(context: GXTemplateContext, rootNode: GXNode) :
)
}
}

if (Log.isLog()) {
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=withChildView $childLayout targetView = $targetView")
}

GXViewLayoutParamsUtils.updateLayoutParams(targetView, childLayout, mergeX, mergeY)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ class GXGlobalCache {
key: GXTemplateEngine.GXTemplateItem,
value: Layout
) {
layoutForPrepareView[key.key()] = value

layoutForPrepareView[key.key(gxTemplateContext.size)] = value
if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=putLayoutForPrepareView key=${key.hashCode()} value=$value"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=putLayoutForPrepareView key=${key.hashCode()} value=$value")
}
}

Expand All @@ -24,28 +22,26 @@ class GXGlobalCache {
key: GXTemplateEngine.GXTemplateItem
): Layout? {
if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=getLayoutForPrepareView key=${key.hashCode()}"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=getLayoutForPrepareView key=${key.hashCode()}")
}
return layoutForPrepareView[key.key()]
return layoutForPrepareView[key.key(gxTemplateContext.size)]
}

fun isExistForPrepareView(key: GXTemplateEngine.GXTemplateItem): Boolean {
return layoutForPrepareView.containsKey(key.key())
fun isExistForPrepareView(
gxMeasureSize: GXTemplateEngine.GXMeasureSize,
key: GXTemplateEngine.GXTemplateItem
): Boolean {
return layoutForPrepareView.containsKey(key.key(gxMeasureSize))
}

/**
* layout cache for preview view.
*
* the cache will use to reduce computation at create view step.
*/
private val layoutForPrepareView: MutableMap<String, Layout> =
mutableMapOf()
private val layoutForPrepareView: MutableMap<String, Layout> = mutableMapOf()

private val layoutForTemplateItem: MutableMap<String, Layout> =
mutableMapOf()
private val layoutForTemplateItem: MutableMap<String, Layout> = mutableMapOf()

fun clean() {
layoutForTemplateItem.clear()
Expand All @@ -57,31 +53,25 @@ class GXGlobalCache {
key: GXTemplateEngine.GXTemplateItem,
value: Layout
) {
layoutForTemplateItem[key.key()] = value
layoutForTemplateItem[key.key(gxTemplateContext.size)] = value
if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=putLayoutForTemplateItem key=${key.key()} value=${value}"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=putLayoutForTemplateItem key=${key.key(gxTemplateContext.size)} value=${value}")
}
}

fun getLayoutForTemplateItem(
gxTemplateContext: GXTemplateContext,
key: GXTemplateEngine.GXTemplateItem
): Layout? {
val value = layoutForTemplateItem[key.key()]
val value = layoutForTemplateItem[key.key(gxTemplateContext.size)]
if (Log.isLog()) {
Log.e(
gxTemplateContext.tag,
"traceId=${gxTemplateContext.traceId} tag=getLayoutForTemplateItem key=${key.key()} value=${value}"
)
Log.e(gxTemplateContext.tag, "traceId=${gxTemplateContext.traceId} tag=getLayoutForTemplateItem key=${key.key(gxTemplateContext.size)} value=${value}")
}
return value
}

fun isExistForTemplateItem(key: GXTemplateEngine.GXTemplateItem): Boolean {
return layoutForTemplateItem.containsKey(key.key())
fun isExistForTemplateItem(gxMeasureSize: GXTemplateEngine.GXMeasureSize, key: GXTemplateEngine.GXTemplateItem): Boolean {
return layoutForTemplateItem.containsKey(key.key(gxMeasureSize))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)

add_library(entry SHARED hello.cpp)
add_library(entry SHARED stretch.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}/libstretch.so)
36 changes: 0 additions & 36 deletions GaiaXStretch/bindings/harmony_stretch/entry/src/main/cpp/hello.cpp

This file was deleted.

Loading

0 comments on commit 2129c0d

Please sign in to comment.