Make WordPress Core

Ticket #35667: 35667.4.diff

File 35667.4.diff, 3.0 KB (added by pento, 8 years ago)
  • src/wp-includes/class-wp-text-diff-renderer-table.php

     
    5656        protected $compat_fields = array( '_show_split_view', 'inline_diff_renderer', '_diff_threshold' );
    5757
    5858        /**
     59         * Caches the output of count_chars() in compute_string_distance()
     60         *
     61         * @var array
     62         * @since 5.0.0
     63         */
     64        protected $count_cache = array();
     65
     66        /**
     67         * Caches the difference calculation in compute_string_distance()
     68         *
     69         * @var array
     70         * @since 5.0.0
     71         */
     72        protected $difference_cache = array();
     73
     74        /**
    5975         * Constructor - Call parent constructor with params array.
    6076         *
    6177         * This will set class properties based on the key value pairs in the array.
    … …  
    396412                        if ( false === $final_pos ) { // This orig is paired with a blank final.
    397413                                array_splice( $final_rows, $orig_pos, 0, -1 );
    398414                        } elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.
    399                                 $diff_pos = $final_pos - $orig_pos;
    400                                 while ( $diff_pos < 0 ) {
    401                                         array_splice( $final_rows, $orig_pos, 0, $diff_pos++ );
    402                                 }
     415                                $diff_array = range( -1, $final_pos - $orig_pos );
     416                                array_splice( $final_rows, $orig_pos, 0, $diff_array );
    403417                        } elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.
    404                                 $diff_pos = $orig_pos - $final_pos;
    405                                 while ( $diff_pos < 0 ) {
    406                                         array_splice( $orig_rows, $orig_pos, 0, $diff_pos++ );
    407                                 }
     418                                $diff_array = range( -1, $orig_pos - $final_pos );
     419                                array_splice( $orig_rows, $orig_pos, 0, $diff_array );
    408420                        }
    409421                }
    410422
    … …  
    435447         */
    436448        public function compute_string_distance( $string1, $string2 ) {
    437449                // Vectors containing character frequency for all chars in each string
    438                 $chars1 = count_chars( $string1 );
    439                 $chars2 = count_chars( $string2 );
     450                $count_key1 = md5( $string1 );
     451                $count_key2 = md5( $string2 );
     452
     453                if ( ! isset( $this->count_cache[ $count_key1 ] ) ) {
     454                        $this->count_cache[ $count_key1 ] = count_chars( $string1 );
     455                }
     456                if ( ! isset( $this->count_cache[ $count_key2 ] ) ) {
     457                        $this->count_cache[ $count_key2 ] = count_chars( $string2 );
     458                }
     459
     460                $chars1 = $this->count_cache[ $count_key1 ];
     461                $chars2 = $this->count_cache[ $count_key2 ];
     462
     463                $difference_key = md5( implode( ',', $chars1 ) . ':' . implode( ',', $chars2 ) );
     464                if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
     465                        // L1-norm of difference vector.
     466                        $this->difference_cache[ $difference_key ] = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
     467                }
    440468
    441                 // L1-norm of difference vector.
    442                 $difference = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );
     469                $difference = $this->difference_cache[ $difference_key ];
    443470
    444471                // $string1 has zero length? Odd. Give huge penalty by not dividing.
    445472                if ( ! $string1 ) {