[Mono-list] today's patch

Miguel de Icaza miguel@ximian.com
21 Oct 2001 16:27:52 -0400


   I think that we will be creating too many objects in a case like
this one, and we wont be reusing them as effectively as we could:

> +					Type [] ifaces = expr_type.GetInterfaces ();
> +
> +					for (int i = ifaces.Length; i > 0;) {
> +						i--;
> +						if (ifaces [i] == target_type)
> +							return new EmptyCast (expr, target_type);
> +					}

   Every time we are query for interfaces on a type T we would create
an array of the interfaces and then scan it (I had a similar problem
yesterday with indexer getters and setters: we would be querying many
times the indexers and create many arrays, indeed, once per ocurrance
of the indexer).

   What I did was to have an auxiliary class that keeps track of the
Indexers associated with a Type (class Indexers) which exports a
static method `GetIndexersForType'.  This static method returns always
an `Indexer' object (or null if there are no indexers defined for the

   The interesting thing is that the ArrayLists created for the Type's
indexer is shared between all of the indexer invocations (ie, they all
point to the same Indexer object for a given type T).

   I think in this case we could do the same, we care about the output
of the above routine as `Does it implement the interface or not', so I
suggest that we change the above code to be:

	if (TypeManager.Implements (expr_type, target_type))
		return new EmptyCast (expr, target_type);
		return null;

    Then we can make `TypeManager.Implements' keep a static hashtable
that caches the result.  You can see an example in
expression.cs::Indexers class.