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

Side by Side Diff: src/runtime.cc

Issue 6520004: Introduce new runtime function to make join with lower memory usage. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Following Anders' advice Created 9 years, 10 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 | « src/runtime.h ('k') | test/mjsunit/fuzz-natives.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 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 5777 matching lines...) Expand 10 before | Expand all | Expand 10 after
5788 SeqTwoByteString* answer = SeqTwoByteString::cast(object); 5788 SeqTwoByteString* answer = SeqTwoByteString::cast(object);
5789 StringBuilderConcatHelper(special, 5789 StringBuilderConcatHelper(special,
5790 answer->GetChars(), 5790 answer->GetChars(),
5791 fixed_array, 5791 fixed_array,
5792 array_length); 5792 array_length);
5793 return answer; 5793 return answer;
5794 } 5794 }
5795 } 5795 }
5796 5796
5797 5797
5798 static MaybeObject* Runtime_StringBuilderJoin(Arguments args) {
5799 NoHandleAllocation ha;
5800 ASSERT(args.length() == 3);
5801 CONVERT_CHECKED(JSArray, array, args[0]);
5802 if (!args[1]->IsSmi()) {
5803 Top::context()->mark_out_of_memory();
5804 return Failure::OutOfMemoryException();
5805 }
5806 int array_length = Smi::cast(args[1])->value();
5807 CONVERT_CHECKED(String, separator, args[2]);
5808
5809 if (!array->HasFastElements()) {
5810 return Top::Throw(Heap::illegal_argument_symbol());
5811 }
5812 FixedArray* fixed_array = FixedArray::cast(array->elements());
5813 if (fixed_array->length() < array_length) {
5814 array_length = fixed_array->length();
5815 }
5816
5817 if (array_length == 0) {
5818 return Heap::empty_string();
5819 } else if (array_length == 1) {
5820 Object* first = fixed_array->get(0);
5821 if (first->IsString()) return first;
5822 }
5823
5824 int separator_length = separator->length();
5825 int max_nof_separators =
5826 (String::kMaxLength + separator_length - 1) / separator_length;
5827 if (max_nof_separators < (array_length - 1)) {
5828 Top::context()->mark_out_of_memory();
5829 return Failure::OutOfMemoryException();
5830 }
5831 int length = (array_length - 1) * separator_length;
5832 for (int i = 0; i < array_length; i++) {
5833 String* element = String::cast(fixed_array->get(i));
5834 int increment = element->length();
5835 if (increment > String::kMaxLength - length) {
5836 Top::context()->mark_out_of_memory();
5837 return Failure::OutOfMemoryException();
5838 }
5839 length += increment;
5840 }
5841
5842 Object* object;
5843 { MaybeObject* maybe_object = Heap::AllocateRawTwoByteString(length);
5844 if (!maybe_object->ToObject(&object)) return maybe_object;
5845 }
5846 SeqTwoByteString* answer = SeqTwoByteString::cast(object);
5847
5848 uc16* sink = answer->GetChars();
5849 #ifdef DEBUG
5850 uc16* end = sink + length;
5851 #endif
5852
5853 String* first = String::cast(fixed_array->get(0));
5854 int first_length = first->length();
5855 String::WriteToFlat(first, sink, 0, first_length);
5856 sink += first_length;
5857
5858 for (int i = 1; i < array_length; i++) {
5859 ASSERT(sink + separator_length <= end);
5860 String::WriteToFlat(separator, sink, 0, separator_length);
5861 sink += separator_length;
5862
5863 String* element = String::cast(fixed_array->get(i));
5864 int element_length = element->length();
5865 ASSERT(sink + element_length <= end);
5866 String::WriteToFlat(element, sink, 0, element_length);
5867 sink += element_length;
5868 }
5869 ASSERT(sink == end);
5870
5871 ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead.
5872 return answer;
5873 }
5874
5875
5798 static MaybeObject* Runtime_NumberOr(Arguments args) { 5876 static MaybeObject* Runtime_NumberOr(Arguments args) {
5799 NoHandleAllocation ha; 5877 NoHandleAllocation ha;
5800 ASSERT(args.length() == 2); 5878 ASSERT(args.length() == 2);
5801 5879
5802 CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); 5880 CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
5803 CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); 5881 CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
5804 return Heap::NumberFromInt32(x | y); 5882 return Heap::NumberFromInt32(x | y);
5805 } 5883 }
5806 5884
5807 5885
(...skipping 5290 matching lines...) Expand 10 before | Expand all | Expand 10 after
11098 } else { 11176 } else {
11099 // Handle last resort GC and make sure to allow future allocations 11177 // Handle last resort GC and make sure to allow future allocations
11100 // to grow the heap without causing GCs (if possible). 11178 // to grow the heap without causing GCs (if possible).
11101 Counters::gc_last_resort_from_js.Increment(); 11179 Counters::gc_last_resort_from_js.Increment();
11102 Heap::CollectAllGarbage(false); 11180 Heap::CollectAllGarbage(false);
11103 } 11181 }
11104 } 11182 }
11105 11183
11106 11184
11107 } } // namespace v8::internal 11185 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | test/mjsunit/fuzz-natives.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698