Revision: 22276
Author:   [email protected]
Date:     Tue Jul  8 09:04:08 2014 UTC
Log:      Introduce code serializer/deserializer.

[email protected]

Review URL: https://codereview.chromium.org/373713006
http://code.google.com/p/v8/source/detail?r=22276

Modified:
 /branches/bleeding_edge/src/assembler.h
 /branches/bleeding_edge/src/compiler.cc
 /branches/bleeding_edge/src/flag-definitions.h
 /branches/bleeding_edge/src/full-codegen.cc
 /branches/bleeding_edge/src/mksnapshot.cc
 /branches/bleeding_edge/src/parser.cc
 /branches/bleeding_edge/src/parser.h
 /branches/bleeding_edge/src/serialize.cc
 /branches/bleeding_edge/src/serialize.h
 /branches/bleeding_edge/src/snapshot-source-sink.cc
 /branches/bleeding_edge/src/snapshot-source-sink.h
 /branches/bleeding_edge/src/version.h
 /branches/bleeding_edge/test/cctest/test-compiler.cc

=======================================
--- /branches/bleeding_edge/src/assembler.h     Mon Jul  7 09:57:29 2014 UTC
+++ /branches/bleeding_edge/src/assembler.h     Tue Jul  8 09:04:08 2014 UTC
@@ -66,6 +66,7 @@
   void set_emit_debug_code(bool value) { emit_debug_code_ = value; }

   bool serializer_enabled() const { return serializer_enabled_; }
+  void enable_serializer() { serializer_enabled_ = true; }

   bool predictable_code_size() const { return predictable_code_size_; }
void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
=======================================
--- /branches/bleeding_edge/src/compiler.cc     Mon Jun 30 13:25:46 2014 UTC
+++ /branches/bleeding_edge/src/compiler.cc     Tue Jul  8 09:04:08 2014 UTC
@@ -933,9 +933,11 @@
     cached_data = NULL;
   } else if (cached_data_mode == PRODUCE_CACHED_DATA) {
     ASSERT(cached_data && !*cached_data);
+    ASSERT(extension == NULL);
   } else {
     ASSERT(cached_data_mode == CONSUME_CACHED_DATA);
     ASSERT(cached_data && *cached_data);
+    ASSERT(extension == NULL);
   }
   Isolate* isolate = source->GetIsolate();
   int source_length = source->length();
@@ -951,6 +953,11 @@
     maybe_result = compilation_cache->LookupScript(
         source, script_name, line_offset, column_offset,
         is_shared_cross_origin, context);
+    if (maybe_result.is_null() && FLAG_serialize_toplevel &&
+        cached_data_mode == CONSUME_CACHED_DATA) {
+      Object* des = CodeSerializer::Deserialize(isolate, *cached_data);
+      return handle(SharedFunctionInfo::cast(des), isolate);
+    }
   }

   if (!maybe_result.ToHandle(&result)) {
@@ -971,17 +978,21 @@
     // Compile the function and add it to the cache.
     CompilationInfoWithZone info(script);
     info.MarkAsGlobal();
+    info.SetCachedData(cached_data, cached_data_mode);
     info.SetExtension(extension);
-    info.SetCachedData(cached_data, cached_data_mode);
     info.SetContext(context);
     if (FLAG_use_strict) info.SetStrictMode(STRICT);
+
     result = CompileToplevel(&info);
     if (extension == NULL && !result.is_null() && !result->dont_cache()) {
       compilation_cache->PutScript(source, context, result);
+ if (FLAG_serialize_toplevel && cached_data_mode == PRODUCE_CACHED_DATA) {
+        *cached_data = CodeSerializer::Serialize(result);
+      }
     }
     if (result.is_null()) isolate->ReportPendingMessages();
   } else if (result->ic_age() != isolate->heap()->global_ic_age()) {
-      result->ResetForNewContext(isolate->heap()->global_ic_age());
+    result->ResetForNewContext(isolate->heap()->global_ic_age());
   }
   return result;
 }
=======================================
--- /branches/bleeding_edge/src/flag-definitions.h Tue Jul 8 08:28:08 2014 UTC +++ /branches/bleeding_edge/src/flag-definitions.h Tue Jul 8 09:04:08 2014 UTC
@@ -423,6 +423,8 @@
 DEFINE_BOOL(trace_stub_failures, false,
             "trace deoptimization of generated code stubs")

