@@ -376,7 +376,6 @@ void KernelLoader::InitializeFields(UriToSourceTable* uri_to_source_table) {
376376 const intptr_t source_table_size = helper_.SourceTableSize ();
377377 const Array& scripts =
378378 Array::Handle (Z, Array::New (source_table_size, Heap::kOld ));
379- patch_classes_ = Array::New (source_table_size, Heap::kOld );
380379
381380 // Copy the Kernel string offsets out of the binary and into the VM's heap.
382381 ASSERT (program_->string_table_offset () >= 0 );
@@ -489,11 +488,7 @@ KernelLoader::KernelLoader(const Script& script,
489488 expression_evaluation_library_(Library::Handle(Z)) {
490489 ASSERT (T.active_class_ == &active_class_);
491490 T.finalize_ = false ;
492-
493- const Array& scripts = Array::Handle (Z, kernel_program_info_.scripts ());
494- patch_classes_ = Array::New (scripts.Length (), Heap::kOld );
495491 library_kernel_data_ = kernel_data.raw ();
496-
497492 H.InitFromKernelProgramInfo (kernel_program_info_);
498493}
499494
@@ -2107,6 +2102,13 @@ const Object& KernelLoader::ClassForScriptAt(const Class& klass,
21072102 intptr_t source_uri_index) {
21082103 const Script& correct_script = Script::Handle (Z, ScriptAt (source_uri_index));
21092104 if (klass.script () != correct_script.raw ()) {
2105+ // Lazily create the [patch_classes_] array in case we need it.
2106+ if (patch_classes_.IsNull ()) {
2107+ const Array& scripts = Array::Handle (Z, kernel_program_info_.scripts ());
2108+ ASSERT (!scripts.IsNull ());
2109+ patch_classes_ = Array::New (scripts.Length (), Heap::kOld );
2110+ }
2111+
21102112 // Use cache for patch classes. This works best for in-order usages.
21112113 PatchClass& patch_class = PatchClass::ZoneHandle (Z);
21122114 patch_class ^= patch_classes_.At (source_uri_index);
0 commit comments