Skip to content

Commit

Permalink
Do not heap allocate during LSPClientPlugin::drawTop.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Nov 21, 2024
1 parent a74263a commit 3001723
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
9 changes: 5 additions & 4 deletions include/eepp/scene/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/eepp/scene/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 );
Expand Down
51 changes: 30 additions & 21 deletions src/tools/ecode/plugins/lsp/lspclientplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <eepp/window/input.hpp>
#include <eepp/window/window.hpp>
#include <nlohmann/json.hpp>
#include <utf8cpp/utf8/unchecked.h>

using namespace EE::Window;
using json = nlohmann::json;
Expand Down Expand Up @@ -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 ) )
Expand Down Expand Up @@ -1905,36 +1906,49 @@ 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() );
if ( symbolsInfoIt == mDocCurrentSymbols.end() )
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();
Expand All @@ -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 );
Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/ecode/plugins/lsp/lspclientplugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class LSPClientPlugin : public Plugin {
}
};
UnorderedMap<URI, std::vector<DisplaySymbolInfo>> mDocCurrentSymbols;
UIIcon* mDrawSepIcon{ nullptr };

LSPClientPlugin( PluginManager* pluginManager, bool sync );

Expand Down

0 comments on commit 3001723

Please sign in to comment.