GDBusMessage: Optimize serializer and deserializer

... by using a switch instead of if-then-else.

Signed-off-by: David Zeuthen <davidz@redhat.com>
This commit is contained in:
David Zeuthen 2011-06-14 18:11:09 -04:00
parent 7963a4cf95
commit a6d33d3a28

View File

@ -1058,6 +1058,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
GVariant *ret;
GError *local_error;
gboolean is_leaf;
const gchar *type_string;
type_string = g_variant_type_peek_string (type);
#ifdef DEBUG_SERIALIZER
if (!just_align)
@ -1076,8 +1079,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
is_leaf = TRUE;
local_error = NULL;
if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
switch (type_string[0])
{
case 'b': /* G_VARIANT_TYPE_BOOLEAN */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1088,9 +1092,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_boolean (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
{
break;
case 'y': /* G_VARIANT_TYPE_BYTE */
if (!just_align)
{
guchar v;
@ -1099,9 +1103,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_byte (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
{
break;
case 'n': /* G_VARIANT_TYPE_INT16 */
if (!ensure_input_padding (mis, 2, &local_error))
goto fail;
if (!just_align)
@ -1112,9 +1116,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_int16 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
{
break;
case 'q': /* G_VARIANT_TYPE_UINT16 */
if (!ensure_input_padding (mis, 2, &local_error))
goto fail;
if (!just_align)
@ -1125,9 +1129,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_uint16 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
{
break;
case 'i': /* G_VARIANT_TYPE_INT32 */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1138,9 +1142,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_int32 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
{
break;
case 'u': /* G_VARIANT_TYPE_UINT32 */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1151,9 +1155,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_uint32 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
{
break;
case 'x': /* G_VARIANT_TYPE_INT64 */
if (!ensure_input_padding (mis, 8, &local_error))
goto fail;
if (!just_align)
@ -1164,9 +1168,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_int64 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
{
break;
case 't': /* G_VARIANT_TYPE_UINT64 */
if (!ensure_input_padding (mis, 8, &local_error))
goto fail;
if (!just_align)
@ -1177,9 +1181,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_uint64 (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
{
break;
case 'd': /* G_VARIANT_TYPE_DOUBLE */
if (!ensure_input_padding (mis, 8, &local_error))
goto fail;
if (!just_align)
@ -1194,9 +1198,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_double (u.v_double);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
break;
case 's': /* G_VARIANT_TYPE_STRING */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1212,9 +1216,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
ret = g_variant_new_string (v);
g_free (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
{
break;
case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1240,9 +1244,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
ret = g_variant_new_object_path (v);
g_free (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
{
break;
case 'g': /* G_VARIANT_TYPE_SIGNATURE */
if (!just_align)
{
guchar len;
@ -1266,9 +1270,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
ret = g_variant_new_signature (v);
g_free (v);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
{
break;
case 'h': /* G_VARIANT_TYPE_HANDLE */
if (!ensure_input_padding (mis, 4, &local_error))
goto fail;
if (!just_align)
@ -1279,8 +1283,9 @@ parse_value_from_blob (GMemoryInputStream *mis,
goto fail;
ret = g_variant_new_handle (v);
}
}
else if (g_variant_type_is_array (type))
break;
case 'a': /* G_VARIANT_TYPE_ARRAY */
{
guint32 array_len;
goffset offset;
@ -1366,7 +1371,10 @@ parse_value_from_blob (GMemoryInputStream *mis,
g_variant_builder_clear (&builder);
}
}
else if (g_variant_type_is_dict_entry (type))
break;
default:
if (g_variant_type_is_dict_entry (type))
{
const GVariantType *key_type;
const GVariantType *value_type;
@ -1505,6 +1513,8 @@ parse_value_from_blob (GMemoryInputStream *mis,
g_free (s);
goto fail;
}
break;
}
g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
@ -1863,82 +1873,86 @@ append_value_to_blob (GVariant *value,
GError **error)
{
gsize padding_added;
const gchar *type_string;
type_string = g_variant_type_peek_string (type);
padding_added = 0;
if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
switch (type_string[0])
{
case 'b': /* G_VARIANT_TYPE_BOOLEAN */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
gboolean v = g_variant_get_boolean (value);
g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
{
break;
case 'y': /* G_VARIANT_TYPE_BYTE */
if (value != NULL)
{
guint8 v = g_variant_get_byte (value);
g_data_output_stream_put_byte (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
{
break;
case 'n': /* G_VARIANT_TYPE_INT16 */
padding_added = ensure_output_padding (mos, dos, 2);
if (value != NULL)
{
gint16 v = g_variant_get_int16 (value);
g_data_output_stream_put_int16 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
{
break;
case 'q': /* G_VARIANT_TYPE_UINT16 */
padding_added = ensure_output_padding (mos, dos, 2);
if (value != NULL)
{
guint16 v = g_variant_get_uint16 (value);
g_data_output_stream_put_uint16 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
{
break;
case 'i': /* G_VARIANT_TYPE_INT32 */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
gint32 v = g_variant_get_int32 (value);
g_data_output_stream_put_int32 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
{
break;
case 'u': /* G_VARIANT_TYPE_UINT32 */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
guint32 v = g_variant_get_uint32 (value);
g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
{
break;
case 'x': /* G_VARIANT_TYPE_INT64 */
padding_added = ensure_output_padding (mos, dos, 8);
if (value != NULL)
{
gint64 v = g_variant_get_int64 (value);
g_data_output_stream_put_int64 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
{
break;
case 't': /* G_VARIANT_TYPE_UINT64 */
padding_added = ensure_output_padding (mos, dos, 8);
if (value != NULL)
{
guint64 v = g_variant_get_uint64 (value);
g_data_output_stream_put_uint64 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
{
break;
case 'd': /* G_VARIANT_TYPE_DOUBLE */
padding_added = ensure_output_padding (mos, dos, 8);
if (value != NULL)
{
@ -1950,9 +1964,9 @@ append_value_to_blob (GVariant *value,
u.v_double = g_variant_get_double (value);
g_data_output_stream_put_uint64 (dos, u.v_uint64, NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
break;
case 's': /* G_VARIANT_TYPE_STRING */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
@ -1965,9 +1979,9 @@ append_value_to_blob (GVariant *value,
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
{
break;
case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
@ -1978,9 +1992,9 @@ append_value_to_blob (GVariant *value,
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
{
break;
case 'g': /* G_VARIANT_TYPE_SIGNATURE */
if (value != NULL)
{
gsize len;
@ -1990,17 +2004,18 @@ append_value_to_blob (GVariant *value,
g_data_output_stream_put_string (dos, v, NULL, NULL);
g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
}
}
else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
{
break;
case 'h': /* G_VARIANT_TYPE_HANDLE */
padding_added = ensure_output_padding (mos, dos, 4);
if (value != NULL)
{
gint32 v = g_variant_get_handle (value);
g_data_output_stream_put_int32 (dos, v, NULL, NULL);
}
}
else if (g_variant_type_is_array (type))
break;
case 'a': /* G_VARIANT_TYPE_ARRAY */
{
GVariant *item;
GVariantIter iter;
@ -2071,19 +2086,19 @@ append_value_to_blob (GVariant *value,
}
cur_offset = g_memory_output_stream_get_data_size (mos);
array_len = cur_offset - array_payload_begin_offset;
if (!g_seekable_seek (G_SEEKABLE (mos), array_len_offset, G_SEEK_SET, NULL, error))
goto fail;
g_data_output_stream_put_uint32 (dos, array_len, NULL, NULL);
if (!g_seekable_seek (G_SEEKABLE (mos), cur_offset, G_SEEK_SET, NULL, error))
goto fail;
}
}
else if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
break;
default:
if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
{
padding_added = ensure_output_padding (mos, dos, 8);
if (value != NULL)
@ -2140,6 +2155,8 @@ append_value_to_blob (GVariant *value,
g_variant_get_type_string (value));
goto fail;
}
break;
}
if (out_padding_added != NULL)
*out_padding_added = padding_added;