[folks] collection comparators: optimize the case of empty collections
- From: Simon McVittie <smcv src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [folks] collection comparators: optimize the case of empty collections
- Date: Wed, 13 Mar 2013 12:59:10 +0000 (UTC)
commit f2a0df2d2262c69d23bccf212f3564ac5aa1f81c
Author: Simon McVittie <simon mcvittie collabora co uk>
Date: Tue Mar 12 15:27:32 2013 +0000
collection comparators: optimize the case of empty collections
Comparing integers is a lot faster than constructing one or more
GObjects to iterate over the hash table. This saves about 3% of the
user CPU time needed to deal with 2049 simple e-d-s Google contacts,
each with one email address and no phone numbers etc.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=695648
Signed-off-by: Simon McVittie <simon mcvittie collabora co uk>
Reviewed-by: Philip Withnall <philip tecnocode co uk>
folks/utils.vala | 30 +++++++++++++++++++++++++++---
1 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/folks/utils.vala b/folks/utils.vala
index a513f83..4225a05 100644
--- a/folks/utils.vala
+++ b/folks/utils.vala
@@ -74,7 +74,15 @@ public class Folks.Utils : Object
if (a == b)
return true;
- if (a.size == b.size)
+ var a_size = a.size;
+ var b_size = b.size;
+
+ if (a_size == 0 && b_size == 0)
+ {
+ /* fast path: avoid the actual iteration, which creates GObjects */
+ return true;
+ }
+ else if (a_size == b_size)
{
foreach (var key in a.get_keys ())
{
@@ -125,7 +133,15 @@ public class Folks.Utils : Object
if (a == b)
return true;
- if (a.size == b.size)
+ var a_size = a.size;
+ var b_size = b.size;
+
+ if (a_size == 0 && b_size == 0)
+ {
+ /* fast path: avoid the actual iteration, which creates GObjects */
+ return true;
+ }
+ else if (a_size == b_size)
{
foreach (var key in a.get_keys ())
{
@@ -176,7 +192,15 @@ public class Folks.Utils : Object
if (a == b)
return true;
- if (a.size == b.size)
+ var a_size = a.size;
+ var b_size = b.size;
+
+ if (a_size == 0 && b_size == 0)
+ {
+ /* fast path: avoid creating the iterator, which is a GObject */
+ return true;
+ }
+ else if (a_size == b_size)
{
foreach (var val in a)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]