ConnectedLivingSpace

ConnectedLivingSpace

8.1k Downloads

Speed up API wrapper

ShotgunNinja opened this issue ยท 2 comments

commented

The API wrapper scan the assemblies at each call, this can take up to 2 ms. While is true that the CLS handler returned can be cached as long as the active vessel doesn't change, the following replacement for the wrapper will avoid the assemblies scan:

static class CLS
{
  static PropertyInfo cls;

  static CLS()
  {
    Type cls_type = AssemblyLoader
      .loadedAssemblies
      .SelectMany(a => a.assembly.GetExportedTypes())
      .SingleOrDefault(t => t.FullName == "ConnectedLivingSpace.CLSAddon");

    if (cls_type != null) cls = cls_type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static);
  }

  public static bool has()
  {
    return cls != null;
  }

  public static ConnectedLivingSpace.ICLSAddon get()
  {
    return cls == null ? null : (ConnectedLivingSpace.ICLSAddon)cls.GetValue(null, null);
  }
}
commented

Thanks! I will incorporate.

commented

Implemented. Preserved original naming conventions to reduce refactoring with incorporation of new methodology.