OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 | 692 |
693 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) { | 693 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) { |
694 Isolate* isolate = object->GetIsolate(); | 694 Isolate* isolate = object->GetIsolate(); |
695 isolate->counters()->for_in()->Increment(); | 695 isolate->counters()->for_in()->Increment(); |
696 Handle<FixedArray> elements = | 696 Handle<FixedArray> elements = |
697 GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw); | 697 GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw); |
698 return isolate->factory()->NewJSArrayWithElements(elements); | 698 return isolate->factory()->NewJSArrayWithElements(elements); |
699 } | 699 } |
700 | 700 |
701 | 701 |
| 702 Handle<FixedArray> ReduceFixedArrayTo(Handle<FixedArray> array, int length) { |
| 703 ASSERT(array->length() >= length); |
| 704 if (array->length() == length) return array; |
| 705 |
| 706 Handle<FixedArray> new_array = |
| 707 array->GetIsolate()->factory()->NewFixedArray(length); |
| 708 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i)); |
| 709 return new_array; |
| 710 } |
| 711 |
| 712 |
702 Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, | 713 Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, |
703 bool cache_result) { | 714 bool cache_result) { |
704 Isolate* isolate = object->GetIsolate(); | 715 Isolate* isolate = object->GetIsolate(); |
705 if (object->HasFastProperties()) { | 716 if (object->HasFastProperties()) { |
706 if (object->map()->instance_descriptors()->HasEnumCache()) { | 717 if (object->map()->instance_descriptors()->HasEnumCache()) { |
707 int own_property_count = object->map()->EnumLength(); | 718 int own_property_count = object->map()->EnumLength(); |
| 719 // If we have an enum cache, but the enum length of the given map is set |
| 720 // to kInvalidEnumCache, this means that the map itself has never used the |
| 721 // present enum cache. The first step to using the cache is to set the |
| 722 // enum length of the map by counting the number of own descriptors that |
| 723 // are not DONT_ENUM. |
| 724 if (own_property_count == Map::kInvalidEnumCache) { |
| 725 own_property_count = object->map()->NumberOfDescribedProperties( |
| 726 OWN_DESCRIPTORS, DONT_ENUM); |
708 | 727 |
709 // Mark that we have an enum cache if we are allowed to cache it. | 728 if (cache_result) object->map()->SetEnumLength(own_property_count); |
710 if (cache_result && own_property_count == Map::kInvalidEnumCache) { | |
711 int num_enum = object->map()->NumberOfDescribedProperties(DONT_ENUM); | |
712 object->map()->SetEnumLength(num_enum); | |
713 } | 729 } |
714 | 730 |
715 DescriptorArray* desc = object->map()->instance_descriptors(); | 731 DescriptorArray* desc = object->map()->instance_descriptors(); |
716 Handle<FixedArray> keys(FixedArray::cast(desc->GetEnumCache()), isolate); | 732 Handle<FixedArray> keys(FixedArray::cast(desc->GetEnumCache()), isolate); |
717 | 733 |
718 isolate->counters()->enum_cache_hits()->Increment(); | 734 // In case the number of properties required in the enum are actually |
719 return keys; | 735 // present, we can reuse the enum cache. Otherwise, this means that the |
| 736 // enum cache was generated for a previous (smaller) version of the |
| 737 // Descriptor Array. In that case we regenerate the enum cache. |
| 738 if (own_property_count <= keys->length()) { |
| 739 isolate->counters()->enum_cache_hits()->Increment(); |
| 740 return ReduceFixedArrayTo(keys, own_property_count); |
| 741 } |
720 } | 742 } |
721 | 743 |
722 Handle<Map> map(object->map()); | 744 Handle<Map> map(object->map()); |
723 | 745 |
724 if (map->instance_descriptors()->IsEmpty()) { | 746 if (map->instance_descriptors()->IsEmpty()) { |
725 isolate->counters()->enum_cache_hits()->Increment(); | 747 isolate->counters()->enum_cache_hits()->Increment(); |
726 if (cache_result) map->SetEnumLength(0); | 748 if (cache_result) map->SetEnumLength(0); |
727 return isolate->factory()->empty_fixed_array(); | 749 return isolate->factory()->empty_fixed_array(); |
728 } | 750 } |
729 | 751 |
730 isolate->counters()->enum_cache_misses()->Increment(); | 752 isolate->counters()->enum_cache_misses()->Increment(); |
731 | 753 int num_enum = map->NumberOfDescribedProperties(ALL_DESCRIPTORS, DONT_ENUM); |
732 int num_enum = map->NumberOfDescribedProperties(DONT_ENUM); | |
733 | 754 |
734 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); | 755 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
735 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(num_enum); | 756 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(num_enum); |
736 | 757 |
737 Handle<DescriptorArray> descs = | 758 Handle<DescriptorArray> descs = |
738 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); | 759 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); |
739 | 760 |
| 761 int real_size = map->NumberOfOwnDescriptors(); |
| 762 int enum_size = 0; |
740 int index = 0; | 763 int index = 0; |
| 764 |
741 for (int i = 0; i < descs->number_of_descriptors(); i++) { | 765 for (int i = 0; i < descs->number_of_descriptors(); i++) { |
742 PropertyDetails details = descs->GetDetails(i); | 766 PropertyDetails details = descs->GetDetails(i); |
743 if (!details.IsDontEnum()) { | 767 if (!details.IsDontEnum()) { |
| 768 if (i < real_size) ++enum_size; |
744 storage->set(index, descs->GetKey(i)); | 769 storage->set(index, descs->GetKey(i)); |
745 if (!indices.is_null()) { | 770 if (!indices.is_null()) { |
746 if (details.type() != FIELD) { | 771 if (details.type() != FIELD) { |
747 indices = Handle<FixedArray>(); | 772 indices = Handle<FixedArray>(); |
748 } else { | 773 } else { |
749 int field_index = Descriptor::IndexFromValue(descs->GetValue(i)); | 774 int field_index = Descriptor::IndexFromValue(descs->GetValue(i)); |
750 if (field_index >= map->inobject_properties()) { | 775 if (field_index >= map->inobject_properties()) { |
751 field_index = -(field_index - map->inobject_properties() + 1); | 776 field_index = -(field_index - map->inobject_properties() + 1); |
752 } | 777 } |
753 indices->set(index, Smi::FromInt(field_index)); | 778 indices->set(index, Smi::FromInt(field_index)); |
754 } | 779 } |
755 } | 780 } |
756 index++; | 781 index++; |
757 } | 782 } |
758 } | 783 } |
759 ASSERT(index == storage->length()); | 784 ASSERT(index == storage->length()); |
760 | 785 |
761 Handle<FixedArray> bridge_storage = | 786 Handle<FixedArray> bridge_storage = |
762 isolate->factory()->NewFixedArray( | 787 isolate->factory()->NewFixedArray( |
763 DescriptorArray::kEnumCacheBridgeLength); | 788 DescriptorArray::kEnumCacheBridgeLength); |
764 DescriptorArray* desc = object->map()->instance_descriptors(); | 789 DescriptorArray* desc = object->map()->instance_descriptors(); |
765 desc->SetEnumCache(*bridge_storage, | 790 desc->SetEnumCache(*bridge_storage, |
766 *storage, | 791 *storage, |
767 indices.is_null() ? Object::cast(Smi::FromInt(0)) | 792 indices.is_null() ? Object::cast(Smi::FromInt(0)) |
768 : Object::cast(*indices)); | 793 : Object::cast(*indices)); |
769 if (cache_result) { | 794 if (cache_result) { |
770 object->map()->SetEnumLength(index); | 795 object->map()->SetEnumLength(enum_size); |
771 } | 796 } |
772 return storage; | 797 |
| 798 return ReduceFixedArrayTo(storage, enum_size); |
773 } else { | 799 } else { |
774 Handle<StringDictionary> dictionary(object->property_dictionary()); | 800 Handle<StringDictionary> dictionary(object->property_dictionary()); |
775 | 801 |
776 int length = dictionary->NumberOfElements(); | 802 int length = dictionary->NumberOfElements(); |
777 if (length == 0) { | 803 if (length == 0) { |
778 return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); | 804 return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); |
779 } | 805 } |
780 | 806 |
781 // The enumeration array is generated by allocating an array big enough to | 807 // The enumeration array is generated by allocating an array big enough to |
782 // hold all properties that have been seen, whether they are are deleted or | 808 // hold all properties that have been seen, whether they are are deleted or |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1026 data->next = prev_next_; | 1052 data->next = prev_next_; |
1027 data->limit = prev_limit_; | 1053 data->limit = prev_limit_; |
1028 #ifdef DEBUG | 1054 #ifdef DEBUG |
1029 handles_detached_ = true; | 1055 handles_detached_ = true; |
1030 #endif | 1056 #endif |
1031 return deferred; | 1057 return deferred; |
1032 } | 1058 } |
1033 | 1059 |
1034 | 1060 |
1035 } } // namespace v8::internal | 1061 } } // namespace v8::internal |
OLD | NEW |