Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

char* or const char* in binding functions to script


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 _Engine_   Members   -  Reputation: 178

Like
0Likes
Like

Posted 30 October 2012 - 01:59 AM

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 (((

Sponsor:

#2 clb   Members   -  Reputation: 1780

Like
0Likes
Like

Posted 30 October 2012 - 05:23 AM

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.

Edited by clb, 30 October 2012 - 05:23 AM.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
0Likes
Like

Posted 30 October 2012 - 06:40 AM

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

#4 _Engine_   Members   -  Reputation: 178

Like
0Likes
Like

Posted 30 October 2012 - 12:28 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS