From 75046b532247af54c97861ed58aa22285d89c24e Mon Sep 17 00:00:00 2001 From: Dominic Watson Date: Tue, 3 Jul 2012 09:15:39 +0100 Subject: [PATCH] First stab at caching rendered includes for better performance --- .gitignore | 1 + org/cfstatic/CfStatic.cfc | 240 +++++++++++++++--- org/cfstatic/core/PackageCollection.cfc | 1 - org/cfstatic/util/Base.cfc | 4 + tests/Application.cfc | 7 - .../integration/org/cfstatic/CfStaticTest.cfc | 55 ++-- 6 files changed, 232 insertions(+), 76 deletions(-) diff --git a/.gitignore b/.gitignore index 46f708e..c74a142 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ example cfstatic.sublime-project cfstatic.sublime-workspace *.less.css +mxunit \ No newline at end of file diff --git a/org/cfstatic/CfStatic.cfc b/org/cfstatic/CfStatic.cfc index c59131c..0b53f15 100755 --- a/org/cfstatic/CfStatic.cfc +++ b/org/cfstatic/CfStatic.cfc @@ -125,40 +125,49 @@ - var filters = ""; - var buffer = $getStringBuffer(); - var renderCss = not StructKeyExists( arguments, 'type' ) or type eq 'css'; - var renderJs = not StructKeyExists( arguments, 'type' ) or type eq 'js'; - var minifyMode = iif( debugMode, DE('none'), DE( _getMinifyMode() ) ); + var filters = ""; + var renderCache = ""; + var buffer = $getStringBuffer(); + var renderCss = not StructKeyExists( arguments, 'type' ) or type eq 'css'; + var renderJs = not StructKeyExists( arguments, 'type' ) or type eq 'js'; + var includeCachedFile = ""; + var includeAll = ""; + var keys = ""; if ( renderCss ) { - filters = _getRequestIncludeFilters( 'css' ); + filters = _getRequestIncludeFilters( 'css', arguments.debugMode ); if ( _anythingToRender( filters ) ) { - buffer.append( _getCssPackages().renderincludes( - minification = minifyMode - , includePackages = filters.packages - , includeFiles = filters.files - , downloadExternals = _getDownloadexternals() - , charset = _getOutputCharset() - ) ); + includeAll = not Len( filters ) and _getIncludeAllByDefault(); + renderCache = _getRenderedIncludeCache( 'css', arguments.debugMode ); + keys = StructKeyArray( renderCache ); + + for( i=1; i LTE ArrayLen( keys ); i=i+1 ){ + includeCachedFile = includeAll or ListFind( filters, keys[i] ); + if ( includeCachedFile ) { + buffer.append( renderCache[ keys[i] ] ); + } + } } _clearRequestData( 'css' ); } if ( renderJs ) { - filters = _getRequestIncludeFilters( 'js' ); - + filters = _getRequestIncludeFilters( 'js', arguments.debugMode ); buffer.append( _renderRequestData() ); + if ( _anythingToRender( filters ) ) { - buffer.append( _getJsPackages().renderincludes( - minification = minifyMode - , includePackages = filters.packages - , includeFiles = filters.files - , downloadExternals = _getDownloadexternals() - , charset = _getOutputCharset() - ) ); + includeAll = not Len( filters ) and _getIncludeAllByDefault(); + renderCache = _getRenderedIncludeCache( 'js', arguments.debugMode ); + keys = StructKeyArray( renderCache ); + + for( i=1; i LTE ArrayLen( keys ); i=i+1 ){ + includeCachedFile = includeAll or ListFind( filters, keys[i] ); + if ( includeCachedFile ) { + buffer.append( renderCache[ keys[i] ] ); + } + } } _clearRequestData( 'js' ); @@ -182,7 +191,7 @@ _setCssPackages( _packageDirectory( cssDir, _getCssUrl(), _getMinifiedUrl(), 'css' ) ); _cacheIncludeMappings(); - + _cacheRenderedIncludes(); _compileCssAndJavascript(); @@ -208,9 +217,9 @@ + var mappings = StructNew(); var jsPackages = _getJsPackages().getOrdered(); var cssPackages = _getCssPackages().getOrdered(); - var mappings = StructNew(); var i = 0; for( i=1; i LTE ArrayLen( jsPackages ); i=i+1 ){ @@ -297,32 +306,147 @@ - - - + + + var includes = _getRequestIncludes(); var mappings = _getIncludeMappings( type ); - var filters = StructNew(); + var filters = ""; + var includeFiles = debugMode OR ListFindNoCase( "file,none", _getMinifyMode() ); var i = 0; - filters.packages = ArrayNew(1); - filters.files = ArrayNew(1); - for( i=1; i LTE ArrayLen(includes); i++ ){ if ( StructKeyExists( mappings, includes[i] ) ) { - filters.packages = $arrayMerge( filters.packages, mappings[includes[i]].packages ); - filters.files = $arrayMerge( filters.files , mappings[includes[i]].files ); + if ( includeFiles ) { + filters = ListAppend( filters, ArrayToList( mappings[includes[i]].files ) ); + } else { + filters = ListAppend( filters, ArrayToList( mappings[includes[i]].packages ) ); + + } } } - filters.packages = $arrayRemoveDuplicates( filters.packages ); - filters.files = $arrayRemoveDuplicates( filters.files ); - return filters; + + + _setupRenderedIncludeCache(); + + switch( _getMinifyMode() ){ + case 'all' : _cacheRenderedIncludesForAllMode() ; break; + case 'package' : _cacheRenderedIncludesForPackageMode(); break; + default : _cacheRenderedIncludesForFileMode() ; break; + } + + _cacheRenderedIncludesForFileMode( debug = true ); + + + + + + _addRenderedIncludeToCache( 'js', '/', _getJsPackages().renderIncludes( + minification = _getMinifyMode() + , downloadExternals = _getDownloadExternals() + , charset = _getOutputCharset() + ) ); + _addRenderedIncludeToCache( 'css', '/', _getCssPackages().renderIncludes( + minification = _getMinifyMode() + , downloadExternals = _getDownloadExternals() + , charset = _getOutputCharset() + ) ); + + + + + + var collection = ""; + var packages = ""; + var package = ""; + var types = ListToArray("js,css"); + var minifyMode = ""; + var type = ""; + var i = 0; + var n = 0; + + for( n=1; n LTE ArrayLen( types ); n=n+1 ){ + type = types[n]; + + if ( type EQ 'js' ) { + collection = _getJsPackages(); + } else { + collection = _getCssPackages(); + } + packages = collection.getOrdered(); + + + for( i=1; i LTE ArrayLen( packages ); i=i+1 ){ + package = collection.getPackage( packages[i] ); + + if ( packages[i] EQ 'external' and not _getDownloadExternals() ){ + minifyMode = 'none'; + } else { + minifyMode = _getMinifyMode(); + } + + _addRenderedIncludeToCache( type, packages[i], package.renderIncludes( + minification = minifyMode + , charset = _getOutputCharset() + ) ); + } + } + + + + + + + + var types = ListToArray("js,css"); + var type = ""; + var collection = ""; + var packages = ""; + var package = ""; + var files = ""; + var file = ""; + var i = 0; + var n = 0; + var x = 0; + var minified = iif( debug, DE( false ), DE( _getMinifyMode() EQ 'file' ) ); + + for( n=1; n LTE ArrayLen( types ); n=n+1 ){ + type = types[n]; + + if ( type EQ 'js' ) { + collection = _getJsPackages(); + } else { + collection = _getCssPackages(); + } + packages = collection.getOrdered(); + for( i=1; i LTE ArrayLen( packages ); i=i+1 ){ + package = collection.getPackage( packages[i] ); + files = package.getOrdered(); + + for( x=1; x LTE ArrayLen( files ); x=x+1 ) { + file = package.getStaticFile( files[x] ); + + _addRenderedIncludeToCache( + type = type + , path = files[x] + , debug = debug + , rendered = file.renderInclude( + minified = minified and ( packages[i] neq 'external' or _getDownloadExternals() ) + , charset = _getOutputCharset() + ) + ); + } + } + } + + + @@ -746,9 +870,9 @@ - + - + @@ -765,6 +889,46 @@ + + + _renderedIncludeCache = StructNew(); + _renderedIncludeCache.js = $orderedStructNew(); + _renderedIncludeCache.css = $orderedStructNew(); + + _renderedIncludeCache.debug = StructNew(); + _renderedIncludeCache.debug.js = $orderedStructNew(); + _renderedIncludeCache.debug.css = $orderedStructNew(); + + + + + + + + + + + if ( debug ) { + _renderedIncludeCache.debug[ arguments.type ][ arguments.path ] = arguments.rendered; + } else { + _renderedIncludeCache[ arguments.type ][ arguments.path ] = arguments.rendered; + } + + + + + + + + + if ( debug ) { + return _renderedIncludeCache.debug[ arguments.type ]; + } + + return _renderedIncludeCache[ arguments.type ]; + + + diff --git a/org/cfstatic/core/PackageCollection.cfc b/org/cfstatic/core/PackageCollection.cfc index 2841655..a728823 100644 --- a/org/cfstatic/core/PackageCollection.cfc +++ b/org/cfstatic/core/PackageCollection.cfc @@ -309,7 +309,6 @@ var i = ""; ArraySort( packages, 'text' ); - for( i=1; i LTE ArrayLen(packages); i=i+1 ){ _addPackageToOrderedList( packages[i] ); } diff --git a/org/cfstatic/util/Base.cfc b/org/cfstatic/util/Base.cfc index 5aa21f2..982127d 100644 --- a/org/cfstatic/util/Base.cfc +++ b/org/cfstatic/util/Base.cfc @@ -427,6 +427,10 @@ + + + + diff --git a/tests/Application.cfc b/tests/Application.cfc index 4757e26..dbbd3a7 100644 --- a/tests/Application.cfc +++ b/tests/Application.cfc @@ -26,11 +26,4 @@ ) /> - - - onApplicationStart(); - - return true; - - \ No newline at end of file diff --git a/tests/integration/org/cfstatic/CfStaticTest.cfc b/tests/integration/org/cfstatic/CfStaticTest.cfc index 13495ec..5e285a5 100644 --- a/tests/integration/org/cfstatic/CfStaticTest.cfc +++ b/tests/integration/org/cfstatic/CfStaticTest.cfc @@ -25,15 +25,13 @@ var failed = false; rootDir &= 'badFiles/mixedMediaInPackage/'; - cfstatic.init( - staticDirectory = rootDir - , staticUrl = "/any/old/thing" - , minifyMode = "package" - , debugKey = "doNotLetMxUnitDebugScrewTests" - ); - try { - cfstatic.renderIncludes(); + cfstatic.init( + staticDirectory = rootDir + , staticUrl = "/any/old/thing" + , minifyMode = "package" + , debugKey = "doNotLetMxUnitDebugScrewTests" + ); } catch ( "cfstatic.Package.badConfig" e ) { failed = true; @@ -48,14 +46,13 @@ var failed = false; rootDir &= 'badFiles/mixedIeInPackage/'; - cfstatic.init( - staticDirectory = rootDir - , staticUrl = "/any/old/thing" - , minifyMode = "package" - , debugKey = "doNotLetMxUnitDebugScrewTests" - ); try { - cfstatic.renderIncludes(); + cfstatic.init( + staticDirectory = rootDir + , staticUrl = "/any/old/thing" + , minifyMode = "package" + , debugKey = "doNotLetMxUnitDebugScrewTests" + ); } catch ( "cfstatic.Package.badConfig" e ) { failed = true; @@ -70,15 +67,14 @@ var failed = false; rootDir &= 'badFiles/mixedMediaInAll/'; - cfstatic.init( - staticDirectory = rootDir - , staticUrl = "/any/old/thing" - , minifyMode = "all" - , debugKey = "doNotLetMxUnitDebugScrewTests" - ); - try { - cfstatic.renderIncludes(); + cfstatic.init( + staticDirectory = rootDir + , staticUrl = "/any/old/thing" + , minifyMode = "all" + , debugKey = "doNotLetMxUnitDebugScrewTests" + ); + } catch ( "cfstatic.PackageCollection.badConfig" e ) { failed = true; } @@ -91,15 +87,14 @@ var failed = false; rootDir &= 'badFiles/mixedIeInAll/'; - cfstatic.init( - staticDirectory = rootDir - , staticUrl = "/any/old/thing" - , minifyMode = "all" - , debugKey = "doNotLetMxUnitDebugScrewTests" - ); try { - cfstatic.renderIncludes(); + cfstatic.init( + staticDirectory = rootDir + , staticUrl = "/any/old/thing" + , minifyMode = "all" + , debugKey = "doNotLetMxUnitDebugScrewTests" + ); } catch ( "cfstatic.PackageCollection.badConfig" e ) { failed = true;