+DEFINE_BOOL(serialize_toplevel, false, "enable caching of toplevel scripts")
+
 // compiler.cc
 DEFINE_INT(min_preparse_length, 1024,
            "minimum length for automatic enable preparsing")
=======================================
--- /branches/bleeding_edge/src/full-codegen.cc Wed Jun 25 15:26:10 2014 UTC
+++ /branches/bleeding_edge/src/full-codegen.cc Tue Jul  8 09:04:08 2014 UTC
@@ -301,6 +301,11 @@
   CodeGenerator::MakeCodePrologue(info, "full");
   const int kInitialBufferSize = 4 * KB;
   MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize);
+  if (FLAG_serialize_toplevel &&
+ info->cached_data_mode() == PRODUCE_CACHED_DATA && info->is_global()) {
+    masm.enable_serializer();
+  }
+
 #ifdef ENABLE_GDB_JIT_INTERFACE
   masm.positions_recorder()->StartGDBJITLineInfoRecording();
 #endif
=======================================
--- /branches/bleeding_edge/src/mksnapshot.cc   Thu Jul  3 07:57:29 2014 UTC
+++ /branches/bleeding_edge/src/mksnapshot.cc   Tue Jul  8 09:04:08 2014 UTC
@@ -32,17 +32,6 @@
 };


