From aab2ac65be64eafad0b20eae4d8b7f7230c6b754 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Thu, 13 Oct 2011 15:25:48 -0400 Subject: [PATCH] Reduce use of malloc() --- stmclient.cpp | 6 +++--- terminaldisplay.cpp | 4 ++-- terminaldisplay.hpp | 12 ++++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/stmclient.cpp b/stmclient.cpp index 1b450c0..730e584 100644 --- a/stmclient.cpp +++ b/stmclient.cpp @@ -128,9 +128,9 @@ void STMClient::output_new_frame( void ) overlays.apply( new_state ); /* calculate minimal difference from where we are */ - string diff = Terminal::Display::new_frame( !repaint_requested, - *local_framebuffer, - new_state ); + const string diff( Terminal::Display::new_frame( !repaint_requested, + *local_framebuffer, + new_state ) ); swrite( STDOUT_FILENO, diff.data(), diff.size() ); *local_framebuffer = new_state; diff --git a/terminaldisplay.cpp b/terminaldisplay.cpp index 0cea4fe..a18e4ab 100644 --- a/terminaldisplay.cpp +++ b/terminaldisplay.cpp @@ -170,7 +170,7 @@ std::string Display::new_frame( bool initialized, const Framebuffer &last, const /* have renditions changed? */ if ( (!initialized) || (f.ds.get_renditions().sgr() != frame.current_rendition_string) ) { - frame.append( f.ds.get_renditions().sgr() ); + frame.appendstring( f.ds.get_renditions().sgr() ); frame.current_rendition_string = f.ds.get_renditions().sgr(); } @@ -197,7 +197,7 @@ void Display::put_cell( bool initialized, FrameState &frame, const Framebuffer & if ( frame.current_rendition_string != rendition_str ) { /* print renditions */ - frame.append( rendition_str ); + frame.appendstring( rendition_str ); frame.current_rendition_string = rendition_str; } diff --git a/terminaldisplay.hpp b/terminaldisplay.hpp index bb73c26..c7949dd 100644 --- a/terminaldisplay.hpp +++ b/terminaldisplay.hpp @@ -15,8 +15,16 @@ namespace Terminal { Framebuffer last_frame; - FrameState( const Framebuffer &s_last ) : x(0), y(0), str(), cursor_x(0), cursor_y(0), current_rendition_string(), last_frame( s_last ) {} - void append( std::string s ) { str.append( s ); } + FrameState( const Framebuffer &s_last ) + : x(0), y(0), + str(), cursor_x(0), cursor_y(0), current_rendition_string(), + last_frame( s_last ) + { + str.reserve( 1024 ); + } + + void append( const char * s ) { str.append( s ); } + void appendstring( const std::string s ) { str.append( s ); } void append_silent_move( int y, int x ); };