antlrcs icon indicating copy to clipboard operation
antlrcs copied to clipboard

Remove the lamdba allocation during TypeRegistry`1.TryGetValue (which…

Open ndrwrbgs opened this issue 7 years ago • 2 comments

… is called indirectly by Template.Render())

.Where(i => i.IsAssignableFrom(key)) is using key which is captured from the surrounding closure. Therefore, the runtime allocates a new object to capture the Func<Type, bool> paired with it's required key input (rather than if there were, say, a WhereWithState<TSource, TState, TResult>(TSource, TState) method).

Since this is called by Template.Render() it adds unnecessary overhead we can remove without changes to the runtime by expanding the LINQ statement into a foreach loop.

ndrwrbgs avatar Apr 01 '19 04:04 ndrwrbgs

The travis-ci build seems to have been failed for unrelated reasons, the actual test run passed

Total tests: 610. Passed: 606. Failed: 0. Skipped: 4.
Test Run Successful.
Test execution time: 3.2077 Seconds
The command "dotnet test Antlr4.Test.StringTemplate -f netcoreapp2.0" exited with 0.

ndrwrbgs avatar Apr 01 '19 04:04 ndrwrbgs

I don't think it does the allocariok unless this code is hit, unless I misunderstand something grave. And if this code is being hit then it'd be hit in another method if moved anyway, right?

I know it shouldn't be hit frequently given there's a cache but why not avoid the allocation even on cache miss? :)

ndrwrbgs avatar Apr 02 '19 10:04 ndrwrbgs