Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Text setString slow  (Read 5140 times)

0 Members and 1 Guest are viewing this topic.

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
sf::Text setString slow
« on: July 20, 2016, 11:53:12 pm »
Hi, loving SFML but text processing is very slow. I expected it to be slow to a degree (I have worked with Freetype directly myself) however one thing that stands out in SFML is the setString function of sf::Text which takes a very big chunk of CPU time. On investigation I have found the slow down is actually in the copy constructor converting from std::string to sf::String. I can hold things as sf::String internally to a degree and then all is fine but at times I must use std::string and hence suffer the conversion to sf::String.

I wondered if there was a way of speeding this up? Oddly I find if I pass the return from c_str() of std::string (i.e. a char*) then it is slightly quicker.

thanks for any advice

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text setString slow
« Reply #1 on: July 21, 2016, 08:28:31 am »
String conversions are not supposed to be that slow. Can you provide a complete and minimal example that produces your performances problem? Can you also tell us what your environment is (so that we know which encodings you're dealing with)?
Laurent Gomila - SFML developer

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #2 on: July 21, 2016, 03:15:08 pm »
I will look to create a minimal example. The environment is Visual Studio 2015 with configuration properties set to "Use Multi-Byte Character Set" rather than Unicode.

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #3 on: July 21, 2016, 03:26:30 pm »
By the way if I drill down using a profiler I see that sf::Text::setString() takes 21.9% of my program's time
Within that String() takes 21.2%
Within that fromAnsi() takes 20.4%
Within that operator= takes 19.8%
Within that push_back takes 19.7%
Within that Insert takes 16% made up of:
  insert 6.3%
  operator+ 4.2%
  begin 3.5%

If I use sf::String the problem goes away so it must be to do with the conversion from std::string to sf::String. I will try to knock up a test case now

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #4 on: July 21, 2016, 04:00:28 pm »
OK I created a basic test case just passing a vector of std::string into a function that converted it to a sf::string. For 1 million items:

With project set to use Unicode:
Copying to a std::string took 4.9 seconds
Copying to a sf::String took 40.3 seconds

With project set to use Multibyte:
Copying to a std::string took 5.1 seconds
Copying to a sf::String took 40.3 seconds

Note: done in debug mode so no optimization but of course I have not factored in things like caching etc.

void SpeedTestToSFString(const std::vector<std::string> &lotsOfStrings)
{
   // Convert to sf::String
   for (auto &p : lotsOfStrings)
   {
      sf::String sfString(p);      
   }   
}

I will do another test but next time using the text output but it will need to wait until I have a minute
« Last Edit: July 21, 2016, 04:03:06 pm by Keith99 »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #5 on: July 21, 2016, 04:47:20 pm »
Benchmarking unoptimized builds is rather pointless. Re-run your test with optimizations enabled.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text setString slow
« Reply #6 on: July 21, 2016, 06:48:44 pm »
The STL is usually full of checks in debug mode, so yeah please don't benchmark debug builds...
Laurent Gomila - SFML developer

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #7 on: July 23, 2016, 02:30:23 pm »
Since I am comparing two things and not interested in absolute values it makes no difference. I have profiled in release mode and while the absolute numbers are naturally lower the scale of slowdown is identical.

By the way searching this forum I have found other posts from the past where people have obviously suffered the same issue with sf::Text (although sometimes without realising what it is). I am very busy but hope to explore this further and see if I can tweak the SFML code to reduce the problem and then suggest a possible fix.

Keith99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Text setString slow
« Reply #8 on: July 23, 2016, 02:40:34 pm »
Just for completeness my tests in release mode show sf:String copying to be 20 times slower than std::string

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text setString slow
« Reply #9 on: July 23, 2016, 03:10:44 pm »
Quote
Since I am comparing two things and not interested in absolute values it makes no difference.
Since the internal code is not the same in debug and release mode, you're not comparing the same thing. STL stuff is usually slower in debug mode but this shouldn't discourage you from using it, since in release the compiler optimizes away almost everything.

Quote
Just for completeness my tests in release mode show sf:String copying to be 20 times slower than std::string
Can you show the corresponding code please?
Laurent Gomila - SFML developer

 

anything