-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add benchmarks ; add explicit #[inline] annotations
This diff adds benchmarks to get more info regarding Issue #1. It appears that the remaining difference between the "simple" case and the "cargo" case is the result of a difference in performance between using `match` and `if` for tight loops. I suspect it's because of the way that match arms get reordered: if I manually reorder the "if" statement, I can reproduce the match performance. Also added a couple #[inline] annotations in tables.rs, though the difference in performance in my measurements is negligible. Bumped version number to 0.1.1.
- Loading branch information
Showing
8 changed files
with
169 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
target | ||
Cargo.lock | ||
scripts/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
|
||
name = "unicode-width" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
authors = ["kwantam <[email protected]>"] | ||
|
||
homepage = "https://github.com/unicode-rs/unicode-width" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,5 @@ to your `Cargo.toml`: | |
|
||
```toml | ||
[dependencies] | ||
unicode-width = "0.1.0" | ||
unicode-width = "0.1.1" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::iter; | ||
use test::{self, Bencher}; | ||
|
||
use super::UnicodeWidthChar; | ||
|
||
#[cfg(feature = "no_std")] | ||
use std::prelude::v1::*; | ||
|
||
#[bench] | ||
fn cargo(b: &mut Bencher) { | ||
let string = iter::repeat('a').take(4096).collect::<String>(); | ||
|
||
b.iter(|| { | ||
for c in string.chars() { | ||
test::black_box(UnicodeWidthChar::width(c)); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn stdlib(b: &mut Bencher) { | ||
let string = iter::repeat('a').take(4096).collect::<String>(); | ||
|
||
b.iter(|| { | ||
for c in string.chars() { | ||
test::black_box(c.width(false)); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn simple_if(b: &mut Bencher) { | ||
let string = iter::repeat('a').take(4096).collect::<String>(); | ||
|
||
b.iter(|| { | ||
for c in string.chars() { | ||
test::black_box(simple_width_if(c)); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn simple_match(b: &mut Bencher) { | ||
let string = iter::repeat('a').take(4096).collect::<String>(); | ||
|
||
b.iter(|| { | ||
for c in string.chars() { | ||
test::black_box(simple_width_match(c)); | ||
} | ||
}); | ||
} | ||
|
||
#[inline] | ||
fn simple_width_if(c: char) -> Option<usize> { | ||
let cu = c as u32; | ||
if cu < 127 { | ||
if cu > 31 { | ||
Some(1) | ||
} else if cu == 0 { | ||
Some(0) | ||
} else { | ||
None | ||
} | ||
} else { | ||
UnicodeWidthChar::width(c) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn simple_width_match(c: char) -> Option<usize> { | ||
match c as u32 { | ||
cu if cu == 0 => Some(0), | ||
cu if cu < 0x20 => None, | ||
cu if cu < 0x7f => Some(1), | ||
_ => UnicodeWidthChar::width(c) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_str() { | ||
use super::UnicodeWidthStr; | ||
|
||
assert_eq!(UnicodeWidthStr::width("hello"), 10); | ||
assert_eq!("hello".width_cjk(), 10); | ||
assert_eq!(UnicodeWidthStr::width("\0\0\0\x01\x01"), 0); | ||
assert_eq!("\0\0\0\x01\x01".width_cjk(), 0); | ||
assert_eq!(UnicodeWidthStr::width(""), 0); | ||
assert_eq!("".width_cjk(), 0); | ||
assert_eq!(UnicodeWidthStr::width("\u{2081}\u{2082}\u{2083}\u{2084}"), 4); | ||
assert_eq!("\u{2081}\u{2082}\u{2083}\u{2084}".width_cjk(), 8); | ||
} | ||
|
||
#[test] | ||
fn test_char() { | ||
use super::UnicodeWidthChar; | ||
#[cfg(feature = "no_std")] | ||
use core::option::Option::{Some, None}; | ||
|
||
assert_eq!(UnicodeWidthChar::width('h'), Some(2)); | ||
assert_eq!('h'.width_cjk(), Some(2)); | ||
assert_eq!(UnicodeWidthChar::width('\x00'), Some(0)); | ||
assert_eq!('\x00'.width_cjk(), Some(0)); | ||
assert_eq!(UnicodeWidthChar::width('\x01'), None); | ||
assert_eq!('\x01'.width_cjk(), None); | ||
assert_eq!(UnicodeWidthChar::width('\u{2081}'), Some(1)); | ||
assert_eq!('\u{2081}'.width_cjk(), Some(2)); | ||
} | ||
|
||
#[test] | ||
fn test_char2() { | ||
use super::UnicodeWidthChar; | ||
#[cfg(feature = "no_std")] | ||
use core::option::Option::{Some, None}; | ||
|
||
assert_eq!(UnicodeWidthChar::width('\x00'),Some(0)); | ||
assert_eq!('\x00'.width_cjk(),Some(0)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('\x0A'),None); | ||
assert_eq!('\x0A'.width_cjk(),None); | ||
|
||
assert_eq!(UnicodeWidthChar::width('w'),Some(1)); | ||
assert_eq!('w'.width_cjk(),Some(1)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('h'),Some(2)); | ||
assert_eq!('h'.width_cjk(),Some(2)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('\u{AD}'),Some(1)); | ||
assert_eq!('\u{AD}'.width_cjk(),Some(1)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('\u{1160}'),Some(0)); | ||
assert_eq!('\u{1160}'.width_cjk(),Some(0)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('\u{a1}'),Some(1)); | ||
assert_eq!('\u{a1}'.width_cjk(),Some(2)); | ||
|
||
assert_eq!(UnicodeWidthChar::width('\u{300}'),Some(0)); | ||
assert_eq!('\u{300}'.width_cjk(),Some(0)); | ||
} |