From b5a72a1645a9d97bc33d40f1d65043a23a7c0ece Mon Sep 17 00:00:00 2001 From: "kasperl@chromium.org" Date: Thu, 11 Sep 2008 10:51:52 +0000 Subject: [PATCH] Generalized the EvalCache into a CompilationCache and enabled it for scripts too. In the context of Chromium, this should have a very positive impact on memory consumption for web apps that run multiple tabs from the same domain with a lot of the same JavaScript code. For now, the cache retirement policy is really simple: Whenever a mark-sweep collection is started we clear the cache. This guarantees that this change will not have a huge negative impact on memory consumption, but it may not be ideal. We should consider a more sophisticated LRU scheme. Review URL: http://codereview.chromium.org/1933 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@270 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/SConscript | 8 +- src/compilation-cache.cc | 150 +++++++++++++++++++++++++++++++++++++ src/compilation-cache.h | 85 +++++++++++++++++++++ src/compiler.cc | 72 ++++++++++++------ src/handles.cc | 28 ------- src/heap-inl.h | 26 +++++++ src/heap.cc | 41 ++-------- src/heap.h | 27 +------ src/objects-inl.h | 8 +- src/objects.cc | 7 +- src/objects.h | 11 +-- src/runtime.cc | 26 ++----- src/v8-counters.h | 4 +- test/cctest/test-api.cc | 19 ++++- tools/visual_studio/v8_base.vcproj | 8 ++ 15 files changed, 369 insertions(+), 151 deletions(-) create mode 100644 src/compilation-cache.cc create mode 100644 src/compilation-cache.h diff --git a/src/SConscript b/src/SConscript index cc39e22..725f935 100644 --- a/src/SConscript +++ b/src/SConscript @@ -37,9 +37,9 @@ SOURCES = { 'all': [ 'accessors.cc', 'allocation.cc', 'api.cc', 'assembler.cc', 'ast.cc', 'bootstrapper.cc', 'builtins.cc', 'checks.cc', 'code-stubs.cc', - 'codegen.cc', 'compiler.cc', 'contexts.cc', 'conversions.cc', - 'counters.cc', 'dateparser.cc', 'debug.cc', 'disassembler.cc', - 'execution.cc', 'factory.cc', 'flags.cc', 'frames.cc', + 'codegen.cc', 'compilation-cache.cc', 'compiler.cc', 'contexts.cc', + 'conversions.cc', 'counters.cc', 'dateparser.cc', 'debug.cc', + 'disassembler.cc', 'execution.cc', 'factory.cc', 'flags.cc', 'frames.cc', 'global-handles.cc', 'handles.cc', 'hashmap.cc', 'heap.cc', 'ic.cc', 'jsregexp.cc', 'log.cc', 'mark-compact.cc', 'messages.cc', 'objects.cc', 'parser.cc', 'property.cc', 'rewriter.cc', 'runtime.cc', 'scanner.cc', @@ -125,7 +125,7 @@ def ConfigureObjectFiles(): source_objs = context.ConfigureObject(env, source_files) non_snapshot_files = [jscre_obj, dtoa_obj, source_objs] - + # Create snapshot if necessary. empty_snapshot_obj = context.ConfigureObject(env, 'snapshot-empty.cc') if context.use_snapshot: diff --git a/src/compilation-cache.cc b/src/compilation-cache.cc new file mode 100644 index 0000000..67549e5 --- /dev/null +++ b/src/compilation-cache.cc @@ -0,0 +1,150 @@ +// Copyright 2008 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "v8.h" + +#include "compilation-cache.h" + +namespace v8 { namespace internal { + +enum { + NUMBER_OF_ENTRY_KINDS = CompilationCache::EVAL_CONTEXTUAL + 1 +}; + + +// Keep separate tables for the different entry kinds. +static Object* tables[NUMBER_OF_ENTRY_KINDS] = { 0, }; + + +static Handle AllocateTable(int size) { + CALL_HEAP_FUNCTION(CompilationCacheTable::Allocate(size), + CompilationCacheTable); +} + + +static Handle GetTable(CompilationCache::Entry entry) { + Handle result; + if (tables[entry]->IsUndefined()) { + static const int kInitialCacheSize = 64; + result = AllocateTable(kInitialCacheSize); + tables[entry] = *result; + } else { + CompilationCacheTable* table = CompilationCacheTable::cast(tables[entry]); + result = Handle(table); + } + return result; +} + + +// We only re-use a cached function for some script source code if the +// script originates from the same places. This is to avoid issues +// when reporting errors, etc. +static bool HasOrigin(Handle boilerplate, + Handle name, + int line_offset, + int column_offset) { + Handle