Browse Source

Cache the typeid into asCDataType for increased performance.

Lasse Öörni 15 years ago
parent
commit
2e5c04b5c0

+ 23 - 1
ThirdParty/AngelScript/source/as_datatype.cpp

@@ -28,6 +28,7 @@
    [email protected]
    [email protected]
 */
 */
 
 
+// Modified by Lasse Öörni for Urho3D
 
 
 //
 //
 // as_datatype.cpp
 // as_datatype.cpp
@@ -53,6 +54,8 @@ asCDataType::asCDataType()
 	isObjectHandle = false;
 	isObjectHandle = false;
 	isConstHandle  = false;
 	isConstHandle  = false;
 	funcDef        = 0;
 	funcDef        = 0;
+	// Urho3D: reset cached type ID
+	cachedTypeId   = 0;
 }
 }
 
 
 asCDataType::asCDataType(const asCDataType &dt)
 asCDataType::asCDataType(const asCDataType &dt)
@@ -64,6 +67,8 @@ asCDataType::asCDataType(const asCDataType &dt)
 	isObjectHandle = dt.isObjectHandle;
 	isObjectHandle = dt.isObjectHandle;
 	isConstHandle  = dt.isConstHandle;
 	isConstHandle  = dt.isConstHandle;
 	funcDef        = dt.funcDef;
 	funcDef        = dt.funcDef;
+	// Urho3D: copy cached type ID
+	cachedTypeId   = dt.cachedTypeId;
 }
 }
 
 
 asCDataType::~asCDataType()
 asCDataType::~asCDataType()
@@ -180,7 +185,7 @@ asCString asCDataType::Format() const
 			str += "const";
 			str += "const";
 	}
 	}
 
 
-    if( isReference )
+	if( isReference )
 		str += "&";
 		str += "&";
 
 
 	return str;
 	return str;
@@ -196,12 +201,17 @@ asCDataType &asCDataType::operator =(const asCDataType &dt)
 	isObjectHandle   = dt.isObjectHandle;
 	isObjectHandle   = dt.isObjectHandle;
 	isConstHandle    = dt.isConstHandle;
 	isConstHandle    = dt.isConstHandle;
 	funcDef          = dt.funcDef;
 	funcDef          = dt.funcDef;
