Skip to content

[Swift runtime features] Design caching mechanism for type metadata #2807

@jkurdek

Description

@jkurdek

TypeMetadata will used extensively when calling into Swift. We should avoid making a call into native library to obtain it each time by introducing a cache machanism.


Starting point:

public static class TypeMetadataCache {
    TypeMetadata MetadataOf(Type t, Func<Type, TypeMetadata> generator) {
          // make thread safe:
          if (cache.TryGet(t, out var result)) return result;
          var md = generator(t);
          cache.Add(t, md);
          return md;
}

Then you can rewrite this as:

static TypeMetadata typeMetadata = TypeMetadataCache.MetadataOf(typeof(Pair<T, U>), (t) => {
    var typeMetadataT = TypeMetadataCache.MetadataOf (typeof (T), (tT) => TypeMetadata.GetTypeMetadataOrThrow(tT));
    var typeMetadataU = TypeMetadataCache.MetadataOf (typeof(U), (tU) => TypeMetadata.GetTypeMetadataOrThrow(tU));
    return PairPInvokes.PInvokeMetadata (typeMetadataRequest.Complete, tT, TU);
});
public static TypeMetadata Metadata => typeMetadata;

Again, this is a start. The notion is that there is a cache which has a dictionary of Type onto TypeMetadata and takes a type to look up and if it's not present, takes a generator to call to retrieve that type.

Originally posted by @stephen-hawley in #2796 (comment)

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions