-
Notifications
You must be signed in to change notification settings - Fork 579
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
A fix for huge memory usage for larger files. #393
base: master
Are you sure you want to change the base?
Changes from 7 commits
9b8f06e
51ef768
c420d33
3b25797
2893165
cd37095
605e08f
48aa622
f05d38d
61acd7b
3cc7cb1
94ead2a
9d3faef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,28 +207,31 @@ private static void StreamToTextWriter(MemoryStream memory, TextWriter writer) | |
/// Runs the template and returns the result. | ||
/// </summary> | ||
/// <param name="context">The current execution context.</param> | ||
/// <param name="reader"></param> | ||
/// <param name="outputWriter"></param> | ||
/// <returns>The merged result of the template.</returns> | ||
#if RAZOR4 | ||
public async Task Run(ExecuteContext context, TextWriter reader) | ||
public async Task Run(ExecuteContext context, TextWriter outputWriter) | ||
#else | ||
void ITemplate.Run(ExecuteContext context, TextWriter reader) | ||
void ITemplate.Run(ExecuteContext context, TextWriter outputWriter) | ||
#endif | ||
{ | ||
_context = context; | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
using (var writer = new StringWriter(builder)) | ||
string tempFilePath = Path.GetTempFileName(); | ||
|
||
try | ||
{ | ||
_context.CurrentWriter = writer; | ||
using (var writer = new StreamWriter(tempFilePath)) | ||
{ | ||
_context.CurrentWriter = writer; | ||
#if RAZOR4 | ||
await Execute(); | ||
#else | ||
Execute(); | ||
Execute(); | ||
#endif | ||
writer.Flush(); | ||
_context.CurrentWriter = null; | ||
|
||
writer.Flush(); | ||
_context.CurrentWriter = null; | ||
} | ||
|
||
if (Layout != null) | ||
{ | ||
|
@@ -237,23 +240,48 @@ void ITemplate.Run(ExecuteContext context, TextWriter reader) | |
|
||
if (layout == null) | ||
{ | ||
throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver."); | ||
throw new ArgumentException( | ||
"Template you are trying to run uses layout, but no layout found in cache or by resolver."); | ||
} | ||
|
||
// Push the current body instance onto the stack for later execution. | ||
var body = new TemplateWriter(tw => tw.Write(builder.ToString())); | ||
context.PushBody(body); | ||
var body = new TemplateWriter( | ||
tw => | ||
{ | ||
using (var reader = new StreamReader(tempFilePath)) | ||
{ | ||
// Push the current body instance onto the stack for later execution. | ||
while ( !reader.EndOfStream) | ||
{ | ||
tw.Write(reader.ReadLine()); | ||
} | ||
} | ||
}); | ||
|
||
context.PushBody(body); | ||
|
||
context.PushSections(); | ||
|
||
#if RAZOR4 | ||
await layout.Run(context, reader); | ||
await layout.Run(context, outputWriter); | ||
#else | ||
layout.Run(context, reader); | ||
layout.Run(context, outputWriter); | ||
#endif | ||
return; | ||
} | ||
|
||
reader.Write(builder.ToString()); | ||
using (var reader = new StreamReader(tempFilePath)) | ||
{ | ||
// Push the current body instance onto the stack for later execution. | ||
while (!reader.EndOfStream) | ||
{ | ||
outputWriter.Write(reader.ReadLine()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many thanks :) I had a problem generating huge files, that's why I tried to use a file Does that change look sensible? Possibly the best approach would be to generate small parts of the text at Cheers, On Sat, Jul 16, 2016 at 5:20 PM, campersau [email protected] wrote:
|
||
} | ||
} | ||
|
||
} | ||
finally | ||
{ | ||
File.Delete(tempFilePath); | ||
} | ||
} | ||
|
||
|
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.
ReadLine
does not include the new line characters. That's why some tests are failing.