[wp-trac] [WordPress Trac] #14889: Memory Comparison Broken / WordPress Memory Limit

WordPress Trac wp-trac at lists.automattic.com
Wed Jun 8 12:41:05 UTC 2011


#14889: Memory Comparison Broken / WordPress Memory Limit
-----------------------------------------------+---------------------------
 Reporter:  hakre                              |       Owner:
     Type:  defect (bug)                       |      Status:  new
 Priority:  normal                             |   Milestone:  Future
Component:  General                            |  Release
 Severity:  normal                             |     Version:  3.0
 Keywords:  has-patch 3.2-early needs-testing  |  Resolution:
-----------------------------------------------+---------------------------

Comment (by hakre):

 I had the idea of using a string as a map to calculate the values but
 didn't know if it would work out. I could test now the following routine
 while I was testing for #17725:

 {{{
 function wp_convert_hr_to_bytes( $size ) {
         $bytes = (float) $size;
         $last = strtolower( substr( $size, -1 ) );
         $pos = strpos( ' kmg', $last , 1);
         if ( $pos ) $bytes *= pow( 1024, $pos);
         return round( $bytes );
 }
 }}}

 It uses the proposed lookup with substr(-1) and the pow() function based
 on the mapping. strpos will return false if `'k'`, `'m'` or `'g'` is not
 found in `$last`. Otherwise `$pos` is the exponent of 1024.

 I could not run a speed comparison so far, however it reduces the number
 of ifs and I think strpos is quite fast, probably even faster than looking
 up key hashes in the array structure.

 I added a round() at the end as there are no half bytes. The function will
 always return a float.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/14889#comment:27>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list