From owner-svn-src-stable@FreeBSD.ORG Mon Mar 4 16:50:01 2013 Return-Path: Delivered-To: svn-src-stable@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id CFE4B44A; Mon, 4 Mar 2013 16:50:01 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id C253C7CB; Mon, 4 Mar 2013 16:50:01 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.5/8.14.5) with ESMTP id r24Go1PK026357; Mon, 4 Mar 2013 16:50:01 GMT (envelope-from ian@svn.freebsd.org) Received: (from ian@localhost) by svn.freebsd.org (8.14.6/8.14.5/Submit) id r24Go1M9026353; Mon, 4 Mar 2013 16:50:01 GMT (envelope-from ian@svn.freebsd.org) Message-Id: <201303041650.r24Go1M9026353@svn.freebsd.org> From: Ian Lepore Date: Mon, 4 Mar 2013 16:50:01 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r247803 - stable/9/sbin/devd X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Mar 2013 16:50:01 -0000 Author: ian Date: Mon Mar 4 16:50:01 2013 New Revision: 247803 URL: http://svnweb.freebsd.org/changeset/base/247803 Log: MFC r246134 ... Improve devd startup time, by tweaking some string handling routines that are heavily used when parsing config files. Mostly these changes avoid making temporary copies of the strings, and avoid doing byte at a time append operations, on the most-used code path. On a 1.2 GHz ARM processor this reduces the time to parse the config files from 13 to 6 seconds. Modified: stable/9/sbin/devd/devd.cc stable/9/sbin/devd/devd.hh Directory Properties: stable/9/sbin/devd/ (props changed) Modified: stable/9/sbin/devd/devd.cc ============================================================================== --- stable/9/sbin/devd/devd.cc Mon Mar 4 16:44:19 2013 (r247802) +++ stable/9/sbin/devd/devd.cc Mon Mar 4 16:50:01 2013 (r247803) @@ -137,7 +137,7 @@ config cfg; event_proc::event_proc() : _prio(-1) { - // nothing + _epsvec.reserve(4); } event_proc::~event_proc() @@ -241,25 +241,18 @@ my_system(const char *command) bool action::do_action(config &c) { - string s = c.expand_string(_cmd); + string s = c.expand_string(_cmd.c_str()); if (Dflag) fprintf(stderr, "Executing '%s'\n", s.c_str()); my_system(s.c_str()); return (true); } -match::match(config &c, const char *var, const char *re) - : _var(var), _re("^") +match::match(config &c, const char *var, const char *re) : + _inv(re[0] == '!'), + _var(var), + _re(c.expand_string(_inv ? re + 1 : re, "^", "$")) { - if (!c.expand_string(string(re)).empty() && - c.expand_string(string(re)).at(0) == '!') { - _re.append(c.expand_string(string(re)).substr(1)); - _inv = 1; - } else { - _re.append(c.expand_string(string(re))); - _inv = 0; - } - _re.append("$"); regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE); } @@ -625,24 +618,37 @@ config::expand_one(const char *&src, str do { buffer.append(src++, 1); } while (is_id_char(*src)); - buffer.append("", 1); dst.append(get_variable(buffer.c_str())); } const string -config::expand_string(const string &s) +config::expand_string(const char *src, const char *prepend, const char *append) { - const char *src; + const char *var_at; string dst; - src = s.c_str(); - while (*src) { - if (*src == '$') - expand_one(src, dst); - else - dst.append(src++, 1); + /* + * 128 bytes is enough for 2427 of 2438 expansions that happen + * while parsing config files, as tested on 2013-01-30. + */ + dst.reserve(128); + + if (prepend != NULL) + dst = prepend; + + for (;;) { + var_at = strchr(src, '$'); + if (var_at == NULL) { + dst.append(src); + break; + } + dst.append(src, var_at - src); + src = var_at; + expand_one(src, dst); } - dst.append("", 1); + + if (append != NULL) + dst.append(append); return (dst); } Modified: stable/9/sbin/devd/devd.hh ============================================================================== --- stable/9/sbin/devd/devd.hh Mon Mar 4 16:44:19 2013 (r247802) +++ stable/9/sbin/devd/devd.hh Mon Mar 4 16:50:01 2013 (r247803) @@ -90,9 +90,9 @@ public: virtual bool do_match(config &); virtual bool do_action(config &) { return true; } private: + bool _inv; std::string _var; std::string _re; - bool _inv; regex_t _regex; }; @@ -162,7 +162,8 @@ public: void pop_var_table(); void set_variable(const char *var, const char *val); const std::string &get_variable(const std::string &var); - const std::string expand_string(const std::string &var); + const std::string expand_string(const char * var, + const char * prepend = NULL, const char * append = NULL); char *set_vars(char *); void find_and_execute(char); protected: