Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(998)

Side by Side Diff: runtime/vm/object.cc

Issue 397413002: Reimplement Symbols using hash table template. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 6790 matching lines...) Expand 10 before | Expand all | Expand 10 after
6801 } 6801 }
6802 6802
6803 6803
6804 void RedirectionData::PrintJSONImpl(JSONStream* stream, bool ref) const { 6804 void RedirectionData::PrintJSONImpl(JSONStream* stream, bool ref) const {
6805 Object::PrintJSONImpl(stream, ref); 6805 Object::PrintJSONImpl(stream, ref);
6806 } 6806 }
6807 6807
6808 6808
6809 RawString* Field::GetterName(const String& field_name) { 6809 RawString* Field::GetterName(const String& field_name) {
6810 CompilerStats::make_accessor_name++; 6810 CompilerStats::make_accessor_name++;
6811 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup
6812 // to Symbols.
6811 return String::Concat(Symbols::GetterPrefix(), field_name); 6813 return String::Concat(Symbols::GetterPrefix(), field_name);
6812 } 6814 }
6813 6815
6814 6816
6815 RawString* Field::GetterSymbol(const String& field_name) { 6817 RawString* Field::GetterSymbol(const String& field_name) {
6816 const String& str = String::Handle(Field::GetterName(field_name)); 6818 const String& str = String::Handle(Field::GetterName(field_name));
6817 return Symbols::New(str); 6819 return Symbols::New(str);
6818 } 6820 }
6819 6821
6820 6822
6821 RawString* Field::SetterName(const String& field_name) { 6823 RawString* Field::SetterName(const String& field_name) {
6822 CompilerStats::make_accessor_name++; 6824 CompilerStats::make_accessor_name++;
6825 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup
6826 // to Symbols.
6823 return String::Concat(Symbols::SetterPrefix(), field_name); 6827 return String::Concat(Symbols::SetterPrefix(), field_name);
6824 } 6828 }
6825 6829
6826 6830
6827 RawString* Field::SetterSymbol(const String& field_name) { 6831 RawString* Field::SetterSymbol(const String& field_name) {
6828 const String& str = String::Handle(Field::SetterName(field_name)); 6832 const String& str = String::Handle(Field::SetterName(field_name));
6829 return Symbols::New(str); 6833 return Symbols::New(str);
6830 } 6834 }
6831 6835
6832 6836
(...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after
7649 return descriptor.literal->Hash(); 7653 return descriptor.literal->Hash();
7650 } 7654 }
7651 7655
7652 static uword Hash(const Object& key) { 7656 static uword Hash(const Object& key) {
7653 if (key.IsLiteralToken()) { 7657 if (key.IsLiteralToken()) {
7654 return String::HashRawSymbol(LiteralToken::Cast(key).literal()); 7658 return String::HashRawSymbol(LiteralToken::Cast(key).literal());
7655 } else { 7659 } else {
7656 return String::Cast(key).Hash(); 7660 return String::Cast(key).Hash();
7657 } 7661 }
7658 } 7662 }
7663
7664 static RawObject* NewKey(const Scanner::TokenDescriptor& descriptor) {
7665 return LiteralToken::New(descriptor.kind, *descriptor.literal);
7666 }
7659 }; 7667 };
7660 typedef UnorderedHashMap<CompressedTokenTraits> CompressedTokenMap; 7668 typedef UnorderedHashMap<CompressedTokenTraits> CompressedTokenMap;
7661 7669
7662 7670
7663 // Helper class for creation of compressed token stream data. 7671 // Helper class for creation of compressed token stream data.
7664 class CompressedTokenStreamData : public ValueObject { 7672 class CompressedTokenStreamData : public ValueObject {
7665 public: 7673 public:
7666 static const intptr_t kInitialBufferSize = 16 * KB; 7674 static const intptr_t kInitialBufferSize = 16 * KB;
7667 CompressedTokenStreamData() : 7675 CompressedTokenStreamData() :
7668 buffer_(NULL), 7676 buffer_(NULL),
7669 stream_(&buffer_, Reallocate, kInitialBufferSize), 7677 stream_(&buffer_, Reallocate, kInitialBufferSize),
7670 tokens_(Array::Handle( 7678 tokens_(Array::Handle(
7671 HashTables::New<CompressedTokenMap>(kInitialTableSize))) { 7679 HashTables::New<CompressedTokenMap>(kInitialTableSize))) {
7672 } 7680 }
7673 ~CompressedTokenStreamData() { 7681 ~CompressedTokenStreamData() {
7674 // Safe to discard the hash table now. 7682 // Safe to discard the hash table now.
7675 tokens_.Release(); 7683 tokens_.Release();
7676 } 7684 }
7677 7685
7678 // Add an IDENT token into the stream and the token hash map. 7686 // Add an IDENT token into the stream and the token hash map.
7679 void AddIdentToken(const String* ident) { 7687 void AddIdentToken(const String* ident) {
7680 ASSERT(ident->IsSymbol()); 7688 ASSERT(ident->IsSymbol());
7681 intptr_t index = tokens_.NumOccupied(); 7689 const intptr_t fresh_index = tokens_.NumOccupied();
7682 intptr_t entry; 7690 intptr_t index = Smi::Value(Smi::RawCast(
7683 if (!tokens_.FindKeyOrDeletedOrUnused(*ident, &entry)) { 7691 tokens_.InsertOrGetValue(*ident,
7684 tokens_.InsertKey(entry, *ident); 7692 Smi::Handle(Smi::New(fresh_index)))));
7685 tokens_.UpdatePayload(entry, 0, Smi::Handle(Smi::New(index)));
7686 HashTables::EnsureLoadFactor(0.0, kMaxLoadFactor, tokens_);
7687 } else {
7688 index = Smi::Value(Smi::RawCast(tokens_.GetPayload(entry, 0)));
7689 }
7690 WriteIndex(index); 7693 WriteIndex(index);
7691 } 7694 }
7692 7695
7693 // Add a LITERAL token into the stream and the token hash map. 7696 // Add a LITERAL token into the stream and the token hash map.
7694 void AddLiteralToken(const Scanner::TokenDescriptor& descriptor) { 7697 void AddLiteralToken(const Scanner::TokenDescriptor& descriptor) {
7695 ASSERT(descriptor.literal->IsSymbol()); 7698 ASSERT(descriptor.literal->IsSymbol());
7696 intptr_t index = tokens_.NumOccupied(); 7699 const intptr_t fresh_index = tokens_.NumOccupied();
7697 intptr_t entry; 7700 intptr_t index = Smi::Value(Smi::RawCast(
7698 if (!tokens_.FindKeyOrDeletedOrUnused(descriptor, &entry)) { 7701 tokens_.InsertNewOrGetValue(descriptor,
7699 LiteralToken& new_literal = LiteralToken::Handle( 7702 Smi::Handle(Smi::New(fresh_index)))));
7700 LiteralToken::New(descriptor.kind, *descriptor.literal));
7701 tokens_.InsertKey(entry, new_literal);
7702 tokens_.UpdatePayload(entry, 0, Smi::Handle(Smi::New(index)));
7703 HashTables::EnsureLoadFactor(0.0, kMaxLoadFactor, tokens_);
7704 } else {
7705 index = Smi::Value(Smi::RawCast(tokens_.GetPayload(entry, 0)));
7706 }
7707 WriteIndex(index); 7703 WriteIndex(index);
7708 } 7704 }
7709 7705
7710 // Add a simple token into the stream. 7706 // Add a simple token into the stream.
7711 void AddSimpleToken(intptr_t kind) { 7707 void AddSimpleToken(intptr_t kind) {
7712 stream_.WriteUnsigned(kind); 7708 stream_.WriteUnsigned(kind);
7713 } 7709 }
7714 7710
7715 // Return the compressed token stream. 7711 // Return the compressed token stream.
7716 uint8_t* GetStream() const { return buffer_; } 7712 uint8_t* GetStream() const { return buffer_; }
(...skipping 21 matching lines...) Expand all
7738 } 7734 }
7739 7735
7740 static uint8_t* Reallocate(uint8_t* ptr, 7736 static uint8_t* Reallocate(uint8_t* ptr,
7741 intptr_t old_size, 7737 intptr_t old_size,
7742 intptr_t new_size) { 7738 intptr_t new_size) {
7743 void* new_ptr = ::realloc(reinterpret_cast<void*>(ptr), new_size); 7739 void* new_ptr = ::realloc(reinterpret_cast<void*>(ptr), new_size);
7744 return reinterpret_cast<uint8_t*>(new_ptr); 7740 return reinterpret_cast<uint8_t*>(new_ptr);
7745 } 7741 }
7746 7742
7747 static const intptr_t kInitialTableSize = 32; 7743 static const intptr_t kInitialTableSize = 32;
7748 static const double kMaxLoadFactor;
7749 7744
7750 uint8_t* buffer_; 7745 uint8_t* buffer_;
7751 WriteStream stream_; 7746 WriteStream stream_;
7752 CompressedTokenMap tokens_; 7747 CompressedTokenMap tokens_;
7753 7748
7754 DISALLOW_COPY_AND_ASSIGN(CompressedTokenStreamData); 7749 DISALLOW_COPY_AND_ASSIGN(CompressedTokenStreamData);
7755 }; 7750 };
7756 7751
7757 7752
7758 const double CompressedTokenStreamData::kMaxLoadFactor = 0.75;
7759
7760
7761 RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens, 7753 RawTokenStream* TokenStream::New(const Scanner::GrowableTokenStream& tokens,
7762 const String& private_key) { 7754 const String& private_key) {
7763 // Copy the relevant data out of the scanner into a compressed stream of 7755 // Copy the relevant data out of the scanner into a compressed stream of
7764 // tokens. 7756 // tokens.
7765 CompressedTokenStreamData data; 7757 CompressedTokenStreamData data;
7766 intptr_t len = tokens.length(); 7758 intptr_t len = tokens.length();
7767 for (intptr_t i = 0; i < len; i++) { 7759 for (intptr_t i = 0; i < len; i++) {
7768 Scanner::TokenDescriptor token = tokens[i]; 7760 Scanner::TokenDescriptor token = tokens[i];
7769 if (token.kind == Token::kIDENT) { // Identifier token. 7761 if (token.kind == Token::kIDENT) { // Identifier token.
7770 if (FLAG_compiler_stats) { 7762 if (FLAG_compiler_stats) {
(...skipping 11394 matching lines...) Expand 10 before | Expand all | Expand 10 after
19165 return tag_label.ToCString(); 19157 return tag_label.ToCString();
19166 } 19158 }
19167 19159
19168 19160
19169 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 19161 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
19170 Instance::PrintJSONImpl(stream, ref); 19162 Instance::PrintJSONImpl(stream, ref);
19171 } 19163 }
19172 19164
19173 19165
19174 } // namespace dart 19166 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/symbols.h » ('j') | runtime/vm/symbols.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698