-
Notifications
You must be signed in to change notification settings - Fork 483
Speed up interface proxy w/o target type generation significantly #573
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
Merged
stakx
merged 6 commits into
castleproject:master
from
stakx:interface-proxy-type-generation-speedup
Feb 4, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c8f9d0
Avoid unneeded custom invocation types on interface proxy w/o target
stakx db247cd
Test proper invocation type selection
stakx d6c66f5
New invocation type should work for generic methods, too
stakx b87787f
Some generic methods now use non-generic invocation type
stakx 8c86054
Update `ref/` contracts
stakx 7b6b193
Update the changelog
stakx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Castle.Core.Tests/DynamicProxy.Tests/InvocationTypeReuseTestCase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2004-2021 Castle Project - http://www.castleproject.org/ | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| namespace Castle.DynamicProxy.Tests | ||
| { | ||
| using System; | ||
|
|
||
| using Castle.DynamicProxy.Internal; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| /// <summary> | ||
| /// This fixture checks which <see cref="IInvocation"/> types get used for proxied methods. | ||
| /// Usually, DynamicProxy generates a separate implementation type per proxied method, but | ||
| /// in some cases, it can reuse predefined implementation types. Because this is beneficial | ||
| /// for runtime performance (as it reduces the amount of dynamic type generation performed), | ||
| /// we want to ensure that those predefined types do in fact get picked when they should be. | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class InvocationTypeReuseTestCase : BasePEVerifyTestCase | ||
| { | ||
| [Test] | ||
| public void Non_generic_method_of_interface_proxy_without_target__uses__InterfaceMethodWithoutTargetInvocation() | ||
| { | ||
| var recorder = new InvocationTypeRecorder(); | ||
|
|
||
| var proxy = generator.CreateInterfaceProxyWithoutTarget<IWithNonGenericMethod>(recorder); | ||
| proxy.Method(); | ||
|
|
||
| Assert.AreEqual(typeof(InterfaceMethodWithoutTargetInvocation), recorder.InvocationType); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Generic_method_of_interface_proxy_without_target__uses__InterfaceMethodWithoutTargetInvocation() | ||
| { | ||
| var recorder = new InvocationTypeRecorder(); | ||
|
|
||
| var proxy = generator.CreateInterfaceProxyWithoutTarget<IWithGenericMethod>(recorder); | ||
| proxy.Method(42); | ||
|
|
||
| Assert.AreEqual(typeof(InterfaceMethodWithoutTargetInvocation), recorder.InvocationType); | ||
| } | ||
|
|
||
| public interface IWithNonGenericMethod | ||
| { | ||
| void Method(); | ||
| } | ||
|
|
||
| public interface IWithGenericMethod | ||
| { | ||
| void Method<T>(T arg); | ||
| } | ||
|
|
||
| private sealed class InvocationTypeRecorder : IInterceptor | ||
| { | ||
| public Type InvocationType { get; private set; } | ||
|
|
||
| public void Intercept(IInvocation invocation) | ||
| { | ||
| InvocationType = invocation.GetType(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Castle.Core/DynamicProxy/Internal/InterfaceMethodWithoutTargetInvocation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2004-2021 Castle Project - http://www.castleproject.org/ | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| namespace Castle.DynamicProxy.Internal | ||
| { | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Diagnostics; | ||
| using System.Reflection; | ||
|
|
||
| #if FEATURE_SERIALIZATION | ||
| [Serializable] | ||
| #endif | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public sealed class InterfaceMethodWithoutTargetInvocation : AbstractInvocation | ||
| { | ||
| public InterfaceMethodWithoutTargetInvocation(object target, object proxy, IInterceptor[] interceptors, MethodInfo proxiedMethod, object[] arguments) | ||
| : base(proxy, interceptors, proxiedMethod, arguments) | ||
| { | ||
| // This invocation type is suitable for interface method invocations that cannot proceed | ||
| // to a target, i.e. where `InvokeMethodOnTarget` will always throw: | ||
|
|
||
| Debug.Assert(target == null, $"{nameof(InterfaceMethodWithoutTargetInvocation)} does not support targets."); | ||
| Debug.Assert(proxiedMethod.IsAbstract, $"{nameof(InterfaceMethodWithoutTargetInvocation)} does not support non-abstract methods."); | ||
|
|
||
| // Why this restriction? Because it greatly benefits proxy type generation performance. | ||
| // | ||
| // For invocations that can proceed to a target, `InvokeMethodOnTarget`'s implementation | ||
| // depends on the target method's signature. Because of this, DynamicProxy needs to | ||
| // dynamically generate a separate invocation type per such method. Type generation is | ||
| // always expensive... that is, slow. | ||
| // | ||
| // However, if it is known that `InvokeMethodOnTarget` won't forward, but throw, | ||
| // no custom (dynamically generated) invocation type is needed at all, and we can use | ||
| // this unspecific invocation type instead. | ||
| } | ||
|
|
||
| // The next three properties mimick the behavior seen with an interface proxy without target. | ||
| // (This is why this type's name starts with `Interface`.) A similar type could be written | ||
| // for class proxies without target, but the values returned here would be different. | ||
|
|
||
| public override object InvocationTarget => null; | ||
|
|
||
| public override MethodInfo MethodInvocationTarget => null; | ||
|
|
||
| public override Type TargetType => null; | ||
|
|
||
| protected override void InvokeMethodOnTarget() => ThrowOnNoTarget(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.