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

Side by Side Diff: src/heap.cc

Issue 409007: Some optimizations for packer.js. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 1 month 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 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 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 1756 matching lines...) Expand 10 before | Expand all | Expand 10 after
1767 if (first_length == 0) { 1767 if (first_length == 0) {
1768 return second; 1768 return second;
1769 } 1769 }
1770 1770
1771 int second_length = second->length(); 1771 int second_length = second->length();
1772 if (second_length == 0) { 1772 if (second_length == 0) {
1773 return first; 1773 return first;
1774 } 1774 }
1775 1775
1776 int length = first_length + second_length; 1776 int length = first_length + second_length;
1777
1778 // Optimization for packer.js. This popular library uses two-character
1779 // strings as keys in a hash. Check whether we already have the string
1780 // in the symbol table to prevent creation of many unneccesary strings.
1781 if (length == 2) {
1782 unsigned c1 = first->Get(0);
1783 unsigned c2 = second->Get(0);
1784 String* symbol;
Søren Thygesen Gjesse 2009/11/19 21:42:57 Please add a comment on why digits only will not w
1785 if ((c1 - '0' > '9' - '0' ||
Søren Thygesen Gjesse 2009/11/19 21:42:57 Why c1 - '0' > '9' - '0' instead of c1 > '9'?
Erik Corry 2009/11/20 10:12:46 I moved this to a different function so I could po
1786 c2 - '0' > '9' - '0') &&
1787 symbol_table()->LookupTwoCharsIfExists(c1, c2, &symbol)) {
1788 return symbol;
1789 // Now we know the length is 2, we might as well make use of that fact
1790 // when building the new string.
1791 } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this
1792 ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this.
1793 Object* result = AllocateRawAsciiString(2);
1794 if (result->IsFailure()) return result;
1795 char* dest = SeqAsciiString::cast(result)->GetChars();
1796 dest[0] = c1;
1797 dest[1] = c2;
1798 return result;
1799 } else {
1800 Object* result = AllocateRawTwoByteString(2);
1801 if (result->IsFailure()) return result;
1802 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
1803 dest[0] = c1;
1804 dest[1] = c2;
1805 return result;
1806 }
1807 }
1808
1777 bool is_ascii = first->IsAsciiRepresentation() 1809 bool is_ascii = first->IsAsciiRepresentation()
1778 && second->IsAsciiRepresentation(); 1810 && second->IsAsciiRepresentation();
1779 1811
1780 // Make sure that an out of memory exception is thrown if the length 1812 // Make sure that an out of memory exception is thrown if the length
1781 // of the new cons string is too large. 1813 // of the new cons string is too large.
1782 if (length > String::kMaxLength || length < 0) { 1814 if (length > String::kMaxLength || length < 0) {
1783 Top::context()->mark_out_of_memory(); 1815 Top::context()->mark_out_of_memory();
1784 return Failure::OutOfMemoryException(); 1816 return Failure::OutOfMemoryException();
1785 } 1817 }
1786 1818
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1836 1868
1837 1869
1838 Object* Heap::AllocateSubString(String* buffer, 1870 Object* Heap::AllocateSubString(String* buffer,
1839 int start, 1871 int start,
1840 int end) { 1872 int end) {
1841 int length = end - start; 1873 int length = end - start;
1842 1874
1843 if (length == 1) { 1875 if (length == 1) {
1844 return Heap::LookupSingleCharacterStringFromCode( 1876 return Heap::LookupSingleCharacterStringFromCode(
1845 buffer->Get(start)); 1877 buffer->Get(start));
1878 } else if (length == 2) {
Søren Thygesen Gjesse 2009/11/19 21:42:57 Refactor duplicated code.
Erik Corry 2009/11/20 10:12:46 Done.
1879 // Optimization for packer.js. This popular library uses two-character
1880 // strings as keys in a hash. Check whether we already have the string
1881 // in the symbol table to prevent creation of many unneccesary strings.
1882 unsigned c1 = buffer->Get(start);
1883 unsigned c2 = buffer->Get(start + 1);
1884 String* symbol;
1885 if ((c1 - '0' > '9' - '0' ||
1886 c2 - '0' > '9' - '0') &&
1887 symbol_table()->LookupTwoCharsIfExists(c1, c2, &symbol)) {
1888 return symbol;
1889 // Now we know the length is 2, we might as well make use of that fact
1890 // when building the new string.
1891 } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this
1892 ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this.
1893 Object* result = AllocateRawAsciiString(2);
1894 if (result->IsFailure()) return result;
1895 char* dest = SeqAsciiString::cast(result)->GetChars();
1896 dest[0] = c1;
1897 dest[1] = c2;
1898 return result;
1899 } else {
1900 Object* result = AllocateRawTwoByteString(2);
1901 if (result->IsFailure()) return result;
1902 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
1903 dest[0] = c1;
1904 dest[1] = c2;
1905 return result;
1906 }
1846 } 1907 }
1847 1908
1848 // Make an attempt to flatten the buffer to reduce access time. 1909 // Make an attempt to flatten the buffer to reduce access time.
1849 if (!buffer->IsFlat()) { 1910 if (!buffer->IsFlat()) {
1850 buffer->TryFlatten(); 1911 buffer->TryFlatten();
1851 } 1912 }
1852 1913
1853 Object* result = buffer->IsAsciiRepresentation() 1914 Object* result = buffer->IsAsciiRepresentation()
1854 ? AllocateRawAsciiString(length) 1915 ? AllocateRawAsciiString(length)
1855 : AllocateRawTwoByteString(length); 1916 : AllocateRawTwoByteString(length);
(...skipping 2140 matching lines...) Expand 10 before | Expand all | Expand 10 after
3996 for (int i = 0; i < kNumberOfCaches; i++) { 4057 for (int i = 0; i < kNumberOfCaches; i++) {
3997 if (caches_[i] != NULL) { 4058 if (caches_[i] != NULL) {
3998 delete caches_[i]; 4059 delete caches_[i];
3999 caches_[i] = NULL; 4060 caches_[i] = NULL;
4000 } 4061 }
4001 } 4062 }
4002 } 4063 }
4003 4064
4004 4065
4005 } } // namespace v8::internal 4066 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698