JS speed - Konq vs. Mozilla

Koos Vriezen koos.vriezen at xs4all.nl
Wed Aug 21 20:15:12 BST 2002


On Wed, 21 Aug 2002, Koos Vriezen wrote:

> Hmm, it doesn't trigger the SIGVTALRM on my slower box (11/14 s). Will fix
> that...

... here it is. Made a KJSCPUGuard class of it

Koos
-------------- next part --------------
Index: ecma/kjs_proxy.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/ecma/kjs_proxy.h,v
retrieving revision 1.18
diff -u -3 -p -r1.18 kjs_proxy.h
--- ecma/kjs_proxy.h	2002/06/17 20:50:23	1.18
+++ ecma/kjs_proxy.h	2002/08/21 19:14:30
@@ -24,6 +24,7 @@
 
 #include <qvariant.h>
 #include <qstring.h>
+#include <sys/time.h>
 
 class KHTMLPart;
 class KJSDebugWin;
@@ -66,6 +67,16 @@ public:
 
   // Helper method, to access the private KHTMLPart::jScript()
   static KJSProxy *proxy( KHTMLPart *part );
+};
+
+class KJSCPUGuard {
+public:
+  KJSCPUGuard(unsigned int msec=5000, unsigned int i_msec=10000);
+  ~KJSCPUGuard();
+private:
+  void (*oldAlarmHandler)(int);
+  static void alarmHandler(int);
+  itimerval oldtv; 
 };
 
 #endif
Index: ecma/kjs_proxy.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/ecma/kjs_proxy.cpp,v
retrieving revision 1.82
diff -u -3 -p -r1.82 kjs_proxy.cpp
--- ecma/kjs_proxy.cpp	2002/08/13 16:22:15	1.82
+++ ecma/kjs_proxy.cpp	2002/08/21 19:14:31
@@ -60,11 +60,6 @@ public:
   void applyUserAgent();
 
 private:
-  static void alarmHandler(int) {
-      kdDebug(6070) << "alarmhandler" << endl;
-      if (KMessageBox::warningYesNo(0L, i18n("A script on this page is causing KHTML to freeze. If it continues to run, other applications may become less responsive.\nDo you want to abort the script?"), "JavaScript", i18n("OK"), i18n("Cancel")) == KMessageBox::Yes)
-          ExecState::requestTerminate();
-  }
   KJS::ScriptInterpreter* m_script;
   bool m_debugEnabled;
 #ifndef NDEBUG
@@ -145,15 +140,12 @@ QVariant KJSProxyImpl::evaluate(QString 
   KJS::Value thisNode = n.isNull() ? Window::retrieve( m_part ) : getDOMNode(m_script->globalExec(),n);
 
   UString code( str );
-
-  void (*oldAlarmHandler)(int) = signal(SIGVTALRM, KJSProxyImpl::alarmHandler);
-  itimerval oldtv, tv = { { 10, 0 }, { 5, 0 } };
-  setitimer(ITIMER_VIRTUAL, &tv, &oldtv);
 
-  Completion comp = m_script->evaluate(code, thisNode);
-
-  setitimer(ITIMER_VIRTUAL, &oldtv, 0L);
-  signal(SIGVTALRM, oldAlarmHandler);
+  Completion comp;
+  {
+    KJSCPUGuard guard;
+    comp = m_script->evaluate(code, thisNode);
+  }
 
   bool success = ( comp.complType() == Normal ) || ( comp.complType() == ReturnValue );
 
@@ -360,4 +352,26 @@ KJSProxy * KJSProxy::proxy( KHTMLPart *p
 KJSProxy *kjs_html_init(KHTMLPart *khtmlpart)
 {
   return new KJSProxyImpl(khtmlpart);
+}
+
+KJSCPUGuard::KJSCPUGuard(unsigned int ms, unsigned int i_ms)
+{
+  oldAlarmHandler = signal(SIGVTALRM, alarmHandler);
+  itimerval tv = { 
+      { i_ms / 1000, (i_ms * 1000) % 1000000 }, 
+      { ms / 1000, (ms * 1000) % 1000000 }
+  };
+  setitimer(ITIMER_VIRTUAL, &tv, &oldtv);
+}
+
+KJSCPUGuard::~KJSCPUGuard()
+{
+  setitimer(ITIMER_VIRTUAL, &oldtv, 0L);
+  signal(SIGVTALRM, oldAlarmHandler);
+}
+
+void KJSCPUGuard::alarmHandler(int) {
+  kdDebug(6070) << "alarmhandler" << endl;
+  if (KMessageBox::warningYesNo(0L, i18n("A script on this page is causing KHTML to freeze. If it continues to run, other applications may become less responsive.\nDo you want to abort the script?"), "JavaScript", i18n("OK"), i18n("Cancel")) == KMessageBox::Yes)
+    ExecState::requestTerminate();
 }
Index: ecma/kjs_events.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/ecma/kjs_events.cpp,v
retrieving revision 1.59
diff -u -3 -p -r1.59 kjs_events.cpp
--- ecma/kjs_events.cpp	2002/08/15 12:53:04	1.59
+++ ecma/kjs_events.cpp	2002/08/21 19:14:31
@@ -88,7 +88,11 @@ void JSEventListener::handleEvent(DOM::E
     // ... and in the interpreter
     interpreter->setCurrentEvent( &evt );
 
-    Value retval = listener.call(exec, thisObj, args);
+    Value retval;
+    {
+        KJSCPUGuard guard;
+        retval = listener.call(exec, thisObj, args);
+    }
 
     if ( !scope.isEmpty() ) {
       listener.setScope( oldScope );


More information about the kfm-devel mailing list