Make WordPress Core

Ticket #35667: 35667.2.diff

File 35667.2.diff, 2.8 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                if ( ! isset( $this->count_cache[ $string1 ] ) ) {
     451                        $this->count_cache[ $string1 ] = count_chars($string1);
     452                }
     453                if ( ! isset( $this->count_cache[ $string2 ] ) ) {
     454                        $this->count_cache[ $string2 ] = count_chars($string2);
     455                }
     456
     457                $chars1 = $this->count_cache[ $string1 ];
     458                $chars2 = $this->count_cache[ $string2 ];
     459
     460                $difference_key = implode( ',', $chars1 ) . ':' . implode( ',', $chars2 );
     461                if ( ! isset( $this->difference_cache[ $difference_key ] ) ) {
     462                        // L1-norm of difference vector.
     463                        $this->difference_cache[ $difference_key ] = array_sum( array_map( array($this, 'difference'), $chars1, $chars2 ) );
     464                }
     465
     466                $difference = $this->difference_cache[ $difference_key ];
    440467
    441468                // L1-norm of difference vector.
    442469                $difference = array_sum( array_map( array( $this, 'difference' ), $chars1, $chars2 ) );