Browse Source

Restored AngelScript typeid caching.

Lasse Öörni 14 years ago
parent
commit
bc8b45b061

+ 7 - 7
Engine/Script/ScriptFile.cpp

@@ -257,8 +257,8 @@ asIScriptObject* ScriptFile::CreateObject(const String& className)
     
     
     // Ensure that the type implements the "ScriptObject" interface, so it can be returned to script properly
     // Ensure that the type implements the "ScriptObject" interface, so it can be returned to script properly
     bool found = false;
     bool found = false;
-    Map<asIObjectType*, bool>::ConstIterator i = checkedClasses_.Find(type);
-    if (i != checkedClasses_.End())
+    Map<asIObjectType*, bool>::ConstIterator i = validClasses_.Find(type);
+    if (i != validClasses_.End())
         found = i->second_;
         found = i->second_;
     else
     else
     {
     {
@@ -272,7 +272,7 @@ asIScriptObject* ScriptFile::CreateObject(const String& className)
                 break;
                 break;
             }
             }
         }
         }
-        checkedClasses_[type] = found;
+        validClasses_[type] = found;
     }
     }
     if (!found)
     if (!found)
     {
     {
@@ -298,7 +298,7 @@ asIScriptFunction* ScriptFile::GetFunction(const String& declaration)
     if (!compiled_)
     if (!compiled_)
         return 0;
         return 0;
     
     
-    Map<String, asIScriptFunction*>::ConstIterator i = functions_.Find(declaration);
+    HashMap<String, asIScriptFunction*>::ConstIterator i = functions_.Find(declaration);
     if (i != functions_.End())
     if (i != functions_.End())
         return i->second_;
         return i->second_;
     
     
@@ -315,10 +315,10 @@ asIScriptFunction* ScriptFile::GetMethod(asIScriptObject* object, const String&
     asIObjectType* type = object->GetObjectType();
     asIObjectType* type = object->GetObjectType();
     if (!type)
     if (!type)
         return 0;
         return 0;
-    Map<asIObjectType*, Map<String, asIScriptFunction*> >::ConstIterator i = methods_.Find(type);
+    Map<asIObjectType*, HashMap<String, asIScriptFunction*> >::ConstIterator i = methods_.Find(type);
     if (i != methods_.End())
     if (i != methods_.End())
     {
     {
-        Map<String, asIScriptFunction*>::ConstIterator j = i->second_.Find(declaration);
+        HashMap<String, asIScriptFunction*>::ConstIterator j = i->second_.Find(declaration);
         if (j != i->second_.End())
         if (j != i->second_.End())
             return j->second_;
             return j->second_;
     }
     }
@@ -526,7 +526,7 @@ void ScriptFile::ReleaseModule()
     {
     {
         // Clear search caches and event handlers
         // Clear search caches and event handlers
         includeFiles_.Clear();
         includeFiles_.Clear();
-        checkedClasses_.Clear();
+        validClasses_.Clear();
         functions_.Clear();
         functions_.Clear();
         methods_.Clear();
         methods_.Clear();
         UnsubscribeFromAllEventsWithUserData();
         UnsubscribeFromAllEventsWithUserData();

+ 6 - 5
Engine/Script/ScriptFile.h

@@ -23,9 +23,10 @@
 
 
 #pragma once
 #pragma once
 
 
+#include "HashMap.h"
+#include "HashSet.h"
 #include "Resource.h"
 #include "Resource.h"
 #include "ScriptEventListener.h"
 #include "ScriptEventListener.h"
-#include "Set.h"
 
 
 class Script;
 class Script;
 class ScriptInstance;
 class ScriptInstance;
@@ -96,13 +97,13 @@ private:
     /// Compiled flag.
     /// Compiled flag.
     bool compiled_;
     bool compiled_;
     /// Encountered include files during script file loading.
     /// Encountered include files during script file loading.
-    Set<String> includeFiles_;
+    HashSet<String> includeFiles_;
     /// Search cache for checking whether script classes implement "ScriptObject" interface.
     /// Search cache for checking whether script classes implement "ScriptObject" interface.
-    Map<asIObjectType*, bool> checkedClasses_;
+    Map<asIObjectType*, bool> validClasses_;
     /// Search cache for functions.
     /// Search cache for functions.
-    Map<String, asIScriptFunction*> functions_;
+    HashMap<String, asIScriptFunction*> functions_;
     /// Search cache for methods.
     /// Search cache for methods.
-    Map<asIObjectType*, Map<String, asIScriptFunction*> > methods_;
+    Map<asIObjectType*, HashMap<String, asIScriptFunction*> > methods_;
 };
 };
 
 
 /// Get currently executing script file.
 /// Get currently executing script file.

+ 22 - 0
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()
@@ -212,12 +217,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;
@@ -250,6 +260,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;
 
 
@@ -269,6 +282,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;
@@ -276,6 +292,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;
@@ -288,6 +307,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
@@ -112,14 +113,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;
@@ -128,6 +134,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

@@ -3493,10 +3493,16 @@ void asCScriptEngine::GCEnumCallback(void *reference)
 
 
 
 
 // TODO: multithread: The mapTypeIdToDataType must be protected with critical sections in all functions that access it
 // TODO: multithread: The mapTypeIdToDataType must be protected with critical sections in all functions that access it
+/// Urho3D: modified for type id caching
 int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dtIn) const
 int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dtIn) const
 {
 {
 	if( dtIn.IsNullHandle() ) return 0;
 	if( dtIn.IsNullHandle() ) return 0;
 
 
+	/// Urho3D: check cached type id first
+	int typeId = dtIn.GetCachedTypeId();
+	if( typeId )
+		return typeId;
+
 	// Register the base form
 	// Register the base form
 	asCDataType dt(dtIn);
 	asCDataType dt(dtIn);
 	if( dt.GetObjectType() )
 	if( dt.GetObjectType() )
@@ -3509,7 +3515,7 @@ int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dtIn) const
 	{
 	{
 		if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
 		if( mapTypeIdToDataType.GetValue(cursor)->IsEqualExceptRefAndConst(dt) )
 		{
 		{
-			int typeId = mapTypeIdToDataType.GetKey(cursor);
+			typeId = mapTypeIdToDataType.GetKey(cursor);
 			if( dtIn.GetObjectType() && !(dtIn.GetObjectType()->flags & asOBJ_ASHANDLE) )
 			if( dtIn.GetObjectType() && !(dtIn.GetObjectType()->flags & asOBJ_ASHANDLE) )
 			{
 			{
 				// The the ASHANDLE types behave like handles, but are really 
 				// The the ASHANDLE types behave like handles, but are really 
@@ -3520,6 +3526,8 @@ int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dtIn) const
 					typeId |= asTYPEID_HANDLETOCONST;
 					typeId |= asTYPEID_HANDLETOCONST;
 			}
 			}
 
 
+			// Urho3D: cache the type id for next query
+			dtIn.SetCachedTypeId(typeId);
 			return typeId;
 			return typeId;
 		}
 		}
 
 
@@ -3529,7 +3537,7 @@ int asCScriptEngine::GetTypeIdFromDataType(const asCDataType &dtIn) 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;