Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks for linecache #32

Merged
merged 3 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ before_script:
script:
# Required for tests. Install only if not present already
- bash -c 'which xi-core || cargo install --git https://github.com/xi-editor/xi-editor xi-core'
- bash -c 'if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]] ; then cargo clippy && cargo fmt -- --check && cargo test ; else cargo test ; fi'
- cargo clippy
- cargo fmt -- --check
- cargo test
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ syntect = { version = "3.2.0", default-features = false }
[dependencies.clippy]
optional = true
version = "0.0.302"

[dev-dependencies]
criterion = "0.2"

[[bench]]
name = "linecache"
harness = false
143 changes: 143 additions & 0 deletions benches/linecache.rs

Large diffs are not rendered by default.

47 changes: 29 additions & 18 deletions examples/it_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ extern crate tokio;
extern crate xrl;

use futures::{future, Future, Stream};
use xrl::{
XiNotification,
Client, ServerResult, Frontend,
FrontendBuilder, spawn, MeasureWidth
};

use xrl::{spawn, Client, Frontend, FrontendBuilder, MeasureWidth, ServerResult, XiNotification};

// Type that represent our client
struct MyFrontend {
Expand All @@ -19,26 +14,41 @@ struct MyFrontend {

// Implement how our client handles notifications and requests from the core.
impl Frontend for MyFrontend {

fn handle_notification(&mut self, notification: XiNotification) -> ServerResult<()> {
use XiNotification::*;

match notification {
Update(update) => println!("received `update` from Xi core:\n{:?}", update),
ScrollTo(scroll) => println!("received `scroll_to` from Xi core:\n{:?}", scroll),
DefStyle(style) => println!("received `def_style` from Xi core:\n{:?}", style),
AvailablePlugins(plugins) => println!("received `available_plugins` from Xi core:\n{:?}", plugins),
AvailablePlugins(plugins) => {
println!("received `available_plugins` from Xi core:\n{:?}", plugins)
}
UpdateCmds(cmds) => println!("received `update_cmds` from Xi core:\n{:?}", cmds),
PluginStarted(plugin) => println!("received `plugin_started` from Xi core:\n{:?}", plugin),
PluginStoped(plugin) => println!("received `plugin_stoped` from Xi core:\n{:?}", plugin),
ConfigChanged(config) => println!("received `config_changed` from Xi core:\n{:?}", config),
PluginStarted(plugin) => {
println!("received `plugin_started` from Xi core:\n{:?}", plugin)
}
PluginStoped(plugin) => {
println!("received `plugin_stoped` from Xi core:\n{:?}", plugin)
}
ConfigChanged(config) => {
println!("received `config_changed` from Xi core:\n{:?}", config)
}
ThemeChanged(theme) => println!("received `theme_changed` from Xi core:\n{:?}", theme),
Alert(alert) => println!("received `alert` from Xi core:\n{:?}", alert),
AvailableThemes(themes) => println!("received `available_themes` from Xi core:\n{:?}", themes),
AvailableThemes(themes) => {
println!("received `available_themes` from Xi core:\n{:?}", themes)
}
FindStatus(status) => println!("received `find_status` from Xi core:\n{:?}", status),
ReplaceStatus(status) => println!("received `replace_status` from Xi core:\n{:?}", status),
AvailableLanguages(langs) => println!("received `available_languages` from Xi core:\n{:?}", langs),
LanguageChanged(lang) => println!("received `language_changed` from Xi core:\n{:?}", lang),
ReplaceStatus(status) => {
println!("received `replace_status` from Xi core:\n{:?}", status)
}
AvailableLanguages(langs) => {
println!("received `available_languages` from Xi core:\n{:?}", langs)
}
LanguageChanged(lang) => {
println!("received `language_changed` from Xi core:\n{:?}", lang)
}
}
Box::new(future::ok(()))
}
Expand All @@ -61,21 +71,22 @@ fn main() {
let (client, core_stderr) = spawn("xi-core", MyFrontendBuilder {});

// All clients must send client_started notification first
tokio::run(client.client_started(None, None).map_err(|_|()));
tokio::run(client.client_started(None, None).map_err(|_| ()));
// start logging Xi core's stderr
let log_core_errors = core_stderr
.for_each(|msg| {
println!("xi-core stderr: {}", msg);
Ok(())
}).map_err(|_|());
})
.map_err(|_| ());
::std::thread::spawn(move || {
tokio::run(log_core_errors);
});
// Send a request to open a new view, and print the result
let open_new_view = client
.new_view(None)
.map(|view_name| println!("opened new view: {}", view_name))
.map_err(|_|());
.map_err(|_| ());
tokio::run(open_new_view);
// sleep until xi-requests are received
::std::thread::sleep(::std::time::Duration::new(5, 0));
Expand Down
22 changes: 14 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub struct LineCache {
}

impl LineCache {

/// Retrieve the number of invalid lines before
/// the start of the line cache.
pub fn before(&self) -> u64 {
Expand All @@ -35,7 +34,10 @@ impl LineCache {
/// Handle an xi-core update.
pub fn update(&mut self, update: Update) {
debug!("line cache before update: {:?}", self);
debug!("operations to be applied to the line cache: {:?}", &update.operations);
debug!(
"operations to be applied to the line cache: {:?}",
&update.operations
);
let LineCache {
ref mut lines,
ref mut invalid_before,
Expand Down Expand Up @@ -97,7 +99,6 @@ impl<'a, 'b, 'c> UpdateHelper<'a, 'b, 'c> {

// there is no more line to copy so we're done
return;

} else if **old_invalid_after > 0 {
// case 2: there are more lines to copy than invalid lines

Expand Down Expand Up @@ -143,17 +144,23 @@ impl<'a, 'b, 'c> UpdateHelper<'a, 'b, 'c> {
// its *new* line number, given by the "copy"
// operation. This will be used to update the line
// number for all the lines we copy.
old_lines.iter().find_map(|line| {
line.line_num.map(|num| new_first_line_num as i64 - num as i64)
}).unwrap_or(0)
old_lines
.iter()
.find_map(|line| {
line.line_num
.map(|num| new_first_line_num as i64 - num as i64)
})
.unwrap_or(0)
} else {
// if the "copy" operation does not specify a new line
// number, just set the diff to 0
0
};

let copied_lines = old_lines.drain(range).map(|mut line| {
line.line_num = line.line_num.map(|line_num| (line_num as i64 + diff) as u64);
line.line_num = line
.line_num
.map(|line_num| (line_num as i64 + diff) as u64);
line
});
new_lines.extend(copied_lines);
Expand All @@ -167,7 +174,6 @@ impl<'a, 'b, 'c> UpdateHelper<'a, 'b, 'c> {
// STEP 3: Handle the remaining invalid lines
// ------------------------------------------------------------


// We should have at least enought invalid lines to copy, otherwise it indicates there's a
// problem, and we panic.
if **old_invalid_after >= nb_lines {
Expand Down
Loading