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

Side by Side Diff: src/objects-inl.h

Issue 11028056: Also use binary search to search through valid entries. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: In case of VALID_ENTRIES, check valid_entries <= kMax Created 8 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1897 matching lines...) Expand 10 before | Expand all | Expand 10 after
1908 1908
1909 void DescriptorArray::SetNumberOfDescriptors(int number_of_descriptors) { 1909 void DescriptorArray::SetNumberOfDescriptors(int number_of_descriptors) {
1910 WRITE_FIELD( 1910 WRITE_FIELD(
1911 this, kDescriptorLengthOffset, Smi::FromInt(number_of_descriptors)); 1911 this, kDescriptorLengthOffset, Smi::FromInt(number_of_descriptors));
1912 } 1912 }
1913 1913
1914 1914
1915 // Perform a binary search in a fixed array. Low and high are entry indices. If 1915 // Perform a binary search in a fixed array. Low and high are entry indices. If
1916 // there are three entries in this array it should be called with low=0 and 1916 // there are three entries in this array it should be called with low=0 and
1917 // high=2. 1917 // high=2.
1918 template<typename T> 1918 template<SearchMode search_mode, typename T>
1919 int BinarySearch(T* array, String* name, int low, int high) { 1919 int BinarySearch(T* array, String* name, int low, int high, int valid_entries) {
1920 uint32_t hash = name->Hash(); 1920 uint32_t hash = name->Hash();
1921 int limit = high; 1921 int limit = high;
1922 1922
1923 ASSERT(low <= high); 1923 ASSERT(low <= high);
1924 1924
1925 while (low != high) { 1925 while (low != high) {
1926 int mid = (low + high) / 2; 1926 int mid = (low + high) / 2;
1927 String* mid_name = array->GetSortedKey(mid); 1927 String* mid_name = array->GetSortedKey(mid);
1928 uint32_t mid_hash = mid_name->Hash(); 1928 uint32_t mid_hash = mid_name->Hash();
1929 1929
1930 if (mid_hash >= hash) { 1930 if (mid_hash >= hash) {
1931 high = mid; 1931 high = mid;
1932 } else { 1932 } else {
1933 low = mid + 1; 1933 low = mid + 1;
1934 } 1934 }
1935 } 1935 }
1936 1936
1937 for (; low <= limit; ++low) { 1937 for (; low <= limit; ++low) {
1938 int sort_index = array->GetSortedKeyIndex(low); 1938 int sort_index = array->GetSortedKeyIndex(low);
1939 String* entry = array->GetKey(sort_index); 1939 String* entry = array->GetKey(sort_index);
1940 if (entry->Hash() != hash) break; 1940 if (entry->Hash() != hash) break;
1941 if (entry->Equals(name)) return sort_index; 1941 if (entry->Equals(name)) {
1942 if (search_mode == ALL_ENTRIES || sort_index < valid_entries) {
1943 return sort_index;
1944 }
1945 return T::kNotFound;
1946 }
1942 } 1947 }
1943 1948
1944 return T::kNotFound; 1949 return T::kNotFound;
1945 } 1950 }
1946 1951
1947 // Perform a linear search in this fixed array. len is the number of entry 1952 // Perform a linear search in this fixed array. len is the number of entry
1948 // indices that are valid. 1953 // indices that are valid.
1949 template<SearchMode search_mode, typename T> 1954 template<SearchMode search_mode, typename T>
1950 int LinearSearch(T* array, String* name, int len, int valid_entries) { 1955 int LinearSearch(T* array, String* name, int len, int valid_entries) {
1951 uint32_t hash = name->Hash(); 1956 uint32_t hash = name->Hash();
(...skipping 23 matching lines...) Expand all
1975 SLOW_ASSERT(array->IsSortedNoDuplicates(valid_entries)); 1980 SLOW_ASSERT(array->IsSortedNoDuplicates(valid_entries));
1976 } else { 1981 } else {
1977 SLOW_ASSERT(array->IsSortedNoDuplicates()); 1982 SLOW_ASSERT(array->IsSortedNoDuplicates());
1978 } 1983 }
1979 1984
1980 int nof = array->number_of_entries(); 1985 int nof = array->number_of_entries();
1981 if (nof == 0) return T::kNotFound; 1986 if (nof == 0) return T::kNotFound;
1982 1987
1983 // Fast case: do linear search for small arrays. 1988 // Fast case: do linear search for small arrays.
1984 const int kMaxElementsForLinearSearch = 8; 1989 const int kMaxElementsForLinearSearch = 8;
1985 if (search_mode == VALID_ENTRIES || 1990 if ((search_mode == ALL_ENTRIES &&
1986 (search_mode == ALL_ENTRIES && nof < kMaxElementsForLinearSearch)) { 1991 nof <= kMaxElementsForLinearSearch) ||
1992 (search_mode == VALID_ENTRIES &&
1993 valid_entries <= kMaxElementsForLinearSearch)) {
1987 return LinearSearch<search_mode>(array, name, nof, valid_entries); 1994 return LinearSearch<search_mode>(array, name, nof, valid_entries);
1988 } 1995 }
1989 1996
1990 // Slow case: perform binary search. 1997 // Slow case: perform binary search.
1991 return BinarySearch(array, name, 0, nof - 1); 1998 return BinarySearch<search_mode>(array, name, 0, nof - 1, valid_entries);
1992 } 1999 }
1993 2000
1994 2001
1995 int DescriptorArray::Search(String* name, int valid_descriptors) { 2002 int DescriptorArray::Search(String* name, int valid_descriptors) {
1996 return internal::Search<VALID_ENTRIES>(this, name, valid_descriptors); 2003 return internal::Search<VALID_ENTRIES>(this, name, valid_descriptors);
1997 } 2004 }
1998 2005
1999 2006
2000 int DescriptorArray::SearchWithCache(String* name, Map* map) { 2007 int DescriptorArray::SearchWithCache(String* name, Map* map) {
2001 int number_of_own_descriptors = map->NumberOfOwnDescriptors(); 2008 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after
5557 #undef WRITE_UINT32_FIELD 5564 #undef WRITE_UINT32_FIELD
5558 #undef READ_SHORT_FIELD 5565 #undef READ_SHORT_FIELD
5559 #undef WRITE_SHORT_FIELD 5566 #undef WRITE_SHORT_FIELD
5560 #undef READ_BYTE_FIELD 5567 #undef READ_BYTE_FIELD
5561 #undef WRITE_BYTE_FIELD 5568 #undef WRITE_BYTE_FIELD
5562 5569
5563 5570
5564 } } // namespace v8::internal 5571 } } // namespace v8::internal
5565 5572
5566 #endif // V8_OBJECTS_INL_H_ 5573 #endif // V8_OBJECTS_INL_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698