From 3001723a9885235d90e36cb32e227dc2ab39f892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 21 Nov 2024 01:33:32 -0300 Subject: [PATCH] Do not heap allocate during LSPClientPlugin::drawTop. --- include/eepp/scene/node.hpp | 9 ++-- src/eepp/scene/node.cpp | 8 +-- .../ecode/plugins/lsp/lspclientplugin.cpp | 51 +++++++++++-------- .../ecode/plugins/lsp/lspclientplugin.hpp | 1 + 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/include/eepp/scene/node.hpp b/include/eepp/scene/node.hpp index 176aa4859..96261943b 100644 --- a/include/eepp/scene/node.hpp +++ b/include/eepp/scene/node.hpp @@ -405,16 +405,17 @@ class EE_API Node : public Transformable { Uint32 getNodeOfTypeIndex() const; void runOnMainThread( Actions::Runnable::RunnableFunc runnable, - const Time& delay = Seconds( 0 ), const Uint32& uniqueIdentifier = 0 ); + const Time& delay = Seconds( 0 ), + const Action::UniqueID& uniqueIdentifier = 0 ); void setTimeout( Actions::Runnable::RunnableFunc runnable, const Time& delay = Seconds( 0 ), - const Uint32& uniqueIdentifier = 0 ); + const Action::UniqueID& uniqueIdentifier = 0 ); void debounce( Actions::Runnable::RunnableFunc runnable, const Time& delay, - const Uint32& uniqueIdentifier ); + const Action::UniqueID& uniqueIdentifier ); void setInterval( Actions::Runnable::RunnableFunc runnable, const Time& interval, - const Uint32& uniqueIdentifier = 0 ); + const Action::UniqueID& uniqueIdentifier = 0 ); bool isChild( Node* child ) const; diff --git a/src/eepp/scene/node.cpp b/src/eepp/scene/node.cpp index 40235813f..d753945db 100644 --- a/src/eepp/scene/node.cpp +++ b/src/eepp/scene/node.cpp @@ -1585,21 +1585,21 @@ void Node::clearActions() { } void Node::runOnMainThread( Actions::Runnable::RunnableFunc runnable, const Time& delay, - const Uint32& uniqueIdentifier ) { + const Action::UniqueID& uniqueIdentifier ) { Action* action = Actions::Runnable::New( std::move( runnable ), delay ); action->setTag( uniqueIdentifier ); runAction( action ); } void Node::setTimeout( Actions::Runnable::RunnableFunc runnable, const Time& delay, - const Uint32& uniqueIdentifier ) { + const Action::UniqueID& uniqueIdentifier ) { Action* action = Actions::Runnable::New( std::move( runnable ), delay ); action->setTag( uniqueIdentifier ); runAction( action ); } void Node::debounce( Actions::Runnable::RunnableFunc runnable, const Time& delay, - const Uint32& uniqueIdentifier ) { + const Action::UniqueID& uniqueIdentifier ) { Action* prevAction = getActionManager()->getActionByTagFromTarget( this, uniqueIdentifier, true ); if ( prevAction ) { @@ -1612,7 +1612,7 @@ void Node::debounce( Actions::Runnable::RunnableFunc runnable, const Time& delay } void Node::setInterval( Actions::Runnable::RunnableFunc runnable, const Time& interval, - const Uint32& uniqueIdentifier ) { + const Action::UniqueID& uniqueIdentifier ) { Action* action = Actions::Runnable::New( std::move( runnable ), interval, true ); action->setTag( uniqueIdentifier ); runAction( action ); diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index ba83cd041..6877f6adc 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -18,6 +18,7 @@ #include #include #include +#include using namespace EE::Window; using json = nlohmann::json; @@ -1815,11 +1816,11 @@ bool LSPClientPlugin::onMouseMove( UICodeEditor* editor, const Vector2i& positio tryHideTooltip( editor, position ); return false; } - String::HashType tag = - String::hash( "LSPClientPlugin::onMouseMove-" + editor->getDocument().getFilePath() ); - editor->removeActionsByTag( tag ); + + String::HashType tag = hashCombine( String::hash( "LSPClientPlugin::onMouseMove-" ), + String::hash( editor->getDocument().getFilePath() ) ); mEditorsTags[editor].insert( tag ); - editor->runOnMainThread( + editor->debounce( [this, editor, position, tag]() { mEditorsTags[editor].erase( tag ); if ( !editorExists( editor ) ) @@ -1905,21 +1906,31 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart Float fontSize = editor->getUISceneNode()->getUIThemeManager()->getDefaultFontSize(); - bool isPath = !editor->getDocument().getFilePath().empty(); - std::string path( !isPath ? editor->getDocument().getFilename() - : editor->getDocument().getFilePath() ); + // Avoid heap allocating + static constexpr auto MAX_PATH_SIZE = 512; + String::StringBaseType filePath[MAX_PATH_SIZE]; + std::memset( filePath, 0, MAX_PATH_SIZE * sizeof( String::StringBaseType ) ); - Color textColor( editor->getColorScheme().getEditorColor( - mHoveringBreadcrumb == editor ? SyntaxStyleTypes::Text : SyntaxStyleTypes::LineNumber2 ) ); const auto& workspace = getManager()->getWorkspaceFolder(); - if ( isPath && !workspace.empty() && String::startsWith( path, workspace ) ) + std::string_view path( editor->getDocument().getFilePath() ); + if ( !workspace.empty() && String::startsWith( path, workspace ) ) path = path.substr( workspace.size() ); + if ( path.size() > MAX_PATH_SIZE ) + path = path.substr( path.size() - MAX_PATH_SIZE ); + + size_t pp = 0; + const char* c = path.data(); + while ( *c != '\0' && pp < MAX_PATH_SIZE ) + filePath[pp++] = utf8::unchecked::next( c ); + + Color textColor( editor->getColorScheme().getEditorColor( + mHoveringBreadcrumb == editor ? SyntaxStyleTypes::Text : SyntaxStyleTypes::LineNumber2 ) ); Float textOffsetY = eefloor( ( size.getHeight() - font->getLineSpacing( fontSize ) ) * 0.5f ); Vector2f pos( screenStart.x + eefloor( PixelDensity::dpToPx( 8 ) ), screenStart.y + textOffsetY ); - auto drawn = Text::draw( String::fromUtf8( path ), pos, font, fontSize, textColor ); + auto drawn = Text::draw( String::View( filePath ), pos, font, fontSize, textColor ); Lock l( mDocCurrentSymbolsMutex ); auto symbolsInfoIt = mDocCurrentSymbols.find( editor->getDocument().getURI() ); @@ -1927,14 +1938,17 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart return; pos.x += drawn.getWidth(); - UIIcon* icon = getUISceneNode()->findIcon( "chevron-right" ); + if ( mDrawSepIcon == nullptr ) + mDrawSepIcon = getUISceneNode()->findIcon( "chevron-right" ); Float textHeight = drawn.getHeight(); - const auto drawSep = [&pos, textHeight, icon, textColor, &drawn, &screenStart, textOffsetY]() { - if ( icon ) { + const auto& symbolsInfo = symbolsInfoIt->second; + + for ( const auto& info : symbolsInfo ) { + if ( mDrawSepIcon ) { pos.x += eefloor( PixelDensity::dpToPx( 8 ) ); Float iconSize = PixelDensity::dpToPxI( drawn.getHeight() * 0.5f ); - auto iconDrawable = icon->getSize( iconSize ); + auto iconDrawable = mDrawSepIcon->getSize( iconSize ); Color c = iconDrawable->getColor(); iconDrawable->setColor( textColor ); Float iconHeight = iconDrawable->getPixelsSize().getHeight(); @@ -1947,12 +1961,7 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart } else { pos.x += eefloor( PixelDensity::dpToPx( 16 ) ); } - }; - const auto& symbolsInfo = symbolsInfoIt->second; - - for ( const auto& info : symbolsInfo ) { - drawSep(); UIIcon* iconKind = getUISceneNode()->findIcon( info.icon ); if ( iconKind ) { auto iconDrawable = iconKind->getSize( fontSize ); @@ -1965,7 +1974,7 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart iconDrawable->setColor( c ); } - drawn = Text::draw( String::fromUtf8( info.name ), pos, font, fontSize, textColor ); + drawn = Text::draw( info.name, pos, font, fontSize, textColor ); pos.x += drawn.getWidth(); } } diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.hpp b/src/tools/ecode/plugins/lsp/lspclientplugin.hpp index 7ed20ce0e..0dc1082fa 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.hpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.hpp @@ -148,6 +148,7 @@ class LSPClientPlugin : public Plugin { } }; UnorderedMap> mDocCurrentSymbols; + UIIcon* mDrawSepIcon{ nullptr }; LSPClientPlugin( PluginManager* pluginManager, bool sync );