Source code question

Discussions about developing

Moderators: Main Administator, Global Moderator

User avatar
aspegic500
Main Administator
Main Administator
Posts: 735
Joined: Thu Apr 06, 2006 4:53 pm

Re: Source code question

Post by aspegic500 » Tue Mar 01, 2011 7:45 am

note that i don't manage to free memory of a hashmap correctly,
so in the xmoto code, you'll always see a hashmap together with a list with the same name with the suffit List.

kotlomoy
Xmoto Player
Xmoto Player
Posts: 33
Joined: Wed Jan 19, 2011 5:40 pm

Re: Source code question

Post by kotlomoy » Thu Mar 03, 2011 9:57 pm

I've located problem.

Code: Select all

FontGlyph* GLFontManager::getGlyph(const std::string& i_string) {
  GLFontGlyph *v_glyph;
  GLFontGlyphLetter *v_glyphLetter;

  v_glyph = m_glyphs[i_string.c_str()];
  if(v_glyph != NULL) return v_glyph;

  /* make sure that chars exists into the hashmap before continuing */
  unsigned int n = 0;
  std::string v_char;
  while(n < i_string.size()) {
    v_char = utf8::getNextChar(i_string, n);
    if(v_char != "n") {
      if(m_glyphsLetters[v_char.c_str()] == NULL) {
	v_glyphLetter = new GLFontGlyphLetter(v_char, m_ttf);
	m_glyphsLetters[v_char.c_str()] = v_glyphLetter;
	m_glyphsLettersList.push_back(v_glyphLetter);
      }
    }
  }

...

}
operator[] in this string

Code: Select all

      if(m_glyphsLetters[v_char.c_str()] == NULL) {
DOESN'T CREATE COPY OF STRING v_char
It just put pointer vchar.c_str() into hash_map as key.

So, next time when

Code: Select all

    v_char = utf8::getNextChar(i_string, n);
The key in hash_map CHANGES.

User avatar
aspegic500
Main Administator
Main Administator
Posts: 735
Joined: Thu Apr 06, 2006 4:53 pm

Re: Source code question

Post by aspegic500 » Fri Mar 04, 2011 8:11 am

hum, so, how do you explain it works ?

kotlomoy
Xmoto Player
Xmoto Player
Posts: 33
Joined: Wed Jan 19, 2011 5:40 pm

Re: Source code question

Post by kotlomoy » Fri Mar 04, 2011 7:27 pm

hum, so, how do you explain it works ?
May be you just don't use this function because ENABLE_SDLGFX is on.

I've fixed function:

Code: Select all

FontGlyph* GLFontManager::getGlyph(const std::string& i_string) {
  GLFontGlyph *v_glyph;
  GLFontGlyphLetter *v_glyphLetter;

  std::string * pString = new std::string( i_string );
  v_glyph = m_glyphs[ pString->c_str() ];

  if (v_glyph != NULL)
      {
      delete pString;
      return v_glyph;
      }

  /* make sure that chars exists into the hashmap before continuing */

  unsigned int n = 0;
  std::string * pChar;

  while(n < i_string.size())
      {
      pChar = new std::string( utf8::getNextChar( i_string, n ) );

      if ("n" == *pChar || NULL != m_glyphsLetters[ pChar->c_str() ] )
          {
          delete pChar;
          continue;
          }

      v_glyphLetter = new GLFontGlyphLetter(*pChar, m_ttf);
      m_glyphsLetters[pChar->c_str()] = v_glyphLetter;
      m_glyphsLettersList.push_back(v_glyphLetter);
      m_charList.push_back( pChar );
      }
  /* */

  v_glyph = new GLFontGlyph(i_string, m_glyphsLetters);

  m_glyphs[i_string.c_str()] = v_glyph;
  m_glyphsList.push_back(v_glyph);
  m_stringList.push_back( pString );

  return v_glyph;
}
Now I see all text, so it works.

User avatar
aspegic500
Main Administator
Main Administator
Posts: 735
Joined: Thu Apr 06, 2006 4:53 pm

Re: Source code question

Post by aspegic500 » Fri Mar 04, 2011 7:41 pm

no, sdlgfx is never used.

kotlomoy
Xmoto Player
Xmoto Player
Posts: 33
Joined: Wed Jan 19, 2011 5:40 pm

Re: Source code question

Post by kotlomoy » Fri Mar 04, 2011 8:00 pm

Walk this function in debugger (entering hash_map) and look for code where memory for key string allocated and string copied. I didn't find such code, that's why I wrote it myself.
P.S. I use STLport, but looked in original STL too. Didn't find such code.

kotlomoy
Xmoto Player
Xmoto Player
Posts: 33
Joined: Wed Jan 19, 2011 5:40 pm

Re: Source code question

Post by kotlomoy » Fri Mar 04, 2011 8:09 pm

The current problem: it's slow.
Physics levels are very slow. Other are slightly slower too.
But physics levels are big problem. As example, level "Lavine": less than 1 frame per second.

Question:
Is it normal? Is your current project fast enough in debug mode? Or physics works with normal speed in release code only?

User avatar
aspegic500
Main Administator
Main Administator
Posts: 735
Joined: Thu Apr 06, 2006 4:53 pm

Re: Source code question

Post by aspegic500 » Fri Mar 04, 2011 8:33 pm

what do you call debug mode ?

lavine is quite slow on my computer, but it's an extrem level.
i think that almost all levels are ok on my computer. but i agree that some levels developpers must have very good computer and create too big levels

for the hash map, i don't know what to answer. I used an example where as far as i remember, chars where not allocated ; but perhaps xmoto code is wrong about that.

kotlomoy
Xmoto Player
Xmoto Player
Posts: 33
Joined: Wed Jan 19, 2011 5:40 pm

Re: Source code question

Post by kotlomoy » Fri Mar 04, 2011 8:49 pm

what do you call debug mode ?
I mean debug configuration of project: all debug options, no optimization etc.
And release configuration: with all optimizations on, no debug options.

"Lavine" works fine when I use your XMoto 0.5.5 build. And works very bad in my project.
So I think: "Lavine" is slow in my project because no optimizations or because something wrong with my project?

User avatar
aspegic500
Main Administator
Main Administator
Posts: 735
Joined: Thu Apr 06, 2006 4:53 pm

Re: Source code question

Post by aspegic500 » Fri Mar 04, 2011 9:02 pm

the only debug mode is the -g option of gdb to include debug information for different tools.
(the debug mode of xmoto ie xmoto --debug, doesnt slow down xmoto)
but the gdb -g option doesn't really change the speed of xmoto, it works very well too.
of course with some debug tools like valgrind, xmoto becomes very slow, but that's exceptionnel to use it.

there is not really release version and debug version. The only change when you use the unoptimized option is to not allow g++ to use level 3 of optimisations to avoid small loops of special cases you can find in the g++ manual, but it's doesn't really change the xmoto speed too.

Post Reply

Return to “Developer's side”

Who is online

Users browsing this forum: No registered users and 1 guest