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

Support reusable slices #79

Open
DamianEdwards opened this issue Dec 20, 2024 · 1 comment · May be fixed by #80
Open

Support reusable slices #79

DamianEdwards opened this issue Dec 20, 2024 · 1 comment · May be fixed by #80
Labels
enhancement New feature or request performance

Comments

@DamianEdwards
Copy link
Owner

For hyper memory optimization, support a slice declaring itself as reusable so the slice factory can reuse instances rather than creating new ones each time. The slice would return itself to the pool after it renders each time (slices are considered "dead" after rendering today).

This is a bit scary, but for some scenarios might be advantageous.

@DamianEdwards DamianEdwards added enhancement New feature or request performance labels Dec 20, 2024
@DamianEdwards
Copy link
Owner Author

There's an initial spike implementation of this in https://github.com/DamianEdwards/RazorSlices/tree/ReusableSlices

To use, simply add @implements IRazorReusableSlice to the top of your slice .cshtml file. You can optionally implement the bool TryReset() method if your slice has instance-level state it needs to clear before reuse.

Some initial results from adding support for this:

This image of the Visual Studio profiler (.NET Object Allocation Tracking) shows that (after warmup) the only thing allocated when rendering a reusable slice to a string 1000 times is the final string itself:

image

Here's benchmark results comparing rendering the same template via a regular slice vs. a reusable slice:

Method Mean Error StdDev Ratio Gen0 Allocated Alloc Ratio
RazorSlicesHello 195.2 us 1.44 us 1.27 us 1.00 26.1230 484.38 KB 1.00
RazorSlicesReusableHello 177.4 us 0.74 us 0.69 us 0.91 16.1133 296.88 KB 0.61

@DamianEdwards DamianEdwards linked a pull request Dec 20, 2024 that will close this issue
@DamianEdwards DamianEdwards linked a pull request Dec 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant