Ancient tech: register a name, then get an integer to get better performance

Sometimes we want to use string name to represent object, and then we can access (get or set object via a name).
The biggest benefit is that string name is the most intuitive, easy to recognize, and easy to remember.
But the biggest disadvantage is that the performance is not comparable to integer.

There are some widely used methods to overcome the performance issue.

Method 1, don't use string at all. Use integer constants.

The disadvantages:

Method 2, use hash function to make a shorter form, such as an integer.

The disadvantages:


The better solution, RegisterWindowMessage (Windows API) way.
No means we need to use that function, but we can learn how that function works.
Instead of hard coded constants, or hashed uncontrollable values, we make a simple system to register a name to it then get a unique value.

The advantages:

Improvements:

#include <map>
#include <string>
#include <utility>

class NameRegister
{
protected:
	typedef std::map MapType;
public:
	MapType nameMap;
	int currentValue;

	NameRegister() : currentValue(0) {
	}

	int registerName(const std::string & name) {
		MapType::iterator it = this->nameMap.find(name);
		int value;

		if(it == this->nameMap.end()) {
			value = this->currentValue;
			
			++this->currentValue;

			this->nameMap.insert(std::pair(name, value));
		}
		else {
			value = (*it).second;
		}

		return value;
	}

	int getType(const std::string & name) {
		return this->registerName(name);
	}
};

Here are some FAQs you may interesting in.

Q: What's the difference between the registered value and the integer constants (method 1)?

A: The key and most important difference is the "registered value" method is still string name based. So you always still use the string name where it's comfortable.
The registered value is only a kind of shortcut (same as hash value) to access the name.

Q: The value may be different each time because it depends on the register order, is it OK?

A: Yes. Because the value number makes no sense, they are just shortcuts for string name.

Q: How can the registered value be used in game development?

A: It's useful in component system. Each component has a string name. But at runtime, each part that has interesting in any components, retrieve and retain the component type during initialization time then only use the type to access the components. In below code (a snippet from my own code base), see how function componentInit works.

class GRenderComponent : public GComponent
{
private:
	static int thisComponentType;
	static int globalTransformComponentType;
	static int orientationComponentType;

public:
	static void componentInit() {
		super::componentInit();

		thisComponentType = getComponentRegister()->getComponentTypeFromName(
			componentName_Render);
		globalTransformComponentType = getComponentRegister()->getComponentTypeFromName(
			componentName_GlobalTransform);
		orientationComponentType = getComponentRegister()->getComponentTypeFromName(
			componentName_Orientation);
	}
[omitted]

Write a comment

  • Required fields are marked with *
  • Security Code is case sensitive, 'A' is different with 'a'.

If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code: