Jump to content
  • Advertisement
Sign in to follow this  
ASBai

Is it safe to use the char* argument of string factory as static data?

This topic is 537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the normal string factory in angelscript:

static string StringFactory(asUINT length, const char *s)
{
	return string(s, length);
}

But our string class is based on reference counting and copy-on-write, and it support Static Data: The string object can reference the externel char pointer directly, no malloc and memcpy is needed at this case.

 

So if the s argument is long term available, we can write something like:

static string StringFactory(asUINT length, const char *s)
{
	return string(s, length, StaticData); // no malloc and memcpy, requiring the lifetime of argument 's' is long enough
}

It is very efficient for both memory and processor usage.

 

In the "asCScriptEngine::AddConstantString" method, I found that every string constant is stored by the engine within a dedicated table by the deep copy semantic:

asCString *cstr = asNEW(asCString)(str, len);
// ...
stringToIdMap.Insert(asCStringPointer(cstr), index);

I guess the lifetime of items in the stringToIdMap are long enough, so is it possible to setting  cstr.AddressOf() as StringFactory's argument, so we can eliminate the memory allocation and memory copy ops in the factory?

Edited by ASBai

Share this post


Link to post
Share on other sites
Advertisement

Currently the life time of the strings in the stringToIdMap is as long as the engine itself. If your application, like most, keep the engine until the application itself is being shutdown then you should be OK with holding references to the char* given to the string factory functions without making a copy.

Share this post


Link to post
Share on other sites

Thanks,

 

So currently (2.31.2), the "const char *s" argument already a pointer stored in engine.stringToIdMap, it's life time is as long as the engine, right ? :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!