Skip to content

Commit

Permalink
utils: rewrite count_newlines using bytecount::count
Browse files Browse the repository at this point in the history
This uses a optimized byte count and also makes use of SIMD
instructions to optimize the processing of the byte arrays.

Signed-off-by: Otavio Salvador <[email protected]>
  • Loading branch information
otavio committed Oct 14, 2018
1 parent c4a8cdc commit e203057
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ rustc-ap-rustc_target = "272.0.0"
rustc-ap-syntax = "272.0.0"
rustc-ap-syntax_pos = "272.0.0"
failure = "0.1.1"
bytecount = { version = "0.3", features = ["simd-accel"] }

[dev-dependencies]
assert_cli = "0.6"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#[macro_use]
extern crate derive_new;
extern crate atty;
extern crate bytecount;
extern crate diff;
extern crate failure;
extern crate itertools;
Expand Down
6 changes: 4 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use std::borrow::Cow;

use bytecount;

use rustc_target::spec::abi;
use syntax::ast::{
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind, Path,
Expand Down Expand Up @@ -305,8 +307,8 @@ pub fn stmt_expr(stmt: &ast::Stmt) -> Option<&ast::Expr> {

#[inline]
pub fn count_newlines(input: &str) -> usize {
// Using `as_bytes` to omit UTF-8 decoding
input.as_bytes().iter().filter(|&&c| c == b'\n').count()
// Using bytes to omit UTF-8 decoding
bytecount::count(input.as_bytes(), b'\n')
}

// For format_missing and last_pos, need to use the source callsite (if applicable).
Expand Down

0 comments on commit e203057

Please sign in to comment.