aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/dbus/dbus-1.3.0/will-2.patch
blob: e1c756bd6479312281cc77c2a927f060a4adf9e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
From 2d4da5596b34cbd5de2b97971fbb484657a1f485 Mon Sep 17 00:00:00 2001
From: Will Thompson <will.thompson@collabora.co.uk>
Date: Wed, 29 Jul 2009 17:48:21 +0100
Subject: [PATCH 2/6] Index match rules by message type

This avoids scanning all the signal matches while dispatching method
calls and returns, which are rarely matched against.
---
 bus/signals.c |  217 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 139 insertions(+), 78 deletions(-)

diff --git a/bus/signals.c b/bus/signals.c
index b020a76..10e0b5e 100644
--- a/bus/signals.c
+++ b/bus/signals.c
@@ -1022,7 +1022,11 @@ struct BusMatchmaker
 {
   int refcount;
 
-  DBusList *all_rules;
+  /* lists of rules, grouped by the type of message they match. 0
+   * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message
+   * type.
+   */
+  DBusList *rules_by_type[DBUS_NUM_MESSAGE_TYPES];
 };
 
 BusMatchmaker*
@@ -1039,6 +1043,16 @@ bus_matchmaker_new (void)
   return matchmaker;
 }
 
+static DBusList **
+bus_matchmaker_get_rules (BusMatchmaker *matchmaker,
+                          int            message_type)
+{
+  _dbus_assert (message_type >= 0);
+  _dbus_assert (message_type < DBUS_NUM_MESSAGE_TYPES);
+
+  return matchmaker->rules_by_type + message_type;
+}
+
 BusMatchmaker *
 bus_matchmaker_ref (BusMatchmaker *matchmaker)
 {
@@ -1057,14 +1071,20 @@ bus_matchmaker_unref (BusMatchmaker *matchmaker)
   matchmaker->refcount -= 1;
   if (matchmaker->refcount == 0)
     {
-      while (matchmaker->all_rules != NULL)
+      int i;
+
+      for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
         {
-          BusMatchRule *rule;
+          DBusList **rules = bus_matchmaker_get_rules (matchmaker, i);
+
+          while (*rules != NULL)
+            {
+              BusMatchRule *rule;
 
-          rule = matchmaker->all_rules->data;
-          bus_match_rule_unref (rule);
-          _dbus_list_remove_link (&matchmaker->all_rules,
-                                  matchmaker->all_rules);
+              rule = (*rules)->data;
+              bus_match_rule_unref (rule);
+              _dbus_list_remove_link (rules, *rules);
+            }
         }
 
       dbus_free (matchmaker);
@@ -1076,14 +1096,18 @@ dbus_bool_t
 bus_matchmaker_add_rule (BusMatchmaker   *matchmaker,
                          BusMatchRule    *rule)
 {
+  DBusList **rules;
+
   _dbus_assert (bus_connection_is_active (rule->matches_go_to));
 
-  if (!_dbus_list_append (&matchmaker->all_rules, rule))
+  rules = bus_matchmaker_get_rules (matchmaker, rule->message_type);
+
+  if (!_dbus_list_append (rules, rule))
     return FALSE;
 
   if (!bus_connection_add_match_rule (rule->matches_go_to, rule))
     {
-      _dbus_list_remove_last (&matchmaker->all_rules, rule);
+      _dbus_list_remove_last (rules, rule);
       return FALSE;
     }
   
@@ -1171,13 +1195,13 @@ match_rule_equal (BusMatchRule *a,
 }
 
 static void
-bus_matchmaker_remove_rule_link (BusMatchmaker   *matchmaker,
+bus_matchmaker_remove_rule_link (DBusList       **rules,
                                  DBusList        *link)
 {
   BusMatchRule *rule = link->data;
   
   bus_connection_remove_match_rule (rule->matches_go_to, rule);
-  _dbus_list_remove_link (&matchmaker->all_rules, link);
+  _dbus_list_remove_link (rules, link);
 
 #ifdef DBUS_ENABLE_VERBOSE_MODE
   {
@@ -1196,8 +1220,12 @@ void
 bus_matchmaker_remove_rule (BusMatchmaker   *matchmaker,
                             BusMatchRule    *rule)
 {
+  DBusList **rules;
+
   bus_connection_remove_match_rule (rule->matches_go_to, rule);
-  _dbus_list_remove (&matchmaker->all_rules, rule);
+
+  rules = bus_matchmaker_get_rules (matchmaker, rule->message_type);
+  _dbus_list_remove (rules, rule);
 
 #ifdef DBUS_ENABLE_VERBOSE_MODE
   {
@@ -1219,24 +1247,26 @@ bus_matchmaker_remove_rule_by_value (BusMatchmaker   *matchmaker,
                                      DBusError       *error)
 {
   /* FIXME this is an unoptimized linear scan */
-
+  DBusList **rules;
   DBusList *link;
 
+  rules = bus_matchmaker_get_rules (matchmaker, value->message_type);
+
   /* we traverse backward because bus_connection_remove_match_rule()
    * removes the most-recently-added rule
    */
-  link = _dbus_list_get_last_link (&matchmaker->all_rules);
+  link = _dbus_list_get_last_link (rules);
   while (link != NULL)
     {
       BusMatchRule *rule;
       DBusList *prev;
 
       rule = link->data;
-      prev = _dbus_list_get_prev_link (&matchmaker->all_rules, link);
+      prev = _dbus_list_get_prev_link (rules, link);
 
       if (match_rule_equal (rule, value))
         {
-          bus_matchmaker_remove_rule_link (matchmaker, link);
+          bus_matchmaker_remove_rule_link (rules, link);
           break;
         }
 
@@ -1258,6 +1288,7 @@ bus_matchmaker_disconnected (BusMatchmaker   *matchmaker,
                              DBusConnection  *disconnected)
 {
   DBusList *link;
+  int i;
 
   /* FIXME
    *
@@ -1270,41 +1301,46 @@ bus_matchmaker_disconnected (BusMatchmaker   *matchmaker,
   
   _dbus_assert (bus_connection_is_active (disconnected));
 
-  link = _dbus_list_get_first_link (&matchmaker->all_rules);
-  while (link != NULL)
+  for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
     {
-      BusMatchRule *rule;
-      DBusList *next;
+      DBusList **rules = bus_matchmaker_get_rules (matchmaker, i);
 
-      rule = link->data;
-      next = _dbus_list_get_next_link (&matchmaker->all_rules, link);
-
-      if (rule->matches_go_to == disconnected)
-        {
-          bus_matchmaker_remove_rule_link (matchmaker, link);
-        }
-      else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
-               ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
+      link = _dbus_list_get_first_link (rules);
+      while (link != NULL)
         {
-          /* The rule matches to/from a base service, see if it's the
-           * one being disconnected, since we know this service name
-           * will never be recycled.
-           */
-          const char *name;
-
-          name = bus_connection_get_name (disconnected);
-          _dbus_assert (name != NULL); /* because we're an active connection */
-
-          if (((rule->flags & BUS_MATCH_SENDER) &&
-               strcmp (rule->sender, name) == 0) ||
-              ((rule->flags & BUS_MATCH_DESTINATION) &&
-               strcmp (rule->destination, name) == 0))
+          BusMatchRule *rule;
+          DBusList *next;
+
+          rule = link->data;
+          next = _dbus_list_get_next_link (rules, link);
+
+          if (rule->matches_go_to == disconnected)
             {
-              bus_matchmaker_remove_rule_link (matchmaker, link);
+              bus_matchmaker_remove_rule_link (rules, link);
+            }
+          else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
+                   ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
+            {
+              /* The rule matches to/from a base service, see if it's the
+               * one being disconnected, since we know this service name
+               * will never be recycled.
+               */
+              const char *name;
+
+              name = bus_connection_get_name (disconnected);
+              _dbus_assert (name != NULL); /* because we're an active connection */
+
+              if (((rule->flags & BUS_MATCH_SENDER) &&
+                   strcmp (rule->sender, name) == 0) ||
+                  ((rule->flags & BUS_MATCH_DESTINATION) &&
+                   strcmp (rule->destination, name) == 0))
+                {
+                  bus_matchmaker_remove_rule_link (rules, link);
+                }
             }
-        }
 
-      link = next;
+          link = next;
+        }
     }
 }
 
@@ -1504,38 +1540,16 @@ match_rule_matches (BusMatchRule    *rule,
   return TRUE;
 }
 
-dbus_bool_t
-bus_matchmaker_get_recipients (BusMatchmaker   *matchmaker,
-                               BusConnections  *connections,
-                               DBusConnection  *sender,
-                               DBusConnection  *addressed_recipient,
-                               DBusMessage     *message,
-                               DBusList       **recipients_p)
+static dbus_bool_t
+get_recipients_from_list (DBusList       **rules,
+                          DBusConnection  *sender,
+                          DBusConnection  *addressed_recipient,
+                          DBusMessage     *message,
+                          DBusList       **recipients_p)
 {
-  /* FIXME for now this is a wholly unoptimized linear search */
-  /* Guessing the important optimization is to skip the signal-related
-   * match lists when processing method call and exception messages.
-   * So separate match rule lists for signals?
-   */
-  
   DBusList *link;
 
-  _dbus_assert (*recipients_p == NULL);
-
-  /* This avoids sending same message to the same connection twice.
-   * Purpose of the stamp instead of a bool is to avoid iterating over
-   * all connections resetting the bool each time.
-   */
-  bus_connections_increment_stamp (connections);
-
-  /* addressed_recipient is already receiving the message, don't add to list.
-   * NULL addressed_recipient means either bus driver, or this is a signal
-   * and thus lacks a specific addressed_recipient.
-   */
-  if (addressed_recipient != NULL)
-    bus_connection_mark_stamp (addressed_recipient);
-
-  link = _dbus_list_get_first_link (&matchmaker->all_rules);
+  link = _dbus_list_get_first_link (rules);
   while (link != NULL)
     {
       BusMatchRule *rule;
@@ -1545,7 +1559,7 @@ bus_matchmaker_get_recipients (BusMatchmaker   *matchmaker,
 #ifdef DBUS_ENABLE_VERBOSE_MODE
       {
         char *s = match_rule_to_string (rule);
-        
+
         _dbus_verbose ("Checking whether message matches rule %s for connection %p\n",
                        s, rule->matches_go_to);
         dbus_free (s);
@@ -1556,12 +1570,12 @@ bus_matchmaker_get_recipients (BusMatchmaker   *matchmaker,
                               sender, addressed_recipient, message))
         {
           _dbus_verbose ("Rule matched\n");
-          
+
           /* Append to the list if we haven't already */
           if (bus_connection_mark_stamp (rule->matches_go_to))
             {
               if (!_dbus_list_append (recipients_p, rule->matches_go_to))
-                goto nomem;
+                return FALSE;
             }
 #ifdef DBUS_ENABLE_VERBOSE_MODE
           else
@@ -1571,10 +1585,57 @@ bus_matchmaker_get_recipients (BusMatchmaker   *matchmaker,
 #endif /* DBUS_ENABLE_VERBOSE_MODE */
         }
 
-      link = _dbus_list_get_next_link (&matchmaker->all_rules, link);
+      link = _dbus_list_get_next_link (rules, link);
     }
 
   return TRUE;
+}
+
+dbus_bool_t
+bus_matchmaker_get_recipients (BusMatchmaker   *matchmaker,
+                               BusConnections  *connections,
+                               DBusConnection  *sender,
+                               DBusConnection  *addressed_recipient,
+                               DBusMessage     *message,
+                               DBusList       **recipients_p)
+{
+  /* FIXME for now this is a wholly unoptimized linear search */
+  /* Guessing the important optimization is to skip the signal-related
+   * match lists when processing method call and exception messages.
+   * So separate match rule lists for signals?
+   */
+  int type;
+
+  _dbus_assert (*recipients_p == NULL);
+
+  /* This avoids sending same message to the same connection twice.
+   * Purpose of the stamp instead of a bool is to avoid iterating over
+   * all connections resetting the bool each time.
+   */
+  bus_connections_increment_stamp (connections);
+
+  /* addressed_recipient is already receiving the message, don't add to list.
+   * NULL addressed_recipient means either bus driver, or this is a signal
+   * and thus lacks a specific addressed_recipient.
+   */
+  if (addressed_recipient != NULL)
+    bus_connection_mark_stamp (addressed_recipient);
+
+  /* We always need to try the rules that don't specify a message type */
+  if (!get_recipients_from_list (
+          bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID),
+          sender, addressed_recipient, message, recipients_p))
+    goto nomem;
+
+  /* Also try rules that match the type of this message */
+  type = dbus_message_get_type (message);
+  if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES)
+    if (!get_recipients_from_list (
+            bus_matchmaker_get_rules (matchmaker, type),
+            sender, addressed_recipient, message, recipients_p))
+      goto nomem;
+
+  return TRUE;
 
  nomem:
   _dbus_list_clear (recipients_p);
-- 
1.6.3.3