-class ListSnapshotSink : public i::SnapshotByteSink {
- public:
-  explicit ListSnapshotSink(i::List<char>* data) : data_(data) { }
-  virtual ~ListSnapshotSink() {}
-  virtual void Put(int byte, const char* description) { data_->Add(byte); }
-  virtual int Position() { return data_->length(); }
- private:
-  i::List<char>* data_;
-};
-
-
 class SnapshotWriter {
  public:
   explicit SnapshotWriter(const char* snapshot_file)
@@ -93,7 +82,7 @@
       return;

     i::List<char> startup_blob;
-    ListSnapshotSink sink(&startup_blob);
+    i::ListSnapshotSink sink(&startup_blob);

     int spaces[] = {
i::NEW_SPACE, i::OLD_POINTER_SPACE, i::OLD_DATA_SPACE, i::CODE_SPACE,
@@ -417,12 +406,12 @@
// This results in a somewhat smaller snapshot, probably because it gets
     // rid of some things that are cached between garbage collections.
     i::List<char> snapshot_data;
-    ListSnapshotSink snapshot_sink(&snapshot_data);
+    i::ListSnapshotSink snapshot_sink(&snapshot_data);
     i::StartupSerializer ser(internal_isolate, &snapshot_sink);
     ser.SerializeStrongReferences();

     i::List<char> context_data;
-    ListSnapshotSink contex_sink(&context_data);
+    i::ListSnapshotSink contex_sink(&context_data);
     i::PartialSerializer context_ser(internal_isolate, &ser, &contex_sink);
     context_ser.Serialize(&raw_context);
     ser.SerializeWeakReferences();
=======================================
--- /branches/bleeding_edge/src/parser.cc       Tue Jul  8 07:48:22 2014 UTC
+++ /branches/bleeding_edge/src/parser.cc       Tue Jul  8 09:04:08 2014 UTC
@@ -182,7 +182,7 @@
 }


-ScriptData* ScriptData::New(const char* data, int length) {
+ScriptData* ScriptData::New(const char* data, int length, bool owns_store) {
   // The length is obviously invalid.
   if (length % sizeof(unsigned) != 0) {
     return NULL;
@@ -190,7 +190,8 @@

   int deserialized_data_length = length / sizeof(unsigned);
   unsigned* deserialized_data;
- bool owns_store = reinterpret_cast<intptr_t>(data) % sizeof(unsigned) != 0;
+  owns_store =
+ owns_store || reinterpret_cast<intptr_t>(data) % sizeof(unsigned) != 0;
   if (owns_store) {
     // Copy the data to align it.
     deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
=======================================
--- /branches/bleeding_edge/src/parser.h        Tue Jul  8 07:48:22 2014 UTC
+++ /branches/bleeding_edge/src/parser.h        Tue Jul  8 09:04:08 2014 UTC
@@ -72,7 +72,7 @@
// The created ScriptData won't take ownership of the data. If the alignment // is not correct, this will copy the data (and the created ScriptData will
   // take ownership of the copy).
-  static ScriptData* New(const char* data, int length);
+ static ScriptData* New(const char* data, int length, bool owns_store = false);

   virtual ~ScriptData();
   virtual int Length();
=======================================
--- /branches/bleeding_edge/src/serialize.cc    Mon Jun 30 13:25:46 2014 UTC
+++ /branches/bleeding_edge/src/serialize.cc    Tue Jul  8 09:04:08 2014 UTC
@@ -19,6 +19,7 @@
 #include "src/snapshot-source-sink.h"
 #include "src/stub-cache.h"
 #include "src/v8threads.h"
+#include "src/version.h"

 namespace v8 {
 namespace internal {
@@ -1794,14 +1795,15 @@
     return Page::kPageSize - Page::kObjectStartOffset;
   }
 }
+
+
+void Serializer::PadByte() { sink_->Put(kNop, "Padding"); }


 void Serializer::Pad() {
   // The non-branching GetInt will read up to 3 bytes too far, so we need
   // to pad the snapshot to make sure we don't read over the end.
-  for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
-    sink_->Put(kNop, "Padding");
-  }
+  for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) PadByte();
 }


@@ -1811,4 +1813,101 @@
 }


+ScriptData* CodeSerializer::Serialize(Handle<SharedFunctionInfo> info) {
+  // Serialize code object.
+  List<char> payload;
+  ListSnapshotSink listsink(&payload);
+  CodeSerializer ser(info->GetIsolate(), &listsink);
+  DisallowHeapAllocation no_gc;
+  Object** location = Handle<Object>::cast(info).location();
+  ser.VisitPointer(location);
+  ser.Pad();
+
+  // Allocate storage.  The payload length may not be aligned.  Round up.
+  // TODO(yangguo) replace ScriptData with a more generic super class.
+  int payload_length = payload.length();
+  int raw_length = payload_length / sizeof(unsigned) + kHeaderSize;
+  if (!IsAligned(payload_length, sizeof(unsigned))) raw_length++;
+  unsigned* raw_data = i::NewArray<unsigned>(raw_length);
+  char* payload_data = reinterpret_cast<char*>(raw_data + kHeaderSize);
+
+  // Write header.
+  raw_data[kVersionHashOffset] = Version::Hash();
+  raw_data[kPayloadLengthOffset] = payload_length;
+  STATIC_ASSERT(NEW_SPACE == 0);
+  for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
+    raw_data[kReservationsOffset + i] = ser.CurrentAllocationAddress(i);
+  }
+
+ CopyBytes(payload_data, payload.begin(), static_cast<size_t>(payload_length));
+
+  return new ScriptData(Vector<unsigned>(raw_data, raw_length), true);
+}
+
+
+void CodeSerializer::SerializeObject(Object* o, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) {
+  CHECK(o->IsHeapObject());
+  HeapObject* heap_object = HeapObject::cast(o);
+
+  // The code-caches link to context-specific code objects, which
+  // the startup and context serializes cannot currently handle.
+  ASSERT(!heap_object->IsMap() ||
+         Map::cast(heap_object)->code_cache() ==
+             heap_object->GetHeap()->empty_fixed_array());
+
+  int root_index;
+ if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
+    PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
+    return;
+  }
+
+  // TODO(yangguo) wire up builtins.
+  // TODO(yangguo) wire up stubs from stub cache.
+  // TODO(yangguo) wire up script source.
+  // TODO(yangguo) wire up internalized strings
+  ASSERT(!heap_object->IsInternalizedString());
+  // TODO(yangguo) We cannot deal with different hash seeds yet.
+  ASSERT(!heap_object->IsHashTable());
+
+  if (address_mapper_.IsMapped(heap_object)) {
+    int space = SpaceOfObject(heap_object);
+    int address = address_mapper_.MappedTo(heap_object);
+    SerializeReferenceToPreviousObject(space, address, how_to_code,
+                                       where_to_point, skip);
+    return;
+  }
+
+  if (skip != 0) {
+    sink_->Put(kSkip, "SkipFromSerializeObject");
+    sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
+  }
+  // Object has not yet been serialized.  Serialize it here.
+  ObjectSerializer serializer(this, heap_object, sink_, how_to_code,
+                              where_to_point);
+  serializer.Serialize();
+}
+
+
+Object* CodeSerializer::Deserialize(Isolate* isolate, ScriptData* data) {
+ const unsigned* raw_data = reinterpret_cast<const unsigned*>(data->Data());
+  CHECK_EQ(Version::Hash(), raw_data[kVersionHashOffset]);
+  int payload_length = raw_data[kPayloadLengthOffset];
+  const byte* payload_data =
+      reinterpret_cast<const byte*>(raw_data + kHeaderSize);
+  ASSERT_LE(payload_length, data->Length() - kHeaderSize);
+
+  SnapshotByteSource payload(payload_data, payload_length);
+  Deserializer deserializer(&payload);
+  STATIC_ASSERT(NEW_SPACE == 0);
+  // TODO(yangguo) what happens if remaining new space is too small?
+  for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
+    deserializer.set_reservation(
+        i, raw_data[CodeSerializer::kReservationsOffset + i]);
+  }
+  Object* root;
+  deserializer.DeserializePartial(isolate, &root);
+  ASSERT(root->IsSharedFunctionInfo());
+  return root;
+}
 } }  // namespace v8::internal
