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

Author Topic: sf::Font wasting resources  (Read 1817 times)

0 Members and 1 Guest are viewing this topic.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
sf::Font wasting resources
« on: May 02, 2013, 11:58:07 pm »
Hello Laurent,

I've been looking into the sf::Font source code and I believe I found a considerably serious issue. What I found doesn't stop the class from working correctly, but it still is a bad practice, I think. I did debug and confirm what I will say, but if for some reason I am wrong, ignore the post altogether.

Inside sf::Font, we find the std::map<unsigned int, Page> where we store the texture and glyph information of each character size. The sf::Font::Page class by itself declares a sf::Texture locally. I found this a little weird due to the type of the container. An std::map uses a tree internally to keep all members efficiently ordered and this means that whenever we add or remove an element, the tree will re-structure itself to remain ordered, effectively copying around the Page objects.

So, I looked into the sf::Texture copy constructor, and it downloads all data from the source texture and uploads it to the destination texture. This means that on a font with plenty of character sizes active, there are potentially dozens of texture copies, and lots of data transfering to/from the gpu.

I verified this by making sure when a new page was added, sf::Texture would be copied around a few times in the re-structuring process of the tree. It seems like a huge overkill of the performance when the map could be a std::map<unsigned int, Page*>, avoiding unecessary copies all together (I tested it and it works as good).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Font wasting resources
« Reply #1 on: May 03, 2013, 06:26:21 am »
An std::map uses a tree internally to keep all members efficiently ordered and this means that whenever we add or remove an element, the tree will re-structure itself to remain ordered, effectively copying around the Page objects.
That's not true, the std::map nodes are stable. Only pointers are changed, that's why associative containers guarantee that iterators remain valid.

I verified this by making sure when a new page was added, sf::Texture would be copied around a few times in the re-structuring process of the tree.
I think the only time it is copied is during insertion. This issue can be addressed with C++11 move semantics.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: sf::Font wasting resources
« Reply #2 on: May 03, 2013, 01:00:44 pm »
It may have been some bug with my code, which I used to verify this, but indeed changing Page to Page* removed all unecessary copying and ensured only one allocation of sf::Texture :)

Thanks for the feedback!

 

anything