Speed up testing against IUSE by not using regexp

When trying to figure out why it took so long to do a no-op kernel
build (re-build when nothing changed) on Chrome OS, I tracked down one
slowdown to cros-kernel2_src_configure().  This function was taking
~900 ms to execute.

The bulk of that slowdown was in iterating over the list of config
fragments, specifically the "use ${fragment}" test.  We currently have
77 fragments so we were effectively calling the "use" function 77
times.

Digging through the portage code, the slow part of the "use" function
was the block of code to confirm that you specified each USE flag in
your IUSE.  Commenting out the whole "elif" block of code there sped
things up so that the entire cros-kernel2_src_configure() was now
taking ~130 ms.  This means that each call to the "use" function was
taking about 10 ms.

The specific part of the test that was slow was testing against the
regular expression.  It was specifically slow in the Chrome OS kernel
build because we inherit the "cros-board" eclass which populates a
huge number of boards in the USE flag, making the regular expression
totally unwieldly.

One way to speed this whole thing up is to use a bash associative
array.  Unfortunately arrays can't come in through environment
variables, so we'll write a function that declares the array the first
time it's needed.

With this version of the code cros-kernel2_src_configure() now takes
~190 ms which seems like it's OK.  AKA 77 checks against IUSE took 60
ms or less than 1 ms per check.

NOTE: to keep EAPI 4 and older working, we keep doing the regular
expression tests there, though we now do it in the __in_portage_iuse()
function.  In at least one test the extra overhead of the function
made testing USE flags on EAPI 4 ~15% slower, but presumably this is
OK as we want to encourage folks to move to the newer EAPIs.

BUG=chromium:767073
TEST=Time some builds; confirm bad use flags still caught.

Change-Id: Ic74fa49bdf002399ba0d6c41f42d4632b07127a9
Reviewed-on: https://chromium-review.googlesource.com/1524641
Commit-Ready: Douglas Anderson <dianders@chromium.org>
Tested-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
4 files changed