Skip to content

BHoM_Engine: Improve performance of (Try)RunExtensionMethod - #3658

Open
IsakNaslundBh wants to merge 6 commits into
developfrom
BHoM_Engine-#3654-ImprovePerformanceOfRunExtensionMethod
Open

IsakNaslundBh wants to merge 6 commits into
developfrom
BHoM_Engine-#3654-ImprovePerformanceOfRunExtensionMethod

Conversation

@IsakNaslundBh

Copy link
Copy Markdown
Contributor

Issues addressed by this PR

Closes #3654

Improve performance of (Try)RunExtensionMethod

This PR does two main updates:

  • Introducing custom lookup keys over the typ+methodname concatenated strings that was previously used for method lookup. This drastically reduces the overhead of the lookup.
  • Split out single argument methods to their own cache and lookup key. Reason for this is that those are (for most cases) the lightest methods, and gaint the most by ever further reduction in execution time (for example getting Geometry from a Node)
  • Get rid of all locks. Not needed as we are using ConcurrentDictionaries anyway
  • Move caches to the Global class (felt they where better put there next to the other caches)
  • Clear null-caches on assembly load (in case a method that was previously not loaded now could fit)

The performance gain from this will ofc vary both by method and runtime.
For methods with extremly low execution time (like the named Geometry method for a Node) this will mean a up to 7 times improvement in execution time. For slower methods (like Transform for Node) we are more talking about shaving off 20-30% of the runtime (compared to the 80-90% for the really small methods).

For larger methods this wont really matter at all to a large extent.

The part that can make this critical is for when running a method is called a multitude of times, like inside loops or similar, where the previous overhead was slowing down the operation, and leading to situations where the bottlneck was not actually executing the method, but string-concatination and hash computation of named concatenated strings.

This should anyhow give a large to minor performance boost for all extension method runners, without degrading any of the performance.

To note is that I also evaluated other slightly more complex options, including various generic ExtentionMethodRunners with precompiled switches on types etc. THose could in theory squeeze out a tiny fraction mroe of performance, but the gains there started to get to small for me to think that overhead in complication was worth implementing. Can revisit those if we feel there are cases that show this being an issue.

Test files

Will link to various workflows that makes heavy use of this, but as a simple test, All of our visible geometry in GH is going through this method.

Changelog

Additional comments

Make use of custom keys to improve method access and runtime
Get rid of locks not required with ConcurrentDictionaries
Store hits and misses (avoid re-running for nulls)
Ensure null-methods are cleared when a new assembly is loaded
Also here, gets rid of locks not required for COncurrentDictionaries
Previously was doing that, but in a two-step process. This changes it to make use of the Try method.

This method was changed before the TryRun pattern existed.
Rely on the previously used method.ToFunc()
@IsakNaslundBh IsakNaslundBh self-assigned this Nov 27, 2026
@IsakNaslundBh IsakNaslundBh added the type:feature New capability or enhancement label Nov 27, 2026
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

Successfully merging this pull request may close these issues.

BHoM_Engine: Improve performance of RunExtensionMethod

1 participant