Opened 11 years ago

Closed 10 years ago

#2005 closed enhancement (wontfix)

[PATCH] CParser removed + Hotkey patch

Reported by: Jorma Rebane Owned by: Jorma Rebane
Priority: Must Have Milestone:
Component: Core engine Keywords: patch performance memory
Cc: Patch:

Description (last modified by Jorma Rebane)

1) CParser was a horrible freak of a regex parser. It left a horrible memory footprint and slowed the game down during load times. After replacing it with something much much simpler, the game startup and exit time was noticeably faster (alloc/dealloc overhead probably).

How CTokenizer works:

A string tokenizer doesn't own the string or memory you give it, so make sure the resource exists while you parse.

    std::string input = " toggle = 'Alt+F' = else ";
    std::string key, value;

    CTokenizer tokenizer(input);
    CTokenizer token;

    if(tokenizer.next(token, '=')) // split tokens at '='
    {   // if a token was availablee (no EOS)
        token.trim().toString(key); // trim whitespace, then tostr
        // key == "toggle"
    }
    if(tokenizer.next(token, '='))
    {
        token.trim(" '").toString(value); // trim spaces and '
        // value == "Alt+F"
    }

2) Hotkey patch is a fix that was pretty easy to make - it makes hotkey triggers a lot faster and memory efficient than they used to be. No real functionality changed inside 0 A.D. - only the stuff under the hood got changed.

Change History (22)

comment:1 by Jorma Rebane, 11 years ago

Description: modified (diff)
Resolution: fixed
Status: newclosed

comment:2 by Jorma Rebane, 11 years ago

Resolution: fixed
Status: closedreopened

comment:3 by Jorma Rebane, 11 years ago

Status: reopenednew

comment:4 by Kieran P, 11 years ago

Compiling error on my mac:

In file included from ../../../source/ps/ConfigDB.h:53,
                 from ../../../source/graphics/GameView.cpp:43:
../../../source/ps/Parser.h: In constructor ‘SStrTok::SStrTok(const std::string&)’:
../../../source/ps/Parser.h:62: error: ‘const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ has no member named ‘front’
../../../source/ps/Parser.h:62: error: ‘const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ has no member named ‘back’
../../../source/ps/Parser.h: In constructor ‘SStrTok::SStrTok(const CStr8&)’:
../../../source/ps/Parser.h:63: error: ‘const class CStr8’ has no member named ‘front’
../../../source/ps/Parser.h:63: error: ‘const class CStr8’ has no member named ‘back’
NetServer.cpp
make[1]: *** [obj/graphics_Release/GameView.o] Error 1
make: *** [graphics] Error 2

comment:5 by Kieran P, 11 years ago

Infact, I'm getting a few errors during compile. Here is a pastie with all of them: http://pastie.org/8081539

in reply to:  5 ; comment:6 by Jorma Rebane, 11 years ago

Replying to k776:

Infact, I'm getting a few errors during compile. Here is a pastie with all of them: http://pastie.org/8081539

Hi k776, thanks for taking a look at the patch. :) What version of GCC did you compile with? 4.2 or 4.7? std::string::front() and std::string::back() are standard functions, but I can easily replace them with something different.

As for the warnings of 'redundant redefinition of wcsdup', it seems to be some compatibility code in lib/posix/posix.h:

#if OS_MACOSX
# define EMULATE_WCSDUP 1
# define EMULATE_WCSCASECMP 1
#else
# define EMULATE_WCSDUP 0
# define EMULATE_WCSCASECMP 0
#endif

#if EMULATE_WCSDUP
extern wchar_t* wcsdup(const wchar_t* str);
#endif

#if EMULATE_WCSCASECMP
extern int wcscasecmp(const wchar_t* s1, const wchar_t* s2);
#endif

Perhaps you are running a newer compiler that now has wcsdup and wcscasecmp on MAC? In this case we'd have to update the condition:

Edit: Thanks for Markus for pointing out a mistake in the emulation condition. :)

// emulate for < 4.0 or < 4.7
#if OS_MACOSX && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7))
# define EMULATE_WCSDUP 1
# define EMULATE_WCSCASECMP 1
#else
# define EMULATE_WCSDUP 0
# define EMULATE_WCSCASECMP 0
#endif
Last edited 11 years ago by Jorma Rebane (previous) (diff)

in reply to:  6 ; comment:7 by Markus, 11 years ago

Replying to RedFox:

Perhaps you are running a newer compiler that now has wcsdup and wcscasecmp on MAC? In this case we'd have to update the condition:

Thats what #1149 is about.

in reply to:  7 comment:8 by Jorma Rebane, 11 years ago

Replying to Markus:

Thats what #1149 is about.

Thanks for pointing that out :) That ticket is 17 months old, though? Can someone test if adding that GNUC check fixes the problem on their MAC? If so, we can patch that ticket, too.

comment:9 by Jorma Rebane, 11 years ago

Summary: CParser replacement + Hotkey code patch[PATCH] CParser replacement + Hotkey code patch

comment:10 by Jorma Rebane, 11 years ago

Summary: [PATCH] CParser replacement + Hotkey code patch[PATCH] CParser removed + Hotkey patch

comment:11 by Jorma Rebane, 11 years ago

Keywords: patch review added; parser performance strtok hotkey removed

comment:12 by Jorma Rebane, 11 years ago

Priority: Should HaveMust Have

comment:13 by Jorma Rebane, 11 years ago

Milestone: BacklogAlpha 14

comment:14 by Jorma Rebane, 11 years ago

Keywords: performance memory added

comment:15 by Jorma Rebane, 11 years ago

Updated the patch file. Removed setlocale(LC_NUMERIC, "C") and atof calls. As a suggestion from leper, just used the simplest atof implementation possible, which should cover our needs.

Also fixed a bug in Hotkey parsing. Now duplicate/secondary hotkey definitions are ignored.

comment:16 by Jorma Rebane, 11 years ago

Description: modified (diff)

comment:17 by Jorma Rebane, 11 years ago

Fixed a small parsing bug that popped up after removing C atof. Empty tokens were sometimes not discarded. Using _tofloat to parse floats now; some slight optimizations added.

comment:18 by Kieran P, 11 years ago

Applied the latest patch and still getting compile errors. Here is build log and errors: http://pastie.org/8096337

Compiling with GCC 4.2.1.

in reply to:  18 comment:19 by Jorma Rebane, 11 years ago

Replying to k776: Looks like I accidentally left out TerrainManager.cpp. It's already becoming difficult to keep track of which changes apply to which patch... :/

comment:20 by Jorma Rebane, 11 years ago

Since my patches have started overlapping each-other, its impossible to submit one here. Check the original performance ticket #1995 for the cumulative patch.

comment:21 by Kieran P, 11 years ago

Milestone: Alpha 14Alpha 15

comment:22 by historic_bruno, 10 years ago

Keywords: review removed
Milestone: Alpha 15
Resolution: wontfix
Status: newclosed

Closing these tickets as no further active development is expected. See Philip's megapatch-split git branch for an attempt at splitting megapatch into it's separate parts: http://git.wildfiregames.com/gitweb/?p=0ad.git;a=shortlog;h=refs/heads/projects/philip/megapatch-split

Note: See TracTickets for help on using tickets.