[JSC] Make StringRecursionChecker faster in the simple cases without any recursion
https://bugs.webkit.org/show_bug.cgi?id=145102
Reviewed by Darin Adler.
Source/JavaScriptCore:
In general, the array targeted by Array.toString() or Array.join() are pretty
simple. In those simple cases, we spend as much time in StringRecursionChecker
as we do on the actual operation.
The reason for this is the HashSet stringRecursionCheckVisitedObjects used
to detect recursion. We are constantly adding and removing objects which
dirty buckets and force constant rehash.
This patch adds a simple shortcut for those simple case: in addition to the HashSet,
we keep a pointer to the root object of the recursion.
In the vast majority of cases, we no longer touch the HashSet at all.
This patch is a 12% progression on the overall score of ArrayWeighted.
* runtime/StringRecursionChecker.h:
(JSC::StringRecursionChecker::performCheck):
(JSC::StringRecursionChecker::~StringRecursionChecker):
* runtime/VM.h:
LayoutTests:
Improve the coverage a tiny bit.
* js/array-string-recursion-expected.txt: Added.
* js/array-string-recursion.html: Added.
* js/script-tests/array-string-recursion.js: Added.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@184447 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/runtime/StringRecursionChecker.h b/Source/JavaScriptCore/runtime/StringRecursionChecker.h
index c99dd4f..0f1990e 100644
--- a/Source/JavaScriptCore/runtime/StringRecursionChecker.h
+++ b/Source/JavaScriptCore/runtime/StringRecursionChecker.h
@@ -52,7 +52,15 @@
VM& vm = m_exec->vm();
if (!vm.isSafeToRecurse())
return throwStackOverflowError();
- bool alreadyVisited = !vm.stringRecursionCheckVisitedObjects.add(m_thisObject).isNewEntry;
+
+ bool alreadyVisited = false;
+ if (!vm.stringRecursionCheckFirstObject)
+ vm.stringRecursionCheckFirstObject = m_thisObject;
+ else if (vm.stringRecursionCheckFirstObject == m_thisObject)
+ alreadyVisited = true;
+ else
+ alreadyVisited = !vm.stringRecursionCheckVisitedObjects.add(m_thisObject).isNewEntry;
+
if (alreadyVisited)
return emptyString(); // Return empty string to avoid infinite recursion.
return JSValue(); // Indicate success.
@@ -74,8 +82,14 @@
{
if (m_earlyReturnValue)
return;
- ASSERT(m_exec->vm().stringRecursionCheckVisitedObjects.contains(m_thisObject));
- m_exec->vm().stringRecursionCheckVisitedObjects.remove(m_thisObject);
+
+ VM& vm = m_exec->vm();
+ if (vm.stringRecursionCheckFirstObject == m_thisObject)
+ vm.stringRecursionCheckFirstObject = nullptr;
+ else {
+ ASSERT(vm.stringRecursionCheckVisitedObjects.contains(m_thisObject));
+ vm.stringRecursionCheckVisitedObjects.remove(m_thisObject);
+ }
}
}