-
Notifications
You must be signed in to change notification settings - Fork 0
Implement hot reload support with cache clearing and registry reset #39
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
69 changes: 69 additions & 0 deletions
69
src/ExpressiveSharp/Services/ExpressiveHotReloadHandler.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,69 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using System.Reflection.Metadata; | ||
|
|
||
| [assembly: MetadataUpdateHandler(typeof(ExpressiveSharp.Services.ExpressiveHotReloadHandler))] | ||
|
|
||
| namespace ExpressiveSharp.Services; | ||
|
|
||
| internal static class ExpressiveHotReloadHandler | ||
| { | ||
| public static void ClearCache(Type[]? updatedTypes) | ||
| { | ||
| ResetGeneratedRegistries(SelectAffectedAssemblies(updatedTypes)); | ||
| ExpressiveResolver.ClearCachesForMetadataUpdate(); | ||
| ExpressiveReplacer.ClearCachesForMetadataUpdate(); | ||
| } | ||
|
|
||
| public static void UpdateApplication(Type[]? updatedTypes) => ClearCache(updatedTypes); | ||
|
|
||
| /// <summary> | ||
| /// When the runtime tells us which types changed, use their assemblies directly. | ||
| /// Fall back to a full scan only when <paramref name="updatedTypes"/> is null or empty, | ||
| /// which the runtime may do for large/unknown change sets. | ||
| /// </summary> | ||
| private static IEnumerable<Assembly> SelectAffectedAssemblies(Type[]? updatedTypes) | ||
| { | ||
| if (updatedTypes is { Length: > 0 }) | ||
| { | ||
| var set = new HashSet<Assembly>(); | ||
| foreach (var t in updatedTypes) | ||
| { | ||
| if (t is not null) set.Add(t.Assembly); | ||
| } | ||
|
Comment on lines
+31
to
+34
|
||
| return set; | ||
| } | ||
|
|
||
| return AppDomain.CurrentDomain.GetAssemblies(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Invokes <c>ResetMap()</c> on each assembly's generated <c>ExpressionRegistry</c> class | ||
| /// (when present) so the next <c>TryGet</c> rebuilds <c>LambdaExpression</c> instances | ||
| /// from the hot-reloaded factory IL. | ||
| /// </summary> | ||
| private static void ResetGeneratedRegistries(IEnumerable<Assembly> assemblies) | ||
| { | ||
| foreach (var assembly in assemblies) | ||
| { | ||
| if (assembly.IsDynamic) continue; | ||
|
|
||
| Type? registryType; | ||
| try | ||
| { | ||
| registryType = assembly.GetType("ExpressiveSharp.Generated.ExpressionRegistry", throwOnError: false); | ||
| } | ||
| catch | ||
| { | ||
| continue; | ||
| } | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
Comment on lines
+57
to
+60
|
||
|
|
||
| var reset = registryType?.GetMethod("ResetMap", BindingFlags.Static | BindingFlags.NonPublic); | ||
| if (reset is null) continue; | ||
|
|
||
| try { reset.Invoke(null, null); } | ||
| catch { /* best-effort; stale registry stays stale */ } | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| } | ||
|
Comment on lines
+48
to
+67
|
||
| } | ||
| } | ||
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
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
95 changes: 95 additions & 0 deletions
95
tests/ExpressiveSharp.Tests/Services/ExpressiveHotReloadHandlerTests.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,95 @@ | ||
| using System.Reflection; | ||
| using System.Reflection.Metadata; | ||
| using ExpressiveSharp.Services; | ||
| using ExpressiveSharp.Tests.TestFixtures; | ||
|
|
||
| namespace ExpressiveSharp.Tests.Services; | ||
|
|
||
| [TestClass] | ||
| public class ExpressiveHotReloadHandlerTests | ||
| { | ||
| [TestMethod] | ||
| public void ClearCache_AfterResolve_RemovesMemberFromCache() | ||
| { | ||
| var mi = typeof(Product).GetProperty(nameof(Product.Total))!; | ||
| var resolver = new ExpressiveResolver(); | ||
|
|
||
| _ = resolver.FindGeneratedExpression(mi); | ||
| Assert.IsTrue(ExpressiveResolver.IsExpressionCached(mi)); | ||
|
|
||
| ExpressiveHotReloadHandler.ClearCache(null); | ||
|
|
||
| Assert.IsFalse(ExpressiveResolver.IsExpressionCached(mi)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClearCache_PreservesAssemblyScanFilter() | ||
| { | ||
| var sentinel = new Func<Assembly, bool>(_ => true); | ||
| ExpressiveResolver.SetAssemblyScanFilter(sentinel); | ||
| try | ||
| { | ||
| ExpressiveHotReloadHandler.ClearCache(null); | ||
|
|
||
| Assert.AreSame(sentinel, ExpressiveResolver.GetAssemblyScanFilter()); | ||
| } | ||
| finally | ||
| { | ||
| ExpressiveResolver.SetAssemblyScanFilter(null); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClearCache_RebuildReturnsEquivalentExpression() | ||
| { | ||
| var mi = typeof(Product).GetProperty(nameof(Product.Total))!; | ||
| var resolver = new ExpressiveResolver(); | ||
|
|
||
| var before = resolver.FindGeneratedExpression(mi).ToString(); | ||
|
|
||
| ExpressiveHotReloadHandler.ClearCache(null); | ||
|
|
||
| var after = resolver.FindGeneratedExpression(mi).ToString(); | ||
|
|
||
| Assert.AreEqual(before, after); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClearCache_WithNullAndEmptyAndPopulatedArrays_DoesNotThrow() | ||
| { | ||
| ExpressiveHotReloadHandler.ClearCache(null); | ||
| ExpressiveHotReloadHandler.ClearCache([]); | ||
| ExpressiveHotReloadHandler.ClearCache([typeof(Product)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ClearCache_WithUpdatedTypes_ClearsResolverCache() | ||
| { | ||
| var mi = typeof(Product).GetProperty(nameof(Product.Total))!; | ||
| var resolver = new ExpressiveResolver(); | ||
|
|
||
| _ = resolver.FindGeneratedExpression(mi); | ||
| Assert.IsTrue(ExpressiveResolver.IsExpressionCached(mi)); | ||
|
|
||
| ExpressiveHotReloadHandler.ClearCache([typeof(Product)]); | ||
|
|
||
| Assert.IsFalse(ExpressiveResolver.IsExpressionCached(mi)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UpdateApplication_WithNull_DoesNotThrow() | ||
| { | ||
| ExpressiveHotReloadHandler.UpdateApplication(null); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Assembly_RegistersExpressiveHotReloadHandler() | ||
| { | ||
| var attributes = typeof(ExpressiveResolver).Assembly | ||
| .GetCustomAttributes<MetadataUpdateHandlerAttribute>() | ||
| .ToList(); | ||
|
|
||
| Assert.IsTrue(attributes.Any(a => a.HandlerType == typeof(ExpressiveHotReloadHandler)), | ||
| "MetadataUpdateHandlerAttribute for ExpressiveHotReloadHandler not found on ExpressiveSharp assembly."); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClearCacheignores theupdatedTypespayload and instead scans every loaded assembly on each hot reload. In larger apps this can be noticeably expensive (and can repeatedly hit reflection exceptions), even when only one project/type changed. Consider usingupdatedTypes(when non-null/non-empty) to derive the small set of affected assemblies and only attempt registry resets for those, falling back to a full scan only when the runtime doesn’t provide updated types.