Mangled names : Still a thing?

Started by Monsieur OUXX, Tue 02/08/2022 16:20:39

Previous topic - Next topic

Monsieur OUXX

I'm looking at the engine source code.
In this file ( cc_instance.cpp ), I see that the engine looks at the "actions" that have been queued, and then pops them in order to execute them (by calling the c++ function that has a matching prototype).

A function "matches" if :
- It has the same mangled name
- It has the same number of parameters.

To check that it has a known mangled name, it iterates over every known function name, and does a strncmp each time
Code: ags

if (strncmp(thisExportName, mangledName, mangled_len) == 0) { ... // line 352


Is that still a thing?
If yes, that would be a spot for extraordinarily beneficial somewhat beneficial optimization. EDIT: I realize that checking on the string's length first probably removes a lot of work from the comparison. But still, just a few function overloads can add quite many loops! E.g. comparison of mangled names "Button::Animate^4" with "Button::Animate^7"
Or maybe I misunderstood the purpose of this function and it's not meant for executing one instruction but instead for linking modules (and their symbols) together, only once? Or maybe this applies only in Debug mode, and the Release code gets rid of mangled names entirely?



 

Crimson Wizard

#1
There are at least two kinds of "mangled names" in the engine; first refers to the engine API (like the "Button::Animate" you mentioned), and uses "^" symbol to divide name and number of parameters. This may be seen in SystemImports::get_index_of:
https://github.com/adventuregamestudio/ags/blob/7946be9e75a0edda6fc9d955ea771f60f9b8e968/Engine/script/systemimports.cpp#L83-L107

In CallScriptFunction there is a second kind of "mangled name", which uses "$" symbol to divide name and number of parameters. The CallScriptFunction is used to call functions defined right in the game script. So why does it need these mangled names there - it's is a question, and I don't remember an answer. This code was probably not touched in years.

In SystemImports we use std::map to speed up search. Here in CallScriptFunction, I guess, this linear search may be replaced with a hash map where you do two lookups instead of one (first with "mangled" and second with regular name).

As a random thought, there's also a hash multimap which can store several items by the same key. The key could be the function name, and the values could contain a struct with parameter numbers and actual data. So first you get this group of items by a key, then iterate through that small group to find matching parameter number. (if I understand its behavior correctly)

It will be curious to compare a difference in perfomance if one of the above changes is made.

But, of course, it has to be clarified why engine needs this in the CallScriptFunction at all.

Monsieur OUXX

Thanks for clarifying that there are two types of mangled names. But you got my drift: any kind of hashing (or even just numeral codes for functions) could improve performance. Thanks for listening!
 

SMF spam blocked by CleanTalk