[wp-trac] [WordPress Trac] #33898: Reduce Customizer peak memory usage by JSON-encoding settings and controls separately

WordPress Trac noreply at wordpress.org
Wed Sep 16 18:33:59 UTC 2015


#33898: Reduce Customizer peak memory usage by JSON-encoding settings and controls
separately
-------------------------+-----------------
 Reporter:  westonruter  |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  4.4
Component:  Customize    |    Version:  3.4
 Severity:  normal       |   Keywords:
  Focuses:  performance  |
-------------------------+-----------------
 In working on a site with a lot of settings and controls in the
 Customizer, I found that peak memory usage (via `memory_get_peak_usage()`)
 can skyrocket, even to the point where memory usage would come up against
 the `memory_limit` and cause a fatal error. In investigating why, I found
 the culprit to be this line in `wp-admin/customize.php`:

 {{{#!php
 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
 }}}

 Given a site that has 100 instances of a given widget that has 500 fields
 in it (see [https://gist.github.com/westonruter/180251bb5359202995e5
 example widget code]), the metrics are:

 * Initial `_wpCustomizeSettings` JSON size: 14,315,157 bytes
 * `memory_get_usage()`: 18,812,904
 * `memory_get_peak_usage()`: 47,674,256

 If, however, all settings and controls are looped over and serialized
 separately, these are the metrics:

 * Initial `_wpCustomizeSettings` JSON size: 6,803 bytes
 * `memory_get_usage()`: 18,812,904
 * `memory_get_peak_usage()`: 19,980,976

 In both cases, the PHP render time was 0.5 seconds, so doing multiple
 `wp_json_encode()` calls does not add significant processing time compared
 with a single `wp_json_encode()`. The improvements to memory are dramatic,
 however. But when serializing all of the data together, the peak memory
 usage is 140% larger (2.4× higher) than when the settings and controls get
 serialized individually.

 In other words, by serializing settings and controls individually, we can
 cut peak memory usage more and less in half, depending on how many
 settings and controls are registered the Customizer.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33898>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list