Permalink
Browse files

Reducing memory usage

  • Loading branch information...
peteboere committed Jun 23, 2012
1 parent ea5ed5c commit 403a5a59a733ed638e7f1e4d4281dce3bad58caf
Showing with 35 additions and 42 deletions.
  1. +26 −37 lib/Core.php
  2. +3 −1 lib/Hook.php
  3. +6 −4 lib/Importer.php
View
@@ -640,7 +640,7 @@ protected static function calculateVariables () {
}
}
- protected static function placeVariables ( $stream ) {
+ protected static function placeVariables ( &$stream ) {
// Substitute simple case variables
$stream = preg_replace_callback(
@@ -657,12 +657,11 @@ protected static function placeVariables ( $stream ) {
if ( strpos( $string, '$(' ) !== false ) {
$string = preg_replace_callback(
- csscrush_regex::$patt->varFunction,
+ csscrush_regex::$patt->varFunction,
array( 'self', 'cb_placeVariables' ), $string );
csscrush_function::executeCustomFunctions( $string, $var_fn_patt, $var_fn_callback );
}
}
- return $stream;
}
protected static function reset ( $options = null ) {
@@ -709,22 +708,19 @@ protected static function compile ( $stream ) {
self::pruneAliases();
// Parse variables
- $stream = self::extractVariables( $stream );
+ self::extractVariables( $stream );
// Calculate the variable stack
self::calculateVariables();
// Place the variables
- $stream = self::placeVariables( $stream );
+ self::placeVariables( $stream );
// Pull out the mixin declarations
- $stream = self::extractMixins( $stream );
+ self::extractMixins( $stream );
// Pull out the fragments
- $stream = self::extractFragments( $stream );
-
- // Normalize whitespace
- $stream = csscrush_util::normalizeWhiteSpace( $stream );
+ self::extractFragments( $stream );
// Adjust the stream so we can extract the rules cleanly
$map = array(
@@ -736,21 +732,21 @@ protected static function compile ( $stream ) {
$stream = "\n" . str_replace( array_keys( $map ), array_values( $map ), $stream );
// Rules
- $stream = self::extractRules( $stream );
+ self::extractRules( $stream );
// Process any @-in blocks
- $stream = self::prefixSelectors( $stream );
+ self::prefixSelectors( $stream );
// Main processing on the rule objects
self::processRules();
// csscrush::log( array_keys( self::$process->selectorRelationships ) );
// Alias any @-rules
- $stream = self::aliasAtRules( $stream );
+ self::aliasAtRules( $stream );
// Print it all back
- $stream = self::display( $stream );
+ self::display( $stream );
// Add in boilerplate
if ( $options[ 'boilerplate' ] ) {
@@ -763,7 +759,7 @@ protected static function compile ( $stream ) {
return $stream;
}
- protected static function display ( $stream ) {
+ protected static function display ( &$stream ) {
$minify = ! self::$options[ 'debug' ];
$regex = csscrush_regex::$patt;
@@ -823,8 +819,6 @@ protected static function display ( $stream ) {
// Insert string literals
$stream = csscrush_util::strReplaceHash( $stream, self::$storage->tokens->strings );
- // I think we're done
- return $stream;
}
protected static function minify ( $str ) {
@@ -843,10 +837,10 @@ protected static function minify ( $str ) {
array_keys( $replacements ), array_values( $replacements ), $str );
}
- protected static function aliasAtRules ( $stream ) {
+ protected static function aliasAtRules ( &$stream ) {
if ( empty( self::$config->aliases[ 'at-rules' ] ) ) {
- return $stream;
+ return;
}
$aliases = self::$config->aliases[ 'at-rules' ];
@@ -938,10 +932,9 @@ protected static function aliasAtRules ( $stream ) {
} // while
} // foreach
- return $stream;
}
- protected static function prefixSelectors ( $stream ) {
+ protected static function prefixSelectors ( &$stream ) {
$matches = csscrush_regex::matchAll( '@in\s+([^\{]+){', $stream, true );
@@ -1019,8 +1012,6 @@ protected static function prefixSelectors ( $stream ) {
// Concatenate
$stream = $before . $curly_match->inside . $curly_match->after;
}
-
- return $stream;
}
public static function tokenLabelCreate ( $prefix ) {
@@ -1041,6 +1032,7 @@ public static function processRules () {
csscrush_hook::run( 'rule_prealias', $rule );
if ( ! empty( self::$config->aliases ) ) {
+
$rule->addPropertyAliases();
$rule->addFunctionAliases();
$rule->addValueAliases();
@@ -1243,27 +1235,27 @@ protected static function cb_printRule ( $match ) {
############
# Parsing methods
- public static function extractRules ( $stream ) {
- return preg_replace_callback( csscrush_regex::$patt->rule, array( 'self', 'cb_extractRules' ), $stream );
+ public static function extractRules ( &$stream ) {
+ $stream = preg_replace_callback( csscrush_regex::$patt->rule, array( 'self', 'cb_extractRules' ), $stream );
}
- public static function extractVariables ( $stream ) {
- return preg_replace_callback( csscrush_regex::$patt->variables, array( 'self', 'cb_extractVariables' ), $stream );
+ public static function extractVariables ( &$stream ) {
+ $stream = preg_replace_callback( csscrush_regex::$patt->variables, array( 'self', 'cb_extractVariables' ), $stream );
}
- public static function extractComments ( $stream ) {
- return preg_replace_callback( csscrush_regex::$patt->comment, array( 'self', 'cb_extractComments' ), $stream );
+ public static function extractComments ( &$stream ) {
+ $stream = preg_replace_callback( csscrush_regex::$patt->comment, array( 'self', 'cb_extractComments' ), $stream );
}
- public static function extractStrings ( $stream ) {
- return preg_replace_callback( csscrush_regex::$patt->string, array( 'self', 'cb_extractStrings' ), $stream );
+ public static function extractStrings ( &$stream ) {
+ $stream = preg_replace_callback( csscrush_regex::$patt->string, array( 'self', 'cb_extractStrings' ), $stream );
}
- public static function extractMixins ( $stream ) {
- return preg_replace_callback( csscrush_regex::$patt->mixin, array( 'self', 'cb_extractMixins' ), $stream );
+ public static function extractMixins ( &$stream ) {
+ $stream = preg_replace_callback( csscrush_regex::$patt->mixin, array( 'self', 'cb_extractMixins' ), $stream );
}
- public static function extractFragments ( $stream ) {
+ public static function extractFragments ( &$stream ) {
$matches = csscrush_regex::matchAll( '@fragment\s+(<name>)\s*{', $stream, true );
@@ -1348,10 +1340,7 @@ public static function extractFragments ( $stream ) {
$stream = $before . $fragment_return . $after;
}
}
-
- return $stream;
}
-
}
View
@@ -10,15 +10,17 @@ class csscrush_hook {
static public $record = array();
static public function add ( $hook, $fn_name ) {
+
// Store in associative array so no duplicates
if ( function_exists( $fn_name ) ) {
self::$record[ $hook ][ $fn_name ] = true;
}
}
static public function run ( $hook, $arg_obj ) {
+
// Run all callbacks attached to the hook
- if ( !isset( self::$record[ $hook ] ) ) {
+ if ( ! isset( self::$record[ $hook ] ) ) {
return;
}
foreach ( array_keys( self::$record[ $hook ] ) as $fn_name ) {
View
@@ -52,8 +52,9 @@ public static function hostfile () {
$stream = file_get_contents( $prependFile ) . $stream;
}
- $stream = csscrush::extractComments( $stream );
- $stream = csscrush::extractStrings( $stream );
+ csscrush::extractComments( $stream );
+ csscrush::extractStrings( $stream );
+ $stream = csscrush_util::normalizeWhiteSpace( $stream );
// If rewriting URLs as absolute we need to do some extra work
if ( $options[ 'rewrite_import_urls' ] === 'absolute' ) {
@@ -142,8 +143,9 @@ public static function hostfile () {
// they will be brought inline with the hostfile
// Start with extracting strings and comments in the import
- $import->content = csscrush::extractComments( $import->content );
- $import->content = csscrush::extractStrings( $import->content );
+ csscrush::extractComments( $import->content );
+ csscrush::extractStrings( $import->content );
+ $import->content = csscrush_util::normalizeWhiteSpace( $import->content );
$import->dir = dirname( $import->url );

0 comments on commit 403a5a5

Please sign in to comment.