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

Use Razor caching to drastically improve performance. #202

Merged
merged 6 commits into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/Common/Razor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open RazorEngine.Templating
open RazorEngine.Configuration

type RazorRender(layoutRoots, namespaces) =
// Create resolver & set it to the global static filed
// Create resolver & set it to the global static field
let templateResolver =
{ new ITemplateResolver with
member x.Resolve name = File.ReadAllText(RazorRender.Resolve(layoutRoots, name + ".cshtml")) }
Expand All @@ -50,6 +50,23 @@ type RazorRender(layoutRoots, namespaces) =
let templateservice = new TemplateService(config)
do Razor.SetTemplateService(templateservice)

let handleCompile source f =
try
f ()
with
| :? TemplateCompilationException as ex ->
let csharp = Path.GetTempFileName() + ".cs"
File.WriteAllText(csharp, ex.SourceCode)
Log.run (fun () ->
use _c = Log.colored ConsoleColor.Red
printfn "\nProcessing the file '%s' failed\nSource written to: '%s'\nCompilation errors:" source csharp
for error in ex.Errors do
let errorType = if error.IsWarning then "warning" else "error"
printfn " - %s: (%d, %d) %s" errorType error.Line error.Column error.ErrorText
printfn ""
)
Log.close() // wait for the message to be printed completly
failwith "Generating HTML failed."
/// Global resolver (for use in 'DocPageTempalateBase')
static member val Resolver = null with get, set
/// Find file in one of the specified layout roots
Expand All @@ -66,28 +83,24 @@ type RazorRender(layoutRoots, namespaces) =
member val Model : obj = obj() with get, set
/// Dynamic object with more properties (?)
member val ViewBag = new DynamicViewBag() with get,set

member x.CompileTemplate(source, ?modelType) =
handleCompile source (fun _ ->
match modelType with
| Some t -> Razor.Compile(source, t, source)
| _ -> Razor.Compile(source, source))
member x.ProcessFileCache(name, ?properties) =
x.ViewBag <- new DynamicViewBag()
for k, v in defaultArg properties [] do
x.ViewBag.AddValue(k, v)
Razor.Run(name, x.Model, x.ViewBag)
/// Process source file and return result as a string
member x.ProcessFile(source, ?properties) =
try
handleCompile source (fun _ ->
x.ViewBag <- new DynamicViewBag()
for k, v in defaultArg properties [] do
x.ViewBag.AddValue(k, v)
let html = Razor.Parse(File.ReadAllText(source), x.Model, x.ViewBag, source)
html
with
| :? TemplateCompilationException as ex ->
let csharp = Path.GetTempFileName() + ".cs"
File.WriteAllText(csharp, ex.SourceCode)
Log.run (fun () ->
use _c = Log.colored ConsoleColor.Red
printfn "\nProcessing the file '%s' failed\nSource written to: '%s'\nCompilation errors:" source csharp
for error in ex.Errors do
printfn " - (%d, %d) %s" error.Line error.Column error.ErrorText
printfn ""
)
failwith "Generating HTML failed."

html)
and StringDictionary(dict:IDictionary<string, string>) =
member x.Dictionary = dict
/// Report more useful errors when key not found (.NET dictionary does not do this...)
Expand Down
21 changes: 15 additions & 6 deletions src/FSharp.MetadataFormat/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -899,14 +899,16 @@ type MetadataFormat =
[ for ns in asm.Namespaces do
for n in ns.Modules do yield! nestedModules n ]

let razor = RazorRender(layoutRoots, ["FSharp.MetadataFormat"])

let moduleTemplateFile = RazorRender.Resolve(layoutRoots, moduleTemplate)
Parallel.pfor modules (fun () -> RazorRender(layoutRoots,["FSharp.MetadataFormat"])) (fun modul _ razor ->
razor.CompileTemplate(moduleTemplateFile, typeof<ModuleInfo>)
for modul in modules do
Log.logf "Generating module: %s" modul.UrlName
razor.Model <- box (ModuleInfo.Create(modul, asm))
let out = razor.ProcessFile(moduleTemplateFile, props)
let out = razor.ProcessFileCache(moduleTemplateFile, props)
File.WriteAllText(outDir @@ (modul.UrlName + ".html"), out)
Log.logf "Finished module: %s" modul.UrlName
razor)

Log.logf "Generating types..."
let rec nestedTypes (modul:Module) = seq {
Expand All @@ -919,10 +921,17 @@ type MetadataFormat =

// Generate documentation for all types
let typeTemplateFile = RazorRender.Resolve(layoutRoots, typeTemplate)
Parallel.pfor types (fun () -> RazorRender(layoutRoots, ["FSharp.MetadataFormat"])) (fun typ _ razor ->
razor.CompileTemplate(typeTemplateFile, typeof<TypeInfo>)
for typ in types do
Log.logf "Generating type: %s" typ.UrlName
razor.Model <- box (TypeInfo.Create(typ, asm))
let out = razor.ProcessFile(typeTemplateFile, props)
let out = razor.ProcessFileCache(typeTemplateFile, props)
File.WriteAllText(outDir @@ (typ.UrlName + ".html"), out)
Log.logf "Finished type: %s" typ.UrlName
razor)
//Parallel.pfor types (fun () -> RazorRender(layoutRoots, ["FSharp.MetadataFormat"])) (fun typ _ razor ->
// Log.logf "Generating type: %s" typ.UrlName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please remove this dead code. thanks

// razor.Model <- box (TypeInfo.Create(typ, asm))
// let out = razor.ProcessFile(typeTemplateFile, props)
// File.WriteAllText(outDir @@ (typ.UrlName + ".html"), out)
// Log.logf "Finished type: %s" typ.UrlName
// razor)