Просмотр исходного кода

Use lazy_static to cache datetime formats

One of those two date formats was re-compiled before any date was displayed. Now they are compiled only the first time they're used, and cached versions are used thereafter, resulting in a speedup.
Ben S 10 лет назад
Родитель
Сommit
e07992d08c
4 измененных файлов с 44 добавлено и 29 удалено
  1. 22 16
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 3 4
      src/main.rs
  4. 17 8
      src/output/details.rs

+ 22 - 16
Cargo.lock

@@ -7,7 +7,8 @@ dependencies = [
  "datetime 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "locale 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "natord 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "num_cpus 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -23,7 +24,7 @@ name = "advapi32-sys"
 version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -32,7 +33,7 @@ name = "aho-corasick"
 version = "0.3.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "memchr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -47,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "byteorder"
-version = "0.3.13"
+version = "0.4.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
@@ -77,7 +78,7 @@ version = "0.3.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -96,6 +97,11 @@ dependencies = [
  "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "lazy_static"
+version = "0.1.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "libc"
 version = "0.1.12"
@@ -103,7 +109,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "libc"
-version = "0.2.0"
+version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
@@ -165,10 +171,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "memchr"
-version = "0.1.6"
+version = "0.1.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -181,7 +187,7 @@ name = "num"
 version = "0.1.27"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -235,12 +241,12 @@ dependencies = [
 
 [[package]]
 name = "rand"
-version = "0.3.11"
+version = "0.3.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
- "winapi 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -249,7 +255,7 @@ version = "0.1.41"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "aho-corasick 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
- "memchr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -289,7 +295,7 @@ name = "tempdir"
 version = "0.3.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -305,7 +311,7 @@ name = "tz"
 version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -332,7 +338,7 @@ dependencies = [
 
 [[package]]
 name = "winapi"
-version = "0.2.4"
+version = "0.2.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]

+ 2 - 1
Cargo.toml

@@ -11,12 +11,13 @@ ansi_term = "0.7.0"
 bitflags = "0.1"
 datetime = "0.4.1"
 getopts = "0.2.14"
+lazy_static = "0.1.*"
 libc = "0.2.0"
 locale = "0.1.2"
 natord = "1.0.7"
 num_cpus = "0.2.7"
 number_prefix = "0.2.3"
-scoped_threadpool = "*"
+scoped_threadpool = "0.1.*"
 term_grid = "0.1.2"
 unicode-width = "0.1.3"
 users = "0.4.3"

+ 3 - 4
src/main.rs

@@ -1,5 +1,5 @@
 #![warn(trivial_casts, trivial_numeric_casts)]
-#![warn(unused_extern_crates, unused_qualifications)]
+#![warn(unused_qualifications)]
 #![warn(unused_results)]
 
 extern crate ansi_term;
@@ -15,9 +15,8 @@ extern crate term_grid;
 extern crate unicode_width;
 extern crate users;
 
-#[cfg(feature="git")]
-extern crate git2;
-
+#[cfg(feature="git")] extern crate git2;
+#[macro_use] extern crate lazy_static;
 
 use std::env;
 use std::path::{Component, Path};

+ 17 - 8
src/output/details.rs

@@ -129,13 +129,11 @@ use output::column::{Alignment, Column, Columns, Cell, SizeFormat};
 use ansi_term::{ANSIString, ANSIStrings, Style};
 
 use datetime::local::{LocalDateTime, DatePiece};
-use datetime::format::{DateFormat};
-use datetime::zoned::{TimeZone};
+use datetime::format::DateFormat;
+use datetime::zoned::TimeZone;
 
 use locale;
 
-use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
-
 use users::{OSUsers, Users};
 use users::mock::MockUsers;
 
@@ -562,6 +560,8 @@ impl<U> Table<U> where U: Users {
     }
 
     fn render_size(&self, size: f::Size, size_format: SizeFormat) -> Cell {
+        use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
+
         if let f::Size::Some(offset) = size {
             let result = match size_format {
                 SizeFormat::DecimalBytes  => decimal_prefix(offset as f64),
@@ -591,14 +591,14 @@ impl<U> Table<U> where U: Users {
     fn render_time(&self, timestamp: f::Time) -> Cell {
         let date = self.tz.at(LocalDateTime::at(timestamp.0 as i64));
 
-        let format = if date.year() == self.current_year {
-                DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap()
+        let datestamp = if date.year() == self.current_year {
+                DATE_AND_TIME.format(&date, &self.time)
             }
             else {
-                DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
+                DATE_AND_YEAR.format(&date, &self.time)
             };
 
-        Cell::paint(self.colours.date, &format.format(&date, &self.time))
+        Cell::paint(self.colours.date, &datestamp)
     }
 
     fn render_git_status(&self, git: f::Git) -> Cell {
@@ -753,6 +753,15 @@ impl TreePart {
 }
 
 
+lazy_static! {
+    static ref DATE_AND_TIME: DateFormat<'static> =
+        DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap();
+
+    static ref DATE_AND_YEAR: DateFormat<'static> =
+        DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap();
+}
+
+
 #[cfg(test)]
 pub mod test {
     pub use super::Table;