Advertisement

char* or const char* in binding functions to script

Started by October 30, 2012 07:59 AM
2 comments, last by _Engine_ 12 years ago
Hi!

In script we can use only string but they are very very slow because using of strings leads to lots of allocations.

Simple example:

strint str;
str = "lots" + " of " + "allocations";

this simple line of code leads to 7 allocations and 3 deallocations. If we put 10 such line of code and we got 70 allocations and 30 deallocations in single frame. And this is awefull because this becaume bottlenck.

This problem may gone if i would bind functions like void opAdd(const char* in) / void opAdd(char* in) but i can't do that (((

Also in c++ code we never use std::string, we use only char str[] and we never have problems with unnecessary allocations. Also, because
we can't bind function with char* as parameter almost always we got unnecessary allocation when we call c++ function from script containing string as parametr.

Is there are any solution to avoid problem with string allocations? Do not use strings not a option (((
Even if you could bind const char * strings to AngelScript (I don't know if it's possible, someone else might know better), it doesn't solve your example. E.g. the following would not work

void foo(const char *str);
int main()
{
foo("lots" + " of " + "allocations");
}

Being able to register function foo to AngelScript helps nothing, since you could not call it like that anyways (from either side of the script boundary).

The C++ solution to the large number of allocations and deallocations in your example are move constructors ( http://msdn.microsof...y/dd293665.aspx ) introduced in C++11, which reduce the amount of unnecessary temporary object allocations and deallocations that need to be made.
Advertisement
In C++ char[] or char* is convenient and efficient, but it requires that the programmer know what he's doing so that the memory is properly managed. In AngelScript the programmer shouldn't have to have much knowledge to know how to work with strings, nor should he be able to (willfully or not) do something wrong like deallocate a local variable.

That said. It is possible to register char* by registering a value type to represent the pointer, e.g.


engine->RegisterObjectType("charptr", sizeof(char*), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);


You can then register the functions and objects to take this type wherever the application function takes a char* or const char*. Doing it like this will force the script writer to manually do the necessary memory allocations and deallocations, so be aware of this and prepare yourself for memory leaks or crashes due to badly written scripts.

You may also want to consider implementing your own custom string object rather than use the std::string. With a custom string object you can fully control how the memory is allocated and deallocated, and thus reduce the overhead to a minimum without sacrificing the "sandboxing" of the scripts. Perhaps, just a custom allocator for std::string is enough to do this.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks to advise I will try to dig this theme :)

This topic is closed to new replies.

Advertisement