Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#21332 closed enhancement (fixed)

Improve speed of add_query_arg()

Reported by: nacin Owned by: nacin
Milestone: 3.5 Priority: normal
Severity: normal Version:
Component: Performance Keywords: has-patch
Focuses: Cc:


On my local install on edit.php, add_query_arg() is called 186 times, but it ends up being nearly 6% of total time, per some profiling by both me and kurtpayne. I've seen it called far more times on other pages. I've known it to be slow (not perceptually, simply from profiling outputs) and have avoided using it. Not fun for a utility function.

What may normally be called micro-optimizations could have some impact at this scale. End result, the function is nearly twice as fast:

  • Remove error-suppression operators from func_get_args() and func_get_arg() — they're unnecessary, as these functions will never emit an error in this context. These were introduced in [1823] and proliferated since then. Fixing this alone is about one-third of the speed-up.
  • Avoid func_get_args() and func_get_arg() as much as possible, loading func_get_args() once as $args.
  • Smaller stuff: Remove unnecessary preg_match() and strlen() when trying to detect and chop off the protocol; don't cast a known array to an array.

We could also switch from wp_parse_str() to parse_str(), effectively reverting the first piece in [5709/trunk/wp-includes/functions.php]. I see no reason to be calling the wp_parse_str filter here in a utility function, though it's been around for a while already and it's not like apply_filters() is slow.

Attachments (2)

21332.patch (1.3 KB) - added by SergeyBiryukov 3 years ago.
21332-ut.diff (2.5 KB) - added by ryan 3 years ago.
Quick unit tests

Download all attachments as: .zip

Change History (8)

comment:1 @nacin3 years ago

In [21298]:

Remove unnecessary error suppression operators from func_get_arg() and func_num_args(). see #21332.

comment:2 @kurtpayne3 years ago

  • Cc kpayne@… added

@SergeyBiryukov3 years ago


@ryan3 years ago

Quick unit tests

comment:4 @ryan3 years ago

In [21373]:

Call func_get_args() once in add_query_arg(). Props SergeyBiryukov. see #21332

comment:5 @wonderboymusic3 years ago

  • Keywords has-patch added

comment:6 @nacin3 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [21865]:

Simplify protocol stripping in add_query_arg() by avoiding a regular expression. Don't cast a known array to an array. fixes #21332.

Note: See TracTickets for help on using tickets.