=======================================
--- /branches/bleeding_edge/src/serialize.h     Mon Jun 23 13:52:17 2014 UTC
+++ /branches/bleeding_edge/src/serialize.h     Tue Jul  8 09:04:08 2014 UTC
@@ -8,6 +8,7 @@
 #include "src/hashmap.h"
 #include "src/heap-profiler.h"
 #include "src/isolate.h"
+#include "src/parser.h"
 #include "src/snapshot-source-sink.h"

 namespace v8 {
@@ -469,6 +470,7 @@
   SerializationAddressMapper address_mapper_;
   intptr_t root_index_wave_front_;
   void Pad();
+  void PadByte();

   friend class ObjectSerializer;
   friend class Deserializer;
@@ -551,6 +553,29 @@
 };


+class CodeSerializer : public Serializer {
+ public:
+  CodeSerializer(Isolate* isolate, SnapshotByteSink* sink)
+      : Serializer(isolate, sink) {
+    set_root_index_wave_front(Heap::kStrongRootListLength);
+    InitializeCodeAddressMap();
+  }
+
+  static ScriptData* Serialize(Handle<SharedFunctionInfo> info);
+  virtual void SerializeObject(Object* o, HowToCode how_to_code,
+                               WhereToPoint where_to_point, int skip);
+
+  static Object* Deserialize(Isolate* isolate, ScriptData* data);
+
+  // The data header consists of int-sized entries:
+  // [0] version hash
+  // [1] length in bytes
+ // [2..8] reservation sizes for spaces from NEW_SPACE to PROPERTY_CELL_SPACE.
+  static const int kHeaderSize = 9;
+  static const int kVersionHashOffset = 0;
+  static const int kPayloadLengthOffset = 1;
+  static const int kReservationsOffset = 2;
+};
 } }  // namespace v8::internal

 #endif  // V8_SERIALIZE_H_
=======================================
--- /branches/bleeding_edge/src/snapshot-source-sink.cc Mon Jun 30 13:25:46 2014 UTC +++ /branches/bleeding_edge/src/snapshot-source-sink.cc Tue Jul 8 09:04:08 2014 UTC
@@ -90,6 +90,12 @@
     return false;
   }
 }
+
+
+void DebugSnapshotSink::Put(int byte, const char* description) {
+  PrintF("%24s: %x\n", description, byte);
+  sink_->Put(byte, description);
+}

 }  // namespace v8::internal
 }  // namespace v8
=======================================
--- /branches/bleeding_edge/src/snapshot-source-sink.h Mon Jun 30 13:25:46 2014 UTC +++ /branches/bleeding_edge/src/snapshot-source-sink.h Tue Jul 8 09:04:08 2014 UTC
@@ -82,6 +82,42 @@
 };


