Title: [95062] trunk/Tools
Revision
95062
Author
commit-qu...@webkit.org
Date
2011-09-13 18:03:47 -0700 (Tue, 13 Sep 2011)

Log Message

GTK DumpRenderTree uses inefficient idioms to iterate over G[S]Lists
https://bugs.webkit.org/show_bug.cgi?id=68024

Patch by Leandro Pereira <lean...@profusion.mobi> on 2011-09-13
Reviewed by Gustavo Noronha Silva.

Using g_list_count() and g_list_nth_data() together on a loop is
inneficient since they're both O(n). Iterate over lists in a saner
way.

* DumpRenderTree/gtk/DumpRenderTree.cpp:
(dumpHistoryItem): Reduce the scope for the 'kids' variable, and
iterate on it using g_list_next(). Free the list after done with it.
(dumpBackForwardListForWebView): Instead of appending (which is
expensive in GLists) history items and then iterating from the tail
of the itemsToPrint list, prepend items and walk forwards as usual.
(dumpBackForwardListForAllWebViews): Walk the list in a saner way,
remove the (unneeded) viewList variable.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (95061 => 95062)


--- trunk/Tools/ChangeLog	2011-09-14 01:00:12 UTC (rev 95061)
+++ trunk/Tools/ChangeLog	2011-09-14 01:03:47 UTC (rev 95062)
@@ -1,3 +1,23 @@
+2011-09-13  Leandro Pereira  <lean...@profusion.mobi>
+
+        GTK DumpRenderTree uses inefficient idioms to iterate over G[S]Lists
+        https://bugs.webkit.org/show_bug.cgi?id=68024
+
+        Reviewed by Gustavo Noronha Silva.
+        
+        Using g_list_count() and g_list_nth_data() together on a loop is
+        inneficient since they're both O(n). Iterate over lists in a saner
+        way.
+
+        * DumpRenderTree/gtk/DumpRenderTree.cpp:
+        (dumpHistoryItem): Reduce the scope for the 'kids' variable, and
+        iterate on it using g_list_next(). Free the list after done with it.
+        (dumpBackForwardListForWebView): Instead of appending (which is
+        expensive in GLists) history items and then iterating from the tail
+        of the itemsToPrint list, prepend items and walk forwards as usual.
+        (dumpBackForwardListForAllWebViews): Walk the list in a saner way,
+        remove the (unneeded) viewList variable.
+
 2011-09-13  Ryosuke Niwa  <rn...@webkit.org>
 
         Add Eric's IRC nick.

Modified: trunk/Tools/DumpRenderTree/gtk/DumpRenderTree.cpp (95061 => 95062)


--- trunk/Tools/DumpRenderTree/gtk/DumpRenderTree.cpp	2011-09-14 01:00:12 UTC (rev 95061)
+++ trunk/Tools/DumpRenderTree/gtk/DumpRenderTree.cpp	2011-09-14 01:03:47 UTC (rev 95062)
@@ -330,12 +330,15 @@
     if (webkit_web_history_item_is_target_item(item))
         printf("  **nav target**");
     putchar('\n');
-    GList* kids = webkit_web_history_item_get_children(item);
-    if (kids) {
+
+    if (GList* kids = webkit_web_history_item_get_children(item)) {
         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
-        kids = g_list_sort(kids, (GCompareFunc) compareHistoryItems);
-        for (unsigned i = 0; i < g_list_length(kids); i++)
-            dumpHistoryItem(WEBKIT_WEB_HISTORY_ITEM(g_list_nth_data(kids, i)), indent+4, FALSE);
+        for (GList* kid = g_list_sort(kids, (GCompareFunc) compareHistoryItems); kid; kid = g_list_next(kid)) {
+            WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(kid->data);
+            dumpHistoryItem(item, indent + 4, FALSE);
+            g_object_unref(item);
+        }
+        g_list_free(kids);
     }
     g_object_unref(item);
 }
@@ -354,29 +357,28 @@
         // something is wrong if the item from the last test is in the forward part of the b/f list
         ASSERT(item != prevTestBFItem);
         g_object_ref(item);
-        itemsToPrint = g_list_append(itemsToPrint, item);
+        itemsToPrint = g_list_prepend(itemsToPrint, item);
     }
 
     WebKitWebHistoryItem* currentItem = webkit_web_back_forward_list_get_current_item(bfList);
-
     g_object_ref(currentItem);
-    itemsToPrint = g_list_append(itemsToPrint, currentItem);
+    itemsToPrint = g_list_prepend(itemsToPrint, currentItem);
 
-    gint currentItemIndex = g_list_length(itemsToPrint) - 1;
     gint backListCount = webkit_web_back_forward_list_get_back_length(bfList);
     for (int i = -1; i >= -(backListCount); i--) {
         WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_nth_item(bfList, i);
         if (item == prevTestBFItem)
             break;
         g_object_ref(item);
-        itemsToPrint = g_list_append(itemsToPrint, item);
+        itemsToPrint = g_list_prepend(itemsToPrint, item);
     }
 
-    for (int i = g_list_length(itemsToPrint) - 1; i >= 0; i--) {
-        WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(g_list_nth_data(itemsToPrint, i));
-        dumpHistoryItem(item, historyItemIndent, i == currentItemIndex);
+    for (GList* itemToPrint = itemsToPrint; itemToPrint; itemToPrint = g_list_next(itemToPrint)) {
+        WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(itemToPrint->data);
+        dumpHistoryItem(item, historyItemIndent, item == currentItem);
         g_object_unref(item);
     }
+
     g_list_free(itemsToPrint);
     printf("===============================================\n");
 }
@@ -387,9 +389,8 @@
     dumpBackForwardListForWebView(webView);
 
     // The view list is prepended. Reverse the list so we get the order right.
-    GSList* viewList = g_slist_reverse(webViewList);
-    for (unsigned i = 0; i < g_slist_length(viewList); ++i)
-        dumpBackForwardListForWebView(WEBKIT_WEB_VIEW(g_slist_nth_data(viewList, i)));
+    for (GSList* currentView = g_slist_reverse(webViewList); currentView; currentView = g_slist_next(currentView))
+        dumpBackForwardListForWebView(WEBKIT_WEB_VIEW(currentView->data));
 }
 
 static void invalidateAnyPreviousWaitToDumpWatchdog()
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to