Index: test/cctest/test-heap.cc |
diff --git a/test/cctest/test-heap.cc b/test/cctest/test-heap.cc |
index ee476db6cdc4729b1a65a560fe03734a65f2bfdb..5da64174e3e5c5809f68ca43a24afac50711c1ff 100644 |
--- a/test/cctest/test-heap.cc |
+++ b/test/cctest/test-heap.cc |
@@ -1602,6 +1602,64 @@ TEST(TestInternalWeakListsTraverseWithGC) { |
} |
+TEST(TestSizeOfRegExpCode) { |
+ if (!FLAG_regexp_optimization) return; |
+ |
+ v8::V8::Initialize(); |
+ |
+ Isolate* isolate = CcTest::i_isolate(); |
+ HandleScope scope(isolate); |
+ |
+ LocalContext context; |
+ |
+ // Adjust source below and this check to match |
+ // RegExpImple::kRegExpTooLargeToOptimize. |
+ DCHECK_EQ(i::RegExpImpl::kRegExpTooLargeToOptimize, 10 * KB); |
+ |
+ // Compile a regexp that is much larger if we are using regexp optimizations. |
+ CompileRun( |
+ "var reg_exp_source = '(?:a|bc|def|ghij|klmno|pqrstu)';" |
+ "var half_size_reg_exp;" |
+ "while (reg_exp_source.length < 10 * 1024) {" |
+ " half_size_reg_exp = reg_exp_source;" |
+ " reg_exp_source = reg_exp_source + reg_exp_source;" |
+ "}" |
+ // Flatten string. |
+ "reg_exp_source.match(/f/);"); |
+ |
+ // Get initial heap size after several full GCs, which will stabilize |
+ // the heap size and return with sweeping finished completely. |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ MarkCompactCollector* collector = CcTest::heap()->mark_compact_collector(); |
+ if (collector->sweeping_in_progress()) { |
+ collector->EnsureSweepingCompleted(); |
+ } |
+ int initial_size = static_cast<int>(CcTest::heap()->SizeOfObjects()); |
+ |
+ CompileRun("'foo'.match(reg_exp_source);"); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ int size_with_regexp = static_cast<int>(CcTest::heap()->SizeOfObjects()); |
+ |
+ CompileRun("'foo'.match(half_size_reg_exp);"); |
+ CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags); |
+ int size_with_optimized_regexp = |
+ static_cast<int>(CcTest::heap()->SizeOfObjects()); |
+ |
+ int size_of_regexp_code = size_with_regexp - initial_size; |
+ |
+ CHECK_LE(size_of_regexp_code, 500 * KB); |
+ |
+ // Small regexp is half the size, but compiles to more than twice the code |
+ // due to the optimization steps. |
+ CHECK_GE(size_with_optimized_regexp, |
+ size_with_regexp + size_of_regexp_code * 2); |
+} |
+ |
+ |
TEST(TestSizeOfObjects) { |
v8::V8::Initialize(); |