From af6c0be5238d0656c5d4fe2c8b0297c228922d2f Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Tue, 19 Nov 2024 16:38:47 +0100 Subject: [PATCH] Optimize state updates --- .../src/main/new_arch/MarkdownCommitHook.cpp | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/android/src/main/new_arch/MarkdownCommitHook.cpp b/android/src/main/new_arch/MarkdownCommitHook.cpp index bbaef301..78b10e48 100644 --- a/android/src/main/new_arch/MarkdownCommitHook.cpp +++ b/android/src/main/new_arch/MarkdownCommitHook.cpp @@ -107,26 +107,31 @@ RootShadowNode::Unshared MarkdownCommitHook::shadowTreeWillCommit( rootNode = rootNode->cloneTree( nodes.textInput->getFamily(), [this, &stateData, &textInputState, &nodes](ShadowNode const &node) { - auto newStateData = - std::make_shared(stateData); - // force measurement of a map buffer - newStateData->cachedAttributedStringId = 0; - - // setting -1 as the event counter makes sure that the update will be ignored by the java - // part of the code, which is what we want as we don't change the attributed string here - if (previousEventCount_.contains(nodes.textInput->getTag()) && + std::shared_ptr newNode = nullptr; + + if (stateData.cachedAttributedStringId != 0) { + auto newStateData = + std::make_shared(stateData); + + // force measurement of a map buffer + newStateData->cachedAttributedStringId = 0; + + // setting -1 as the event counter makes sure that the update will be ignored by the java + // part of the code, which is what we want as we don't change the attributed string here + if (previousEventCount_.contains(nodes.textInput->getTag()) && previousEventCount_[nodes.textInput->getTag()] == stateData.mostRecentEventCount) { newStateData->mostRecentEventCount = -1; - } else { + } else { previousEventCount_[nodes.textInput->getTag()] = stateData.mostRecentEventCount; - } + } + + auto newState = std::make_shared>( + newStateData, textInputState); - // clone the text input with the new state - auto newNode = node.clone({ - .state = - std::make_shared>( - newStateData, textInputState), - }); + newNode = node.clone({ .state = newState }); + } else { + newNode = node.clone({}); + } const auto currentDecoratorProps = nodes.decorator->getProps()->rawProps["markdownStyle"];