[rygel] core: Pre-compile regex for substitute variables
- From: Zeeshan Ali Khattak <zeeshanak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [rygel] core: Pre-compile regex for substitute variables
- Date: Fri, 20 Aug 2010 19:57:31 +0000 (UTC)
commit c98f8c5f95e787467c7d04db6c2cdcb727912efd
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date: Fri Aug 20 22:40:01 2010 +0300
core: Pre-compile regex for substitute variables
Apparently these string comparisons were taking a hell lot of time and the
reason was that string.replace() compiles regex each time it needs to
replace a string.
src/rygel/rygel-media-object.vala | 39 +++++++++++++++++++++++++++++++-----
1 files changed, 33 insertions(+), 6 deletions(-)
---
diff --git a/src/rygel/rygel-media-object.vala b/src/rygel/rygel-media-object.vala
index 6b3a01e..c106e11 100644
--- a/src/rygel/rygel-media-object.vala
+++ b/src/rygel/rygel-media-object.vala
@@ -27,6 +27,10 @@ using Gee;
* Represents a media object (container and item).
*/
public abstract class Rygel.MediaObject : GLib.Object {
+ private static Regex real_name_regex;
+ private static Regex user_name_regex;
+ private static Regex host_name_regex;
+
public string id;
public string upnp_class;
public uint64 modified;
@@ -60,12 +64,35 @@ public abstract class Rygel.MediaObject : GLib.Object {
}
set {
- this._title = value.replace ("@REALNAME@",
- Environment.get_real_name ());
- _title = _title.replace ("@USERNAME@",
- Environment.get_user_name ());
- _title = _title.replace ("@HOSTNAME@",
- Environment.get_host_name ());
+ try {
+ this._title = real_name_regex.replace_literal (
+ value,
+ -1,
+ 0,
+ Environment.get_real_name ());
+ this._title = user_name_regex.replace_literal (
+ this._title,
+ -1,
+ 0,
+ Environment.get_user_name ());
+ this._title = host_name_regex.replace_literal (
+ this._title,
+ -1,
+ 0,
+ Environment.get_host_name ());
+ } catch (GLib.RegexError err) {
+ assert_not_reached ();
+ }
+ }
+ }
+
+ static construct {
+ try {
+ real_name_regex = new Regex (Regex.escape_string ("@REALNAME@"));
+ user_name_regex = new Regex (Regex.escape_string ("@USERNAME@"));
+ host_name_regex = new Regex (Regex.escape_string ("@HOSTNAME@"));
+ } catch (GLib.RegexError err) {
+ assert_not_reached ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]