From 77c5f51d3591673794b7e57cb2f2d2c9432c6d60 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 18 Feb 2020 03:05:16 +0900 Subject: [PATCH] improved line reading performance dramatically. ...By removing notify. It was completely unnecessary. HOLY SHIT did I over complicate things. Past me was seriously stupid. [ci-build] --- Cargo.lock | 241 ++------------------------------------------------- Cargo.toml | 1 - src/sound.rs | 30 +++---- 3 files changed, 15 insertions(+), 257 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b05c611..93ac8b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,7 +189,7 @@ dependencies = [ "num-traits", "stdweb", "thiserror", - "winapi 0.3.8", + "winapi", ] [[package]] @@ -222,56 +222,9 @@ dependencies = [ "cfg-if", "libc", "redox_users", - "winapi 0.3.8", + "winapi", ] -[[package]] -name = "filetime" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "winapi 0.3.8", -] - -[[package]] -name = "fsevent" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" -dependencies = [ - "bitflags", - "fsevent-sys", -] - -[[package]] -name = "fsevent-sys" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" -dependencies = [ - "libc", -] - -[[package]] -name = "fuchsia-zircon" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" -dependencies = [ - "bitflags", - "fuchsia-zircon-sys", -] - -[[package]] -name = "fuchsia-zircon-sys" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" - [[package]] name = "getopts" version = "0.2.21" @@ -304,57 +257,12 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549" -[[package]] -name = "inotify" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24e40d6fd5d64e2082e0c796495c8ef5ad667a96d03e5aaa0becfd9d47bcbfb8" -dependencies = [ - "bitflags", - "inotify-sys", - "libc", -] - -[[package]] -name = "inotify-sys" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74a1aa87c59aeff6ef2cc2fa62d41bc43f54952f55652656b18a02fd5e356c0" -dependencies = [ - "libc", -] - -[[package]] -name = "iovec" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" -dependencies = [ - "libc", -] - -[[package]] -name = "kernel32-sys" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lazycell" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" - [[package]] name = "lewton" version = "0.10.0" @@ -379,16 +287,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" dependencies = [ "cc", - "winapi 0.3.8", -] - -[[package]] -name = "log" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" -dependencies = [ - "cfg-if", + "winapi", ] [[package]] @@ -425,60 +324,6 @@ dependencies = [ "cc", ] -[[package]] -name = "mio" -version = "0.6.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" -dependencies = [ - "cfg-if", - "fuchsia-zircon", - "fuchsia-zircon-sys", - "iovec", - "kernel32-sys", - "libc", - "log", - "miow", - "net2", - "slab", - "winapi 0.2.8", -] - -[[package]] -name = "mio-extras" -version = "2.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" -dependencies = [ - "lazycell", - "log", - "mio", - "slab", -] - -[[package]] -name = "miow" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" -dependencies = [ - "kernel32-sys", - "net2", - "winapi 0.2.8", - "ws2_32-sys", -] - -[[package]] -name = "net2" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" -dependencies = [ - "cfg-if", - "libc", - "winapi 0.3.8", -] - [[package]] name = "nom" version = "4.2.3" @@ -489,24 +334,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "notify" -version = "4.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80ae4a7688d1fab81c5bf19c64fc8db920be8d519ce6336ed4e7efe024724dbd" -dependencies = [ - "bitflags", - "filetime", - "fsevent", - "fsevent-sys", - "inotify", - "libc", - "mio", - "mio-extras", - "walkdir", - "winapi 0.3.8", -] - [[package]] name = "num-traits" version = "0.2.11" @@ -677,27 +504,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - [[package]] name = "shlex" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" -[[package]] -name = "slab" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" - [[package]] name = "slice-deque" version = "0.2.4" @@ -706,7 +518,7 @@ checksum = "ffddf594f5f597f63533d897427a570dbaa9feabaaa06595b74b71b7014507d7" dependencies = [ "libc", "mach", - "winapi 0.3.8", + "winapi", ] [[package]] @@ -722,7 +534,6 @@ dependencies = [ "dirs", "getopts", "lazy_static", - "notify", "quick-xml", "rand", "regex", @@ -801,17 +612,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" -[[package]] -name = "walkdir" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" -dependencies = [ - "same-file", - "winapi 0.3.8", - "winapi-util", -] - [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -836,7 +636,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97d468a911faaaeb783693b004e1c62e0063e646b0afae5c146cd144e566e66d" dependencies = [ "widestring", - "winapi 0.3.8", + "winapi", ] [[package]] @@ -856,12 +656,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6" -[[package]] -name = "winapi" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" - [[package]] name = "winapi" version = "0.3.8" @@ -872,39 +666,14 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" -dependencies = [ - "winapi 0.3.8", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "ws2_32-sys" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] diff --git a/Cargo.toml b/Cargo.toml index ba329b0..35bf187 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ description = "A Rusty SoundSense alternative" edge = ["web-view/edge"] [dependencies] -notify = "4.0" regex = {version="1.3", default-features=false, features=["std", "perf"]} rodio = {git="https://github.com/RustAudio/rodio", rev="2c7d1716707876aad450949fa9c7d4f3a8c5ea3b"} quick-xml = "0.17" diff --git a/src/sound.rs b/src/sound.rs index 129b0c1..f4f1ff5 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -1,6 +1,6 @@ use std::time::{Instant, Duration}; use std::fs::{self, File}; -use std::io::{Read, Seek, SeekFrom}; +use std::io::{Read, Seek, SeekFrom, BufReader, BufRead}; use std::path::{Path, PathBuf}; use std::collections::{BTreeMap, HashSet}; use std::sync::{ @@ -10,7 +10,6 @@ use std::sync::{ }; use crate::message::*; -use notify::{Watcher, RecursiveMode, DebouncedEvent}; use rodio::*; use rand::prelude::*; use rand::distributions::weighted::WeightedIndex; @@ -79,9 +78,7 @@ pub struct SoundEntry { pub fn run(sound_rx: Receiver, ui_tx: Sender) { let mut manager : Option = None; - let mut file : Option = None; - let (notify_tx, notify_rx) = std::sync::mpsc::channel(); - let mut watcher = notify::watcher(notify_tx, Duration::from_millis(100)).unwrap(); + let mut buf_reader : Option> = None; let mut prev = Instant::now(); loop { @@ -92,10 +89,10 @@ pub fn run(sound_rx: Receiver, ui_tx: Sender) { use SoundMessage::*; match message { ChangeGamelog(path) => { - watcher.watch(&path, RecursiveMode::NonRecursive).unwrap(); + // watcher.watch(&path, RecursiveMode::NonRecursive).unwrap(); let mut file0 = File::open(&path).unwrap(); file0.seek(SeekFrom::End(0)).unwrap(); - file = Some(file0); + buf_reader = Some(BufReader::new(file0)); ui_tx.send(UIMessage::LoadedGamelog).unwrap(); } @@ -130,19 +127,12 @@ pub fn run(sound_rx: Receiver, ui_tx: Sender) { } } - for event in notify_rx.try_iter() { - if file.is_some() && manager.is_some() { - let manager = manager.as_mut().unwrap(); - if let DebouncedEvent::Write(_path) = event { - let file = file.as_mut().unwrap(); - let mut buf = Vec::new(); - file.read_to_end(&mut buf).unwrap(); - let lossy = String::from_utf8_lossy(&buf); - lossy.lines().for_each(|log| { - manager.process_log(log); - }); - } - } + if buf_reader.is_some() && manager.is_some() { + let manager = manager.as_mut().unwrap(); + let buf_reader = buf_reader.as_mut().unwrap(); + buf_reader.lines() + .filter_map(|result| result.ok()) + .for_each(|log| manager.process_log(&log)); } if let Some(manager) = manager.as_mut() { manager.maintain(dt);