Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Handles, Implicit Handles and GetReturnTypeId


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
5 replies to this topic

#1 behc   Members   -  Reputation: 631

Like
0Likes
Like

Posted 04 February 2011 - 11:57 AM

Hi

After recent update, I get lots of "The operand is implicitly converted to handle in order to compare them" warnings, all from "hSth == (or !=) null".
I tried turning on asEP_ALLOW_IMPLICIT_HANDLE_TYPES, but that doesn't change anything. My handle types don't have copy operators (in fact, with exception of add-on array, they can't be instanced in script), so I could skip all @, and work with handles more like with pointers in c++. Does/will implicit handles work that way?

My engine uses run-time declared types that works like simplified class inheritance. Base class is defined in C++ (and can be derived from other class using single inheritance model) and run-time declared class can add extra properties and events (thanks for update with function pointers Posted Image), this inheritance model is exposed to AS using asBEHAVE_(IMPLICIT)_REF_CAST, and since I don't know how many classes will be declared all casting behaviors are using single function.

void ASClassDownCast(asIScriptGeneric* gen)
{
	void* p = gen->GetObject();
	gen->SetReturnObject(p);
}

void ASClassUpCast(asIScriptGeneric* gen)
{
	ITHEntity* pGE = static_cast(gen->GetObject());
	if(pGE == NULL)
	{
		gen->SetReturnObject(NULL);
		return;
	}

	int RetType = gen->GetReturnTypeId();

	const ITHClass* pCls = pGE->GetClass();
	while(pCls)
	{
		if(pCls->GetScriptType() == RetType)
		{
			gen->SetReturnObject(pGE);
			return;
		}
		pCls = pCls->GetBaseClass();
	}

	gen->SetReturnObject(NULL);
}
void ASRegisterClassCastOp(asIScriptEngine* engine, const CTHString& SuperCls, const CTHString& DerivedCls)
{
	int r = 0;
	CTHString Decl;

	Decl.Empty();
	Decl << SuperCls << "@ f()";
	r = engine->RegisterObjectBehaviour(DerivedCls.c_str(), asBEHAVE_IMPLICIT_REF_CAST, Decl.c_str(), asFUNCTION(ASClassDownCast), asCALL_GENERIC); assert( r >= 0 );

	Decl.Empty();
	Decl << DerivedCls << "@ f()";
	r = engine->RegisterObjectBehaviour(SuperCls.c_str(), asBEHAVE_REF_CAST, Decl.c_str(), asFUNCTION(ASClassUpCast), asCALL_GENERIC); assert( r >= 0 );
}


I found that DownCast is more than 10x faster (I know it does little work). After code inspection i found that GetReturnTypeId (or rather GetTypeIdFromDataType) does lots of things (some loops, creating new type id's etc). And the question now. Can return type id be somewhere cashed or how can I speed things up a bit here?

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3370

Like
0Likes
Like

Posted 04 February 2011 - 03:34 PM

Handles should be compared with the is or !is operators in order to avoid doing value comparisons by mistake. With these operators you don't need to use the @ symbol.

The asEP_ALLOW_IMPLICIT_HANDLE_TYPES engine property is experimental. It allows you to declare script classes in a way so that all variables of that class will always be treated as handles. It's not documented, but you can find examples of it in test_implicithandle.cpp.

Yes, you can cache the return type id, for example by mapping the function id to the return type id. An faster alternative would be to create a templated version of the ASClassUpCast function where the template variables specify the desired cast. The class hierarchies article in the manual can probably help you with that implementation.

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

#3 behc   Members   -  Reputation: 631

Like
0Likes
Like

Posted 07 February 2011 - 09:49 AM

I somehow missed the purpose for "is" operator, if it is preferred way for handle comparison then I should rewrite my scripts.

I can't use template functions because I don't have any info about derived classes at compile time (they are declared in some config files and loaded when app starts).
Other than that, I use pretty much the same system (but, mine is limited to single parent inheritance only).

Anyway I found quick solution for my performance problem, using Set/GetUserData from asIScriptFunction.
void ASClassUpCast(asIScriptGeneric* gen)
{
	ITHEntity* pGE = static_cast(gen->GetObject());
	if(pGE == NULL)
	{
		gen->SetReturnObject(NULL);
		return;
	}

	int RetType = (int)gen->GetFunctionDescriptor()->GetUserData();

	const ITHClass* pCls = pGE->GetClass();
	while(pCls)
	{
		if(pCls->GetScriptType() == RetType)
		{
			gen->SetReturnObject(pGE);
			return;
		}
		pCls = pCls->GetBaseClass();
	}

	gen->SetReturnObject(NULL);
}
void ASRegisterClassCastOp(asIScriptEngine* engine, const CTHString& SuperCls, const CTHString& DerivedCls)
{
	int r = 0;
	CTHString Decl;

	Decl.Empty();
	Decl << SuperCls << "@ f()";
	r = engine->RegisterObjectBehaviour(DerivedCls.c_str(), asBEHAVE_IMPLICIT_REF_CAST, Decl.c_str(), asFUNCTION(ASClassDownCast), asCALL_GENERIC); assert( r >= 0 );

	Decl.Empty();
	Decl << DerivedCls << "@ f()";
	r = engine->RegisterObjectBehaviour(SuperCls.c_str(), asBEHAVE_REF_CAST, Decl.c_str(), asFUNCTION(ASClassUpCast), asCALL_GENERIC); assert( r >= 0 );
	
	asIScriptFunction* sf = engine->GetFunctionDescriptorById(r);
	sf->SetUserData((void*)sf->GetReturnTypeId());
}



Some numbers to prove, 100000x loop, time in [ms] (before / now)
"empty loop" 5,5 / 5,5
downcast 38,1 / 38,1
upcast 619,2 / 41,9
So I reduced time for upcasting to an order of magnitude of calling simple function (which was my goal).



The way asEP_ALLOW_IMPLICIT_HANDLE_TYPES works, looks promising, but it doesn't work for registered objects (I got -8 'invalid name' error) yet.
I'm not convinced with skipping @ at variable declaration though. How would you know what kind of assignment "=" is, if variable type gives no context?
Basicly I'm ok with "TypeName@ Var;" or even "TypeName@ Var1, Var2; //both are handles", but "@Var1 = Var2" looks very unintuitive for me and is source of 90% of my script errors (that's why I looked at implicit handles in first place). Could you consider implementing asEP_ALLOW_IMPLICIT_HANDLE_TYPES as 3-levels property? (like 0 - standard handles, 1 - @ dropped at assignments, 2 - @ dropped everywhere)



#4 Andreas Jonsson   Moderators   -  Reputation: 3370

Like
0Likes
Like

Posted 09 February 2011 - 05:54 PM

Heh, I forgot about the userdata on the function object. It was added for this very purpose but it slipped my mind.

With implicit handles the = operator would always be a handle assignment, which in turn means that there would not be any way of doing a value assignment, except through an explicit call to an method implemented for that purpose. This is the way Java works, and I'm seriously thinking about adopting it for AngelScript 3.0 (whenever I get that far), as it removes potential ambiguities in the code.

Since asEP_ALLOW_IMPLICIT_HANDLE_TYPES is experimental anyway I can certainly experiment some more. However, I don't know if I'll have time to do anytime soon. If you want to make the changes yourself though, I'll be happy to add them to the library.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#5 behc   Members   -  Reputation: 631

Like
0Likes
Like

Posted 10 February 2011 - 05:11 AM

Implementing asEP_ALLOW_IMPLICIT_HANDLE_TYPES was to complex for me (especially without knowledge of how parser and compiler work). Since I'm interested in dropping @ at handle assignments only, I created new engine property which forces that behavior for all reference types. (btw. script declared types are 'value' or 'reference'?)


(code sent via e-mail)

#6 Andreas Jonsson   Moderators   -  Reputation: 3370

Like
0Likes
Like

Posted 10 February 2011 - 11:41 AM

Script declared classes are always reference types.

I'll take a look at the code you sent me and incorporate it into the SVN as soon as possible. Thanks.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




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