+	// Urho3D: copy cached type id
+	cachedTypeId     = dt.cachedTypeId;
 
 
 	return (asCDataType &)*this;
 	return (asCDataType &)*this;
 }
 }
 
 
 int asCDataType::MakeHandle(bool b, bool acceptHandleForScope)
 int asCDataType::MakeHandle(bool b, bool acceptHandleForScope)
 {
 {
+	// Urho3D: reset cached type ID
+	cachedTypeId = 0;
+
 	if( !b )
 	if( !b )
 	{
 	{
 		isObjectHandle = b;
 		isObjectHandle = b;
@@ -228,6 +238,9 @@ int asCDataType::MakeHandle(bool b, bool acceptHandleForScope)
 
 
 int asCDataType::MakeArray(asCScriptEngine *engine)
 int asCDataType::MakeArray(asCScriptEngine *engine)
 {
 {
+	// Urho3D: reset cached type ID
+	cachedTypeId = 0;
+
 	if( engine->defaultArrayObjectType == 0 )
 	if( engine->defaultArrayObjectType == 0 )
 		return asINVALID_TYPE;
 		return asINVALID_TYPE;
 
 
@@ -247,6 +260,9 @@ int asCDataType::MakeArray(asCScriptEngine *engine)
 
 
 int asCDataType::MakeReference(bool b)
 int asCDataType::MakeReference(bool b)
 {
 {
+	// Urho3D: reset cached type ID
+	cachedTypeId = 0;
+
 	isReference = b;
 	isReference = b;
 
 
 	return 0;
 	return 0;
@@ -254,6 +270,9 @@ int asCDataType::MakeReference(bool b)
 
 
 int asCDataType::MakeReadOnly(bool b)
 int asCDataType::MakeReadOnly(bool b)
 {
 {
+	// Urho3D: reset cached type ID
+	cachedTypeId = 0;
+
 	if( isObjectHandle )
 	if( isObjectHandle )
 	{
 	{
 		isConstHandle = b;
 		isConstHandle = b;
@@ -266,6 +285,9 @@ int asCDataType::MakeReadOnly(bool b)
 
 
 int asCDataType::MakeHandleToConst(bool b)
 int asCDataType::MakeHandleToConst(bool b)
 {
 {
+	// Urho3D: reset cached type ID
+	cachedTypeId = 0;
+
 	if( !isObjectHandle ) return -1;
 	if( !isObjectHandle ) return -1;
 
 
 	isReadOnly = b;
 	isReadOnly = b;

+ 12 - 3
ThirdParty/AngelScript/source/as_datatype.h

@@ -28,6 +28,7 @@
    [email protected]
    [email protected]
 */
 */
 
 
+// Modified by Lasse Öörni for Urho3D
 
 
 //
 //
 // as_datatype.h
 // as_datatype.h
@@ -110,14 +111,19 @@ public:
 	int  GetSizeInMemoryBytes()  const;
 	int  GetSizeInMemoryBytes()  const;
 	int  GetSizeInMemoryDWords() const;
 	int  GetSizeInMemoryDWords() const;
 
 
-	void SetTokenType(eTokenType tt)         {tokenType = tt;}
-	void SetObjectType(asCObjectType *obj)   {objectType = obj;}
-	void SetFuncDef(asCScriptFunction *func) { asASSERT(funcDef); funcDef = func; }
+	// Urho3D: reset cached type id whenever something changes
+	void SetTokenType(eTokenType tt)         { tokenType = tt; cachedTypeId = 0; }
+	void SetObjectType(asCObjectType *obj)   { objectType = obj; cachedTypeId = 0; }
+	void SetFuncDef(asCScriptFunction *func) { asASSERT(funcDef); funcDef = func; cachedTypeId = 0; }
 
 
 	asCDataType &operator =(const asCDataType &);
 	asCDataType &operator =(const asCDataType &);
 
 
 	asSTypeBehaviour *GetBehaviour() const;
 	asSTypeBehaviour *GetBehaviour() const;
 
 
+	// Urho3D: cache the type id for repeated queries
+	void SetCachedTypeId(int id)       const {cachedTypeId = id;}
+	int GetCachedTypeId()              const {return cachedTypeId;}
+
 protected:
 protected:
 	// Base object type
 	// Base object type
 	eTokenType tokenType;
 	eTokenType tokenType;
@@ -126,6 +132,9 @@ protected:
 	asCObjectType *objectType;
 	asCObjectType *objectType;
 	asCScriptFunction *funcDef;
 	asCScriptFunction *funcDef;
 
 
+	// Urho3D: cached type id
+	mutable int cachedTypeId;
+
 	// Top level
 	// Top level
 	bool isReference:1;
 	bool isReference:1;
 	bool isReadOnly:1;
 	bool isReadOnly:1;

+ 10 - 2
ThirdParty/AngelScript/source/as_scriptengine.cpp

@@ -3219,13 +3219,21 @@ int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dt) const
 {
 {
 	if( dt.IsNullHandle() ) return 0;
 	if( dt.IsNullHandle() ) return 0;
 
 
+	// Urho3D: check first for cached id in the type itself
+	int typeId = dt.GetCachedTypeId();
+	if( typeId ) return typeId;
+	
 	// Find the existing type id
 	// Find the existing type id
 	asSMapNode<int,asCDataType*> *cursor = 0;
 	asSMapNode<int,asCDataType*> *cursor = 0;
 	mapTypeIdToDataType.MoveFirst(&cursor);
 	mapTypeIdToDataType.MoveFirst(&cursor);
 	while( cursor )
 	while( cursor )
 	{
 	{
 		if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
 		if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
-			return mapTypeIdToDataType.GetKey(cursor);
+		{
+			typeId = mapTypeIdToDataType.GetKey(cursor);
+			dt.SetCachedTypeId(typeId);
+			return typeId;
+		}
 
 
 		mapTypeIdToDataType.MoveNext(&cursor, cursor);
 		mapTypeIdToDataType.MoveNext(&cursor, cursor);
 	}
 	}
@@ -3233,7 +3241,7 @@ int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dt) const
 	// The type id doesn't exist, create it
 	// The type id doesn't exist, create it
 
 
 	// Setup the basic type id
 	// Setup the basic type id
-	int typeId = typeIdSeqNbr++;
+	typeId = typeIdSeqNbr++;
 	if( dt.GetObjectType() )
 	if( dt.GetObjectType() )
 	{
 	{
 		if( dt.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT ) typeId |= asTYPEID_SCRIPTOBJECT;
 		if( dt.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT ) typeId |= asTYPEID_SCRIPTOBJECT;