diff options
author | Thomas Haller <thaller@redhat.com> | 2021-06-21 14:52:47 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-06-21 15:08:35 +0200 |
commit | 58c3af1a7d646ee19e592b4ce85fa126cf95e5ca (patch) | |
tree | c5f89df40e9e27887127c692e84d1332c2f4f54e | |
parent | 599565331256524a1d1bfddfc959db766a19deb8 (diff) |
libnm: pre-allocate buffer in _nm_sett_info_property_override_create_array()
The buffer created here is only temporary to construct the property info
by _nm_setting_class_commit_full(). We can afford to allocate more than
necessary, if we thereby avoid several reallocations.
-rw-r--r-- | src/libnm-core-impl/nm-setting-private.h | 11 | ||||
-rw-r--r-- | src/libnm-core-impl/nm-setting.c | 31 |
2 files changed, 27 insertions, 15 deletions
diff --git a/src/libnm-core-impl/nm-setting-private.h b/src/libnm-core-impl/nm-setting-private.h index e8373f8e63..7d237dc4c7 100644 --- a/src/libnm-core-impl/nm-setting-private.h +++ b/src/libnm-core-impl/nm-setting-private.h @@ -279,9 +279,18 @@ gboolean _nm_setting_property_is_regular_secret_flags(NMSetting * setting, /*****************************************************************************/ static inline GArray * +_nm_sett_info_property_override_create_array_sized(guint reserved_size) +{ + return g_array_sized_new(FALSE, FALSE, sizeof(NMSettInfoProperty), reserved_size); +} + +static inline GArray * _nm_sett_info_property_override_create_array(void) { - return g_array_new(FALSE, FALSE, sizeof(NMSettInfoProperty)); + /* pre-allocate a relatively large buffer to avoid frequent re-allocations. + * Note that the buffer is only short-lived and will be destroyed by + * _nm_setting_class_commit_full(). */ + return _nm_sett_info_property_override_create_array_sized(20); } GArray *_nm_sett_info_property_override_create_array_ip_config(void); diff --git a/src/libnm-core-impl/nm-setting.c b/src/libnm-core-impl/nm-setting.c index 2e373a5ab0..ec4557cc60 100644 --- a/src/libnm-core-impl/nm-setting.c +++ b/src/libnm-core-impl/nm-setting.c @@ -296,7 +296,9 @@ _nm_setting_class_commit_full(NMSettingClass * setting_class, { NMSettInfoSetting *sett_info; gs_free GParamSpec **property_specs = NULL; - guint i, n_property_specs, override_len; + guint n_property_specs; + guint override_len; + guint i; nm_assert(NM_IS_SETTING_CLASS(setting_class)); nm_assert(!setting_class->setting_info); @@ -309,25 +311,26 @@ _nm_setting_class_commit_full(NMSettingClass * setting_class, nm_assert(!sett_info->property_infos_len); nm_assert(!sett_info->property_infos); + property_specs = + g_object_class_list_properties(G_OBJECT_CLASS(setting_class), &n_property_specs); + if (!properties_override) { override_len = 0; - properties_override = _nm_sett_info_property_override_create_array(); - } else + properties_override = _nm_sett_info_property_override_create_array_sized(n_property_specs); + } else { override_len = properties_override->len; - property_specs = - g_object_class_list_properties(G_OBJECT_CLASS(setting_class), &n_property_specs); - - for (i = 0; i < properties_override->len; i++) { - NMSettInfoProperty *p = &g_array_index(properties_override, NMSettInfoProperty, i); + for (i = 0; i < override_len; i++) { + NMSettInfoProperty *p = &g_array_index(properties_override, NMSettInfoProperty, i); - nm_assert((!!p->name) != (!!p->param_spec)); + nm_assert((!!p->name) != (!!p->param_spec)); - if (!p->name) { - nm_assert(p->param_spec); - p->name = p->param_spec->name; - } else - nm_assert(!p->param_spec); + if (!p->name) { + nm_assert(p->param_spec); + p->name = p->param_spec->name; + } else + nm_assert(!p->param_spec); + } } #if NM_MORE_ASSERTS > 10 |