From d03c277f5eb194bd93e93382e3d904281a64da8a Mon Sep 17 00:00:00 2001 From: "christian.plesner.hansen@gmail.com" Date: Fri, 4 Sep 2009 07:34:25 +0000 Subject: [PATCH] Changed saved context stack to using direct pointers. Before we would create a new persistent handle to hold the context to save when entering another context, now we use a stack of direct pointers that the gc knows about. Review URL: http://codereview.chromium.org/199021 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2820 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/api.cc | 34 ++++++++++++++++------------------ src/api.h | 14 ++++++-------- src/list.h | 5 ++--- src/serialize.cc | 25 ++++++++++++++++++------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/api.cc b/src/api.cc index 679e038d3..1128d3e02 100644 --- a/src/api.cc +++ b/src/api.cc @@ -427,7 +427,7 @@ void Context::Enter() { i::Handle env = Utils::OpenHandle(this); thread_local.EnterContext(env); - thread_local.SaveContext(i::GlobalHandles::Create(i::Top::context())); + thread_local.SaveContext(i::Top::context()); i::Top::set_context(*env); } @@ -441,9 +441,8 @@ void Context::Exit() { } // Content of 'last_context' could be NULL. - i::Handle last_context = thread_local.RestoreContext(); - i::Top::set_context(static_cast(*last_context)); - i::GlobalHandles::Destroy(last_context.location()); + i::Context* last_context = thread_local.RestoreContext(); + i::Top::set_context(last_context); } @@ -3700,19 +3699,21 @@ char* HandleScopeImplementer::RestoreThreadHelper(char* storage) { } -void HandleScopeImplementer::Iterate( - ObjectVisitor* v, - List* blocks, - v8::ImplementationUtilities::HandleScopeData* handle_data) { +void HandleScopeImplementer::IterateThis(ObjectVisitor* v) { // Iterate over all handles in the blocks except for the last. - for (int i = blocks->length() - 2; i >= 0; --i) { - Object** block = blocks->at(i); + for (int i = Blocks()->length() - 2; i >= 0; --i) { + Object** block = Blocks()->at(i); v->VisitPointers(block, &block[kHandleBlockSize]); } // Iterate over live handles in the last block (if any). - if (!blocks->is_empty()) { - v->VisitPointers(blocks->last(), handle_data->next); + if (!Blocks()->is_empty()) { + v->VisitPointers(Blocks()->last(), handle_scope_data_.next); + } + + if (!saved_contexts_.is_empty()) { + Object** start = reinterpret_cast(&saved_contexts_.first()); + v->VisitPointers(start, start + saved_contexts_.length()); } } @@ -3720,18 +3721,15 @@ void HandleScopeImplementer::Iterate( void HandleScopeImplementer::Iterate(ObjectVisitor* v) { v8::ImplementationUtilities::HandleScopeData* current = v8::ImplementationUtilities::CurrentHandleScope(); - Iterate(v, thread_local.Blocks(), current); + thread_local.handle_scope_data_ = *current; + thread_local.IterateThis(v); } char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { HandleScopeImplementer* thread_local = reinterpret_cast(storage); - List* blocks_of_archived_thread = thread_local->Blocks(); - v8::ImplementationUtilities::HandleScopeData* handle_data_of_archived_thread = - &thread_local->handle_scope_data_; - Iterate(v, blocks_of_archived_thread, handle_data_of_archived_thread); - + thread_local->IterateThis(v); return storage + ArchiveSpacePerThread(); } diff --git a/src/api.h b/src/api.h index ca8f523c9..9ae6307b4 100644 --- a/src/api.h +++ b/src/api.h @@ -352,8 +352,8 @@ class HandleScopeImplementer { // contexts have been entered. inline Handle LastEnteredContext(); - inline void SaveContext(Handle context); - inline Handle RestoreContext(); + inline void SaveContext(Context* context); + inline Context* RestoreContext(); inline bool HasSavedContexts(); inline List* Blocks() { return &blocks; } @@ -368,14 +368,12 @@ class HandleScopeImplementer { // Used as a stack to keep track of entered contexts. List > entered_contexts_; // Used as a stack to keep track of saved contexts. - List > saved_contexts_; + List saved_contexts_; bool ignore_out_of_memory; // This is only used for threading support. v8::ImplementationUtilities::HandleScopeData handle_scope_data_; - static void Iterate(ObjectVisitor* v, - List* blocks, - v8::ImplementationUtilities::HandleScopeData* handle_data); + void IterateThis(ObjectVisitor* v); char* RestoreThreadHelper(char* from); char* ArchiveThreadHelper(char* to); @@ -386,12 +384,12 @@ class HandleScopeImplementer { static const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page -void HandleScopeImplementer::SaveContext(Handle context) { +void HandleScopeImplementer::SaveContext(Context* context) { saved_contexts_.Add(context); } -Handle HandleScopeImplementer::RestoreContext() { +Context* HandleScopeImplementer::RestoreContext() { return saved_contexts_.RemoveLast(); } diff --git a/src/list.h b/src/list.h index b6c06d846..dd7ea1c9c 100644 --- a/src/list.h +++ b/src/list.h @@ -62,9 +62,8 @@ class List { return data_[i]; } inline T& at(int i) const { return operator[](i); } - inline T& last() const { - return at(length_ - 1); - } + inline T& last() const { return at(length_ - 1); } + inline T& first() const { return at(0); } INLINE(bool is_empty() const) { return length_ == 0; } INLINE(int length() const) { return length_; } diff --git a/src/serialize.cc b/src/serialize.cc index a16032ad3..9c141da05 100644 --- a/src/serialize.cc +++ b/src/serialize.cc @@ -1201,19 +1201,24 @@ void Serializer::PutGlobalHandleStack(const List >& stack) { void Serializer::PutContextStack() { - List > contexts(2); + List contexts(2); while (HandleScopeImplementer::instance()->HasSavedContexts()) { - Handle context = + Context* context = HandleScopeImplementer::instance()->RestoreContext(); contexts.Add(context); } for (int i = contexts.length() - 1; i >= 0; i--) { HandleScopeImplementer::instance()->SaveContext(contexts[i]); } - PutGlobalHandleStack(contexts); + writer_->PutC('['); + writer_->PutInt(contexts.length()); + if (!contexts.is_empty()) { + Object** start = reinterpret_cast(&contexts.first()); + VisitPointers(start, start + contexts.length()); + } + writer_->PutC(']'); } - void Serializer::PutEncodedAddress(Address addr) { writer_->PutC('P'); writer_->PutAddress(addr); @@ -1541,9 +1546,15 @@ void Deserializer::GetGlobalHandleStack(List >* stack) { void Deserializer::GetContextStack() { - List > entered_contexts(2); - GetGlobalHandleStack(&entered_contexts); - for (int i = 0; i < entered_contexts.length(); i++) { + reader_.ExpectC('['); + int count = reader_.GetInt(); + List entered_contexts(count); + if (count > 0) { + Object** start = reinterpret_cast(&entered_contexts.first()); + VisitPointers(start, start + count); + } + reader_.ExpectC(']'); + for (int i = 0; i < count; i++) { HandleScopeImplementer::instance()->SaveContext(entered_contexts[i]); } } -- 2.34.1