Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BHoM_Engine: ReflectAssemblyOnLoad to only reflect assemblies in BHoM folder #3416

Open
IsakNaslundBh opened this issue Sep 19, 2024 · 1 comment
Assignees
Labels
type:feature New capability or enhancement

Comments

@IsakNaslundBh
Copy link
Contributor

Description:

Did a quick investigation of the load time of BHoM in Grasshopper as well as Excel and found that the load speed of these UIs could be significantly improved by updating the automatic assembly extraction to be filtered to only automatically reflect dlls in the BHoM folder.

private static void ReflectAssemblyOnLoad(object sender, AssemblyLoadEventArgs args)
{
Compute.ExtractAssembly(args?.LoadedAssembly);
}

could be changed to something like:

        private static void ReflectAssemblyOnLoad(object sender, AssemblyLoadEventArgs args)
        {
            Assembly assembly = args?.LoadedAssembly;
            if (assembly != null)
            {
                if (!string.IsNullOrEmpty(assembly.Location) && Query.BHoMFolder() == System.IO.Path.GetDirectoryName(assembly.Location))
                {
                    Compute.ExtractAssembly(assembly);
                }

            }
        }

As an alternative we could use similar Regex filters that are used in the call to LoadAllAssemblies and also then have the regex loaded by a config that could be set up based on the current environment being loaded. Have not yet tested this latter option, but do not see any real reason why it should not work.

DOing this will significantly improve the load time of at least GH and Excel, as it seems like a lot of the time taken to load them is the assembly decomposition of the full appdomain which can be quite big.

@IsakNaslundBh IsakNaslundBh added the type:feature New capability or enhancement label Sep 19, 2024
@IsakNaslundBh IsakNaslundBh self-assigned this Sep 19, 2024
@IsakNaslundBh
Copy link
Contributor Author

Did some quick testing with regex solution, and think adding something like:

BHoMDllAtAllPattern = new Regex(@"oM$|_Engine$|_Adapter$|_UI$|_oM_\d+$|_Engine_\d+$|_Adapter_\d+$|_UI_\d+$");

And a method like:

        private static void LoadIfApplicable(Assembly assembly)
        {
            if (assembly != null && !assembly.IsDynamic && !string.IsNullOrWhiteSpace(assembly.Location))
            {
                string fileName = Path.GetFileNameWithoutExtension(assembly.Location);
                if (BHoMDllAtAllPattern.IsMatch(fileName))
                    Compute.ExtractAssembly(assembly);
            }
        }

That can be called from both ReflectAssemblyOnLoad as well as in the for loop in the static constructor could work quite well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New capability or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant