diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 3453b2a..9f8495f 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -894,7 +894,7 @@ function drupal_get_filename($type, $name, $filename = NULL) {
     // Verify that we have an keyvalue service before using it. This is required
     // because this function is called during installation.
     // @todo Inject database connection into KeyValueStore\DatabaseStorage.
-    if (drupal_container()->hasDefinition('keyvalue') && function_exists('db_query')) {
+    if (drupal_container()->has('keyvalue') && function_exists('db_query')) {
       try {
         $file_list = state()->get('system.' . $type . '.files');
         if ($file_list && isset($file_list[$name]) && file_exists(DRUPAL_ROOT . '/' . $file_list[$name])) {
@@ -2442,7 +2442,7 @@ function drupal_container(Container $new_container = NULL, $rebuild = FALSE) {
       ->addArgument(config_get_config_directory(CONFIG_ACTIVE_DIRECTORY));
     // @todo Replace this with a cache.factory service plus 'config' argument.
     $container
-      ->register('cache.config')
+      ->register('cache.config', 'Drupal\Core\Cache\CacheBackendInterface')
       ->setFactoryClass('Drupal\Core\Cache\CacheFactory')
       ->setFactoryMethod('get')
       ->addArgument('config');
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 0f252ca..d754727 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -3901,7 +3901,7 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS
     // this is an AJAX request.
     // @todo Clean up container call.
     $container = drupal_container();
-    if ($container->has('request') && $container->has('content_negotiation')) {
+    if ($container->has('content_negotiation') && $container->isScopeActive('request')) {
       $type = $container->get('content_negotiation')->getContentType($container->get('request'));
     }
     if (!empty($items['settings']) || (!empty($type) && $type == 'ajax')) {
diff --git a/core/includes/file.inc b/core/includes/file.inc
index 977d186..5adc0bc 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -1441,32 +1441,39 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
 
   $options['key'] = in_array($options['key'], array('uri', 'filename', 'name')) ? $options['key'] : 'uri';
   $files = array();
-  if (is_dir($dir) && $handle = opendir($dir)) {
-    while (FALSE !== ($filename = readdir($handle))) {
-      if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
-        $uri = "$dir/$filename";
-        $uri = file_stream_wrapper_uri_normalize($uri);
-        if (is_dir($uri) && $options['recurse']) {
-          // Give priority to files in this folder by merging them in after any subdirectory files.
-          $files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
-        }
-        elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
-          // Always use this match over anything already set in $files with the
-          // same $$options['key'].
-          $file = new stdClass();
-          $file->uri = $uri;
-          $file->filename = $filename;
-          $file->name = pathinfo($filename, PATHINFO_FILENAME);
-          $key = $options['key'];
-          $files[$file->$key] = $file;
-          if ($options['callback']) {
-            $options['callback']($uri);
+  // Avoid warnings when opendir does not have the permissions to open a
+  // directory.
+  if (is_dir($dir)) {
+    if($handle = @opendir($dir)) {
+      while (FALSE !== ($filename = readdir($handle))) {
+        if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
+          $uri = "$dir/$filename";
+          $uri = file_stream_wrapper_uri_normalize($uri);
+          if (is_dir($uri) && $options['recurse']) {
+            // Give priority to files in this folder by merging them in after any subdirectory files.
+            $files = array_merge(file_scan_directory($uri, $mask, $options, $depth + 1), $files);
+          }
+          elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
+            // Always use this match over anything already set in $files with the
+            // same $$options['key'].
+            $file = new stdClass();
+            $file->uri = $uri;
+            $file->filename = $filename;
+            $file->name = pathinfo($filename, PATHINFO_FILENAME);
+            $key = $options['key'];
+            $files[$file->$key] = $file;
+            if ($options['callback']) {
+              $options['callback']($uri);
+            }
           }
         }
       }
-    }
 
-    closedir($handle);
+      closedir($handle);
+    }
+    else {
+      watchdog('file', '@dir can not be opened', array('@dir' => $dir), WATCHDOG_ERROR);
+    }
   }
 
   return $files;
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index da39097..1fa205d 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1464,11 +1464,9 @@ function install_bootstrap_full(&$install_state) {
   // Clear the module list that was overriden earlier in the process.
   // This will allow all freshly installed modules to be loaded.
   module_list_reset();
-  // @todo The constructor parameters for the Kernel class are for environment,
-  // e.g. 'prod', 'dev', and a boolean indicating whether it is in debug mode.
-  // Drupal does not currently make use of either of these, though that may
-  // change with http://drupal.org/node/1537198.
-  $kernel = new DrupalKernel('prod', FALSE);
+
+  // Instantiate a new kernel that will not compile the container.
+  $kernel = new DrupalKernel('prod', FALSE, NULL, FALSE);
   $kernel->boot();
   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 }
diff --git a/core/includes/module.inc b/core/includes/module.inc
index c1d99f2..76e609e 100644
--- a/core/includes/module.inc
+++ b/core/includes/module.inc
@@ -119,7 +119,7 @@ function module_list_reset() {
  * Builds a list of bootstrap modules and enabled modules and themes.
  *
  * @param $type
- *   The type of list to return:
+ *   (optional) The type of list to return:
  *   - module_enabled: All enabled modules.
  *   - bootstrap: All enabled modules required for bootstrap.
  *   - theme: All themes.
@@ -138,13 +138,13 @@ function module_list_reset() {
  *   callers like system_list() to force-disable a possible configuration
  *   storage controller cache or some other way to circumvent it/take it over.
  */
-function system_list($type) {
+function system_list($type = NULL) {
   $lists = &drupal_static(__FUNCTION__);
 
   // For bootstrap modules, attempt to fetch the list from cache if possible.
   // if not fetch only the required information to fire bootstrap hooks
   // in case we are going to serve the page from cache.
-  if ($type == 'bootstrap') {
+  if ($type === 'bootstrap') {
     if (isset($lists['bootstrap'])) {
       return $lists['bootstrap'];
     }
@@ -188,7 +188,7 @@ function system_list($type) {
         // Build a list of all enabled modules.
         $lists['module_enabled'][$name] = $name;
         // Build a list of filenames so drupal_get_filename can use it.
-        $lists['filepaths'][] = array(
+        $lists['filepaths'][$name] = array(
           'type' => 'module',
           'name' => $name,
           'filepath' => $module_files[$name],
@@ -215,7 +215,7 @@ function system_list($type) {
         $lists['theme'][$name] = $theme;
         // Build a list of filenames so drupal_get_filename can use it.
         if (isset($enabled_themes[$name])) {
-          $lists['filepaths'][] = array(
+          $lists['filepaths'][$name] = array(
             'type' => 'theme',
             'name' => $name,
             'filepath' => $theme->filename,
@@ -249,6 +249,9 @@ function system_list($type) {
         // Set the theme engine prefix.
         $lists['theme'][$key]->prefix = ($lists['theme'][$key]->info['engine'] == 'theme') ? $base_key : $lists['theme'][$key]->info['engine'];
       }
+      // Store a hash of the enabled modules for use when compiling the DIC.
+      $lists['system_list_hash'] = hash('sha256', implode(',', array_keys($lists['filepaths'])));
+
       cache('bootstrap')->set('system_list', $lists);
     }
     // To avoid a separate database lookup for the filepath, prime the
@@ -259,7 +262,7 @@ function system_list($type) {
     }
   }
 
-  return $lists[$type];
+  return $type ? $lists[$type] : $lists;
 }
 
 /**
diff --git a/core/lib/Drupal/Component/PhpStorage/FileReadOnlyStorage.php b/core/lib/Drupal/Component/PhpStorage/FileReadOnlyStorage.php
index 3d1bd95..81a155c 100644
--- a/core/lib/Drupal/Component/PhpStorage/FileReadOnlyStorage.php
+++ b/core/lib/Drupal/Component/PhpStorage/FileReadOnlyStorage.php
@@ -69,4 +69,11 @@ public function delete($name) {
   protected function getFullPath($name) {
     return $this->directory . '/' . $name;
   }
+
+  /**
+   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable().
+   */
+  function writeable() {
+    return FALSE;
+  }
 }
diff --git a/core/lib/Drupal/Component/PhpStorage/FileStorage.php b/core/lib/Drupal/Component/PhpStorage/FileStorage.php
index 80f3ec4..1f690f9 100644
--- a/core/lib/Drupal/Component/PhpStorage/FileStorage.php
+++ b/core/lib/Drupal/Component/PhpStorage/FileStorage.php
@@ -71,4 +71,11 @@ public function delete($name) {
   protected function getFullPath($name) {
     return $this->directory . '/' . $name;
   }
+
+  /**
+   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable().
+   */
+  function writeable() {
+    return TRUE;
+  }
 }
diff --git a/core/lib/Drupal/Component/PhpStorage/PhpStorageInterface.php b/core/lib/Drupal/Component/PhpStorage/PhpStorageInterface.php
index 1eaece3..8b5e271 100644
--- a/core/lib/Drupal/Component/PhpStorage/PhpStorageInterface.php
+++ b/core/lib/Drupal/Component/PhpStorage/PhpStorageInterface.php
@@ -57,6 +57,13 @@ public function load($name);
   public function save($name, $code);
 
   /**
+   * Whether this is a writeable storage.
+   *
+   * @return bool
+   */
+  public function writeable();
+
+  /**
    * Deletes PHP code from storage.
    *
    * @param string $name
diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php
index f1e43d6..aeb270c 100644
--- a/core/lib/Drupal/Core/CoreBundle.php
+++ b/core/lib/Drupal/Core/CoreBundle.php
@@ -8,6 +8,8 @@
 namespace Drupal\Core;
 
 use Drupal\Core\DependencyInjection\Compiler\RegisterKernelListenersPass;
+use Drupal\Core\DependencyInjection\Compiler\RegisterMatchersPass;
+use Drupal\Core\DependencyInjection\Compiler\RegisterNestedMatchersPass;
 use Symfony\Component\DependencyInjection\Definition;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Reference;
@@ -26,6 +28,7 @@
  */
 class CoreBundle extends Bundle
 {
+
   public function build(ContainerBuilder $container) {
 
     // The 'request' scope and service enable services to depend on the Request
@@ -61,41 +64,34 @@ public function build(ContainerBuilder $container) {
       ->addArgument(new Reference('database'))
       ->addArgument(new Reference('lock'));
 
-    $container->register('router.dumper', '\Drupal\Core\Routing\MatcherDumper')
+    $container->register('router.dumper', 'Drupal\Core\Routing\MatcherDumper')
       ->addArgument(new Reference('database'));
     $container->register('router.builder', 'Drupal\Core\Routing\RouteBuilder')
       ->addArgument(new Reference('router.dumper'));
 
-    // @todo Replace below lines with the commented out block below it when it's
-    //   performant to do so: http://drupal.org/node/1706064.
-    $dispatcher = $container->get('dispatcher');
-    $matcher = new \Drupal\Core\Routing\ChainMatcher();
-    $matcher->add(new \Drupal\Core\LegacyUrlMatcher());
-
-    $nested = new \Drupal\Core\Routing\NestedMatcher();
-    $nested->setInitialMatcher(new \Drupal\Core\Routing\PathMatcher(Database::getConnection()));
-    $nested->addPartialMatcher(new \Drupal\Core\Routing\HttpMethodMatcher());
-    $nested->setFinalMatcher(new \Drupal\Core\Routing\FirstEntryFinalMatcher());
-    $matcher->add($nested, 5);
+    $container->register('matcher', 'Drupal\Core\Routing\ChainMatcher');
+    $container->register('legacy_url_matcher', 'Drupal\Core\LegacyUrlMatcher')
+      ->addTag('chained_matcher');
+    $container->register('nested_matcher', 'Drupal\Core\Routing\NestedMatcher')
+      ->addTag('chained_matcher', array('priority' => 5));
 
-    $content_negotation = new \Drupal\Core\ContentNegotiation();
-    $dispatcher->addSubscriber(new \Symfony\Component\HttpKernel\EventListener\RouterListener($matcher));
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\ViewSubscriber($content_negotation));
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\AccessSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\PathSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\LegacyRequestSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\LegacyControllerSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\FinishResponseSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\RequestCloseSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber());
-    $dispatcher->addSubscriber(new \Drupal\Core\EventSubscriber\RouteProcessorSubscriber());
-    $container->set('content_negotiation', $content_negotation);
-    $dispatcher->addSubscriber(\Drupal\Core\ExceptionController::getExceptionListener($container));
+    // The following services are tagged as 'nested_matcher' services and are
+    // processed in the RegisterNestedMatchersPass compiler pass. Each one
+    // needs to be set on the matcher using a different method, so we use a
+    // tag attribute, 'method', which can be retrieved and passed to the
+    // addMethodCall() method that gets called on the matcher service in the
+    // compiler pass.
+    $container->register('path_matcher', 'Drupal\Core\Routing\PathMatcher')
+      ->addArgument(new Reference('database'))
+      ->addTag('nested_matcher', array('method' => 'setInitialMatcher'));
+    $container->register('http_method_matcher', 'Drupal\Core\Routing\HttpMethodMatcher')
+      ->addTag('nested_matcher', array('method' => 'addPartialMatcher'));
+    $container->register('first_entry_final_matcher', 'Drupal\Core\Routing\FirstEntryFinalMatcher')
+      ->addTag('nested_matcher', array('method' => 'setFinalMatcher'));
 
-    /*
-    $container->register('matcher', 'Drupal\Core\LegacyUrlMatcher');
-    $container->register('router_listener', 'Drupal\Core\EventSubscriber\RouterListener')
+    $container->register('router_processor_subscriber', 'Drupal\Core\EventSubscriber\RouteProcessorSubscriber')
+      ->addTag('kernel.event_subscriber');
+    $container->register('router_listener', 'Symfony\Component\HttpKernel\EventListener\RouterListener')
       ->addArgument(new Reference('matcher'))
       ->addTag('kernel.event_subscriber');
     $container->register('content_negotiation', 'Drupal\Core\ContentNegotiation');
@@ -118,15 +114,18 @@ public function build(ContainerBuilder $container) {
       ->addTag('kernel.event_subscriber');
     $container->register('request_close_subscriber', 'Drupal\Core\EventSubscriber\RequestCloseSubscriber')
       ->addTag('kernel.event_subscriber');
-    $container->register('config_global_override_subscriber', '\Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber');
+    $container->register('config_global_override_subscriber', 'Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber')
+      ->addTag('kernel.event_subscriber');
     $container->register('exception_listener', 'Drupal\Core\EventSubscriber\ExceptionListener')
       ->addTag('kernel.event_subscriber')
       ->addArgument(new Reference('service_container'))
       ->setFactoryClass('Drupal\Core\ExceptionController')
       ->setFactoryMethod('getExceptionListener');
 
+    $container->addCompilerPass(new RegisterMatchersPass());
+    $container->addCompilerPass(new RegisterNestedMatchersPass());
     // Add a compiler pass for registering event subscribers.
     $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
-    */
   }
+
 }
diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterMatchersPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterMatchersPass.php
new file mode 100644
index 0000000..e31027e
--- /dev/null
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterMatchersPass.php
@@ -0,0 +1,25 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\Core\DependencyInjection\Compiler\RegisterMatchersPass.
+ */
+
+namespace Drupal\Core\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class RegisterMatchersPass implements CompilerPassInterface {
+  public function process(ContainerBuilder $container) {
+    if (!$container->hasDefinition('matcher')) {
+      return;
+    }
+    $matcher = $container->getDefinition('matcher');
+    foreach ($container->findTaggedServiceIds('chained_matcher') as $id => $attributes) {
+      $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
+      $matcher->addMethodCall('add', array(new Reference($id), $priority));
+    }
+  }
+}
diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterNestedMatchersPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterNestedMatchersPass.php
new file mode 100644
index 0000000..7558552
--- /dev/null
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterNestedMatchersPass.php
@@ -0,0 +1,25 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\Core\DependencyInjection\Compiler\RegisterNestedMatchersPass.
+ */
+
+namespace Drupal\Core\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class RegisterNestedMatchersPass implements CompilerPassInterface {
+  public function process(ContainerBuilder $container) {
+    if (!$container->hasDefinition('nested_matcher')) {
+      return;
+    }
+    $nested = $container->getDefinition('nested_matcher');
+    foreach ($container->findTaggedServiceIds('nested_matcher') as $id => $attributes) {
+      $method = $attributes[0]['method'];
+      $nested->addMethodCall($method, array(new Reference($id)));
+    }
+  }
+}
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 869d4ee..8355da6 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -8,10 +8,12 @@
 namespace Drupal\Core;
 
 use Drupal\Core\CoreBundle;
+use Drupal\Component\PhpStorage\PhpStorageInterface;
 use Symfony\Component\HttpKernel\Kernel;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Symfony\Component\Config\Loader\LoaderInterface;
 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
+use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
 
 /**
  * The DrupalKernel class is the core of Drupal itself.
@@ -26,6 +28,55 @@
 class DrupalKernel extends Kernel {
 
   /**
+   * Holds the list of enabled modules.
+   *
+   * @var array
+   */
+  protected $systemList;
+
+  /**
+   * Whether to use a compiled Container as opposed to a ContainerBuilder.
+   *
+   * @var boolean
+   */
+  protected $useCompiledContainer;
+
+  /**
+   * PHP code storage object to use for the compiled container.
+   *
+   * @var Drupal\Component\PhpStorage\PhpStorageInterface
+   */
+  protected $storage;
+
+  /**
+   * @todo The first two constructor parameters for the Kernel class are for
+   *   environment, e.g. 'prod', 'dev', and a boolean indicating whether it is
+   *   in debug mode. Drupal does not currently make use of either of these,
+   *   though that may change with http://drupal.org/node/1537198.
+   *
+   * @param string $environment
+   * @param bool $debug
+   * @param array $system_list
+   *   The same data structure as system_list().
+   * @param bool $use_compiled_container
+   *   Whether to compile the container to disk or not.
+   */
+  public function __construct($environment, $debug, array $system_list = NULL, $use_compiled_container = TRUE) {
+    parent::__construct($environment, $debug);
+    $this->useCompiledContainer = $use_compiled_container;
+    $this->storage = drupal_php_storage('service_container');
+    if (isset($system_list)) {
+      $this->systemList = $system_list;
+    }
+    else {
+      // @todo This is a temporary measure which will no longer be necessary
+      //   once we have an ExtensionHandler for managing this list. See
+      //   http://drupal.org/node/1331486.
+      $this->systemList = system_list();
+    }
+  }
+
+  /**
    * Overrides Kernel::init().
    */
   public function init() {
@@ -43,9 +94,7 @@ public function registerBundles() {
       new CoreBundle(),
     );
 
-    // @todo Remove the necessity of calling system_list() to find out which
-    // bundles exist. See http://drupal.org/node/1331486
-    $modules = array_keys(system_list('module_enabled'));
+    $modules = array_keys($this->systemList['module_enabled']);
     foreach ($modules as $module) {
       $camelized = ContainerBuilder::camelize($module);
       $class = "Drupal\\{$module}\\{$camelized}Bundle";
@@ -59,14 +108,55 @@ public function registerBundles() {
 
   /**
    * Initializes the service container.
+   *
+   * @todo We are compiling the container and dumping to a PHP file whose name
+   *   is based on the list of enabled modules. A new file is created when this
+   *   list changes but there is currently no garbage collection in place for
+   *   the old files. See http://drupal.org/node/1759582.
    */
   protected function initializeContainer() {
-    // @todo We should be compiling the container and dumping to php so we don't
-    //   have to recompile every time. There is a separate issue for this, see
-    //   http://drupal.org/node/1668892.
-    $this->container = $this->buildContainer();
+    $this->container = NULL;
+    if ($this->useCompiledContainer) {
+      // While the default Symfony class name only depends on the environment, for
+      // testing purposes we can't use that because there would be a collision as
+      // each test method creates a new kernel. On the other hand, the container
+      // only depends on the enabled modules (and only on those that provide
+      // bundles) so we base the name of the container class on the hash of the
+      // enabled modules. We can't directly use the hash though because PHP
+      // identifiers always start with a letter and hashes don't so we add a
+      // character to the beginning. This mechanism also avoids the problem of
+      // needing to rebuild the DIC at the right time on module enable: simply on
+      // the next request the hash will change and so the container will be
+      // rebuilt.
+      $class = 'c' . $this->systemList['system_list_hash'] . ucfirst($this->environment) . ($this->debug ? 'Debug' : '');
+      $cache_file = $class . '.php';
+
+      // First, try to load.
+      if (!class_exists($class)) {
+        $this->storage->load($cache_file);
+      }
+      // If the load succeeded or the class already existed, use it.
+      if (class_exists($class)) {
+        $fully_qualified_classname = '\\' . $class;
+        $this->container = new $fully_qualified_classname;
+      }
+    }
+    if (!isset($this->container)) {
+      $this->container = $this->buildContainer();
+      if ($this->useCompiledContainer && !$this->dumpDrupalContainer($cache_file, $this->container, $class, $this->getContainerBaseClass())) {
+        // We want to log this as an error but we cannot call watchdog() until
+        // the container has been fully built and set in drupal_container().
+        $error = 'Container cannot be written to disk';
+      }
+    }
+
     $this->container->set('kernel', $this);
+
     drupal_container($this->container);
+
+    if (isset($error)) {
+      watchdog('DrupalKernel', $error);
+    }
   }
 
   /**
@@ -84,10 +174,7 @@ protected function buildContainer() {
     foreach ($this->bundles as $bundle) {
       $bundle->build($container);
     }
-
-    // @todo Compile the container: http://drupal.org/node/1706064.
-    //$container->compile();
-
+    $container->compile();
     return $container;
   }
 
@@ -101,6 +188,37 @@ protected function getContainerBuilder() {
   }
 
   /**
+   * Dumps the service container to PHP code in the config directory.
+   *
+   * This method is based on the dumpContainer method in the parent class, but
+   * that method is reliant on the Config component which we do not use here.
+   *
+   * @param string $cache_file
+   *   The full filename to write to.
+   * @param ContainerBuilder $container
+   *   The service container.
+   * @param string $class
+   *   The name of the class to generate.
+   * @param string $baseClass
+   *   The name of the container's base class
+   * @param PhpStorageInterface $storage
+   *   The PHP storage class.
+   *
+   * @return bool
+   *   TRUE if the container was successfully dumped to disk.
+   */
+  protected function dumpDrupalContainer($cache_file, ContainerBuilder $container, $class, $baseClass) {
+    if (!$this->storage->writeable()) {
+      return FALSE;
+    }
+    // Cache the container.
+    $dumper = new PhpDumper($container);
+    $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass));
+
+    return $this->storage->save($cache_file, $content);
+  }
+
+  /**
    * Overrides and eliminates this method from the parent class. Do not use.
    *
    * This method is part of the KernelInterface interface, but takes an object
diff --git a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php
index 8065349..041c0f1 100644
--- a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\Core\EventSubscriber;
 
+use Drupal\Core\Language\LanguageManager;
 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
 use Symfony\Component\HttpKernel\KernelEvents;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -17,6 +18,21 @@
 class FinishResponseSubscriber implements EventSubscriberInterface {
 
   /**
+   * @var LanguageManager
+   *   The LanguageManager object for retrieving the correct language code.
+   */
+  protected $languageManager;
+
+  /**
+   * Constructs a FinishResponseSubscriber object.
+   *
+   * @param LanguageManager $language_manager
+   */
+  public function __construct(LanguageManager $language_manager) {
+    $this->languageManager = $language_manager;
+  }
+
+  /**
    * Sets extra headers on successful responses.
    *
    * @param Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
@@ -30,10 +46,7 @@ public function onRespond(FilterResponseEvent $event) {
     $response->headers->set('X-UA-Compatible', 'IE=edge,chrome=1', false);
 
     // Set the Content-language header.
-    // @todo Receive the LanguageManager object as a constructor argument when
-    //   the dependency injection container allows for it performantly:
-    //   http://drupal.org/node/1706064.
-    $response->headers->set('Content-language', language(LANGUAGE_TYPE_INTERFACE)->langcode);
+    $response->headers->set('Content-language', $this->languageManager->getLanguage(LANGUAGE_TYPE_INTERFACE)->langcode);
 
     // Because pages are highly dynamic, set the last-modified time to now
     // since the page is in fact being regenerated right now.
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index b31d8a0..678fefd 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -846,7 +846,7 @@ protected function rebuildContainer() {
     // container in drupal_container(). Drupal\simpletest\TestBase::tearDown()
     // restores the original container.
     // @see Drupal\Core\DrupalKernel::initializeContainer()
-    $this->kernel = new DrupalKernel('testing', FALSE);
+    $this->kernel = new DrupalKernel('testing', FALSE, NULL, FALSE);
     // Booting the kernel is necessary to initialize the new DIC. While
     // normally the kernel gets booted on demand in
     // Symfony\Component\HttpKernel\handle(), this kernel needs manual booting
diff --git a/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php b/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php
new file mode 100644
index 0000000..73dad87
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\system\Tests\DrupalKernel\DrupalKernelTest.
+ */
+
+namespace Drupal\system\Tests\DrupalKernel;
+
+use Drupal\Core\DrupalKernel;
+use Drupal\simpletest\UnitTestBase;
+use ReflectionClass;
+
+/**
+ * Test compilation of the DIC.
+ */
+class DrupalKernelTest extends UnitTestBase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'DrupalKernel tests',
+      'description' => 'Tests DIC compilation to disk.',
+      'group' => 'DrupalKernel',
+    );
+  }
+
+  /**
+   * Test DIC compilation.
+   */
+  function testCompileDIC() {
+    // Because we'll be instantiating a new kernel during this test, the
+    // container stored in drupal_container() will be updated as a side effect.
+    // We need to be able to restore it to the correct one at the end of this
+    // test.
+    $original_container = drupal_container();
+    global $conf;
+    $conf['php_storage']['service_container'] = array(
+      'class' => 'Drupal\Component\PhpStorage\MTimeProtectedFileStorage',
+      'secret' => $GLOBALS['drupal_hash_salt'],
+    );
+    $module_enabled = array(
+      'system' => 'system',
+      'user' => 'user',
+    );
+    $module_enabled_hash = hash('sha256', implode(',', array_keys($module_enabled)));
+    $system_list = array(
+      'module_enabled' => $module_enabled,
+      'module_enabled_hash' => $module_enabled_hash,
+    );
+    $kernel = new DrupalKernel('testing', FALSE, $system_list);
+    $kernel->boot();
+    // Instantiate it a second time and we should get the compiled Container
+    // class.
+    $kernel = new DrupalKernel('testing', FALSE, $system_list);
+    $kernel->boot();
+    $container = $kernel->getContainer();
+    $refClass = new ReflectionClass($container);
+    $is_compiled_container =
+      $refClass->getParentClass()->getName() == 'Symfony\Component\DependencyInjection\Container' &&
+      !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
+    $this->assertTrue($is_compiled_container);
+
+    // Reset the container.
+    drupal_container(NULL, TRUE);
+
+    // Now use the read-only storage implementation, simulating a "production"
+    // environment.
+    drupal_static_reset('drupal_php_storage');
+    $conf['php_storage']['service_container'] = array(
+      'class' => 'Drupal\Component\PhpStorage\FileReadOnlyStorage',
+    );
+    $kernel = new DrupalKernel('testing', FALSE, $system_list);
+    $kernel->boot();
+    $container = $kernel->getContainer();
+    $refClass = new ReflectionClass($container);
+    $is_compiled_container =
+      $refClass->getParentClass()->getName() == 'Symfony\Component\DependencyInjection\Container' &&
+      !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
+    $this->assertTrue($is_compiled_container);
+
+    // We make this assertion here purely to show that the new container below
+    // is functioning correctly, i.e. we get a brand new ContainerBuilder
+    // which has the required new services, after changing the list of enabled
+    // modules.
+    $this->assertFalse($container->has('bundle_test_class'));
+
+    // Reset the container.
+    drupal_container(NULL, TRUE);
+
+    // Add another module so that a different hash is used for the class name
+    // and we can test that the new module's bundle is registered to the new
+    // container.
+    $module_enabled = array(
+      'system' => 'system',
+      'user' => 'user',
+      'bundle_test' => 'bundle_test',
+    );
+    $module_enabled_hash = hash('sha256', implode(',', array_keys($module_enabled)));
+    $system_list = array(
+      'module_enabled' => $module_enabled,
+      'module_enabled_hash' => $module_enabled_hash,
+    );
+    $kernel = new DrupalKernel('testing', FALSE, $system_list);
+    $kernel->boot();
+    // Instantiate it a second time and we should still get a ContainerBuilder
+    // class because we are using the read-only PHP storage.
+    $kernel = new DrupalKernel('testing', FALSE, $system_list);
+    $kernel->boot();
+    $container = $kernel->getContainer();
+    $refClass = new ReflectionClass($container);
+    $is_container_builder = $refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
+    $this->assertTrue($is_container_builder);
+    // Assert that the new module's bundle was registered to the new container.
+    $this->assertTrue($container->has('bundle_test_class'));
+
+    // Restore the original container.
+    drupal_container($original_container);
+  }
+}
diff --git a/core/modules/system/tests/modules/bundle_test/lib/Drupal/bundle_test/BundleTestBundle.php b/core/modules/system/tests/modules/bundle_test/lib/Drupal/bundle_test/BundleTestBundle.php
index 97b79c1..c2db16a 100644
--- a/core/modules/system/tests/modules/bundle_test/lib/Drupal/bundle_test/BundleTestBundle.php
+++ b/core/modules/system/tests/modules/bundle_test/lib/Drupal/bundle_test/BundleTestBundle.php
@@ -23,9 +23,5 @@ public function build(ContainerBuilder $container) {
 
     // Override a default bundle used by core to a dummy class.
     $container->register('file.usage', 'Drupal\bundle_test\TestFileUsage');
-
-    // @todo Remove when the 'kernel.event_subscriber' tag above is made to
-    //   work: http://drupal.org/node/1706064.
-    $container->get('dispatcher')->addSubscriber($container->get('bundle_test_class'));
   }
 }
diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index 2395a84..5aa8b22 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -479,7 +479,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
     // simpletest_clean_temporary_directories() cannot be used here, since it
     // would also delete file directories of other tests that are potentially
     // running concurrently.
-    file_unmanaged_delete_recursive($test_directory);
+    file_unmanaged_delete_recursive($test_directory, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback'));
     $messages[] = "- Removed test files directory.";
   }
 