+class DummySnapshotSink : public SnapshotByteSink {
+ public:
+  DummySnapshotSink() : length_(0) {}
+  virtual ~DummySnapshotSink() {}
+  virtual void Put(int byte, const char* description) { length_++; }
+  virtual int Position() { return length_; }
+
+ private:
+  int length_;
+};
+
+
+// Wrap a SnapshotByteSink into a DebugSnapshotSink to get debugging output.
+class DebugSnapshotSink : public SnapshotByteSink {
+ public:
+  explicit DebugSnapshotSink(SnapshotByteSink* chained) : sink_(chained) {}
+  virtual void Put(int byte, const char* description) V8_OVERRIDE;
+  virtual int Position() V8_OVERRIDE { return sink_->Position(); }
+
+ private:
+  SnapshotByteSink* sink_;
+};
+
+
+class ListSnapshotSink : public i::SnapshotByteSink {
+ public:
+  explicit ListSnapshotSink(i::List<char>* data) : data_(data) {}
+  virtual void Put(int byte, const char* description) V8_OVERRIDE {
+    data_->Add(byte);
+  }
+  virtual int Position() V8_OVERRIDE { return data_->length(); }
+
+ private:
+  i::List<char>* data_;
+};
+
 }  // namespace v8::internal
 }  // namespace v8

=======================================
--- /branches/bleeding_edge/src/version.h       Tue Apr 29 06:42:26 2014 UTC
+++ /branches/bleeding_edge/src/version.h       Tue Jul  8 09:04:08 2014 UTC
@@ -16,6 +16,7 @@
   static int GetBuild() { return build_; }
   static int GetPatch() { return patch_; }
   static bool IsCandidate() { return candidate_; }
+  static int Hash() { return (major_ << 20) ^ (minor_ << 10) ^ patch_; }

   // Calculate the V8 version string.
   static void GetString(Vector<char> str);
=======================================
--- /branches/bleeding_edge/test/cctest/test-compiler.cc Fri Jun 27 13:48:37 2014 UTC +++ /branches/bleeding_edge/test/cctest/test-compiler.cc Tue Jul 8 09:04:08 2014 UTC
@@ -32,6 +32,7 @@

 #include "src/compiler.h"
 #include "src/disasm.h"
+#include "src/parser.h"
 #include "test/cctest/cctest.h"

 using namespace v8::internal;
@@ -396,6 +397,46 @@
     CHECK_EQ(fun1->code(), fun2->code());
   }
 }
+
+
+TEST(SerializeToplevel) {
+  FLAG_serialize_toplevel = true;
+  v8::HandleScope scope(CcTest::isolate());
+  v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
+  v8::Context::Scope context_scope(context);
+
+  const char* source1 = "1 + 1";
+ const char* source2 = "1 + 2"; // Use alternate string to verify caching.
+
+  Isolate* isolate = CcTest::i_isolate();
+  Handle<String> source1_string = isolate->factory()
+ ->NewStringFromUtf8(CStrVector(source1))
+                                      .ToHandleChecked();
+  Handle<String> source2_string = isolate->factory()
+ ->NewStringFromUtf8(CStrVector(source2))
+                                      .ToHandleChecked();
+
+  ScriptData* cache = NULL;
+
+  Handle<SharedFunctionInfo> orig =
+ Compiler::CompileScript(source1_string, Handle<String>(), 0, 0, false, + Handle<Context>(isolate->native_context()), NULL, + &cache, PRODUCE_CACHED_DATA, NOT_NATIVES_CODE);
+
+  Handle<SharedFunctionInfo> info =
+ Compiler::CompileScript(source2_string, Handle<String>(), 0, 0, false, + Handle<Context>(isolate->native_context()), NULL, + &cache, CONSUME_CACHED_DATA, NOT_NATIVES_CODE);
+
+  CHECK_NE(*orig, *info);
+  Handle<JSFunction> fun =
+      isolate->factory()->NewFunctionFromSharedFunctionInfo(
+          info, isolate->native_context());
+  Handle<JSObject> global(isolate->context()->global_object());
+  Handle<Object> result =
+      Execution::Call(isolate, fun, global, 0, NULL).ToHandleChecked();
+  CHECK_EQ(2, Handle<Smi>::cast(result)->value());
+}


 #ifdef ENABLE_DISASSEMBLER

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to