Merge branch 'xacrimon/fast-chan-mpsc-bad' into 'master'

Switch mpsc and std mutexes to faster alternatives.

See merge request veloren/veloren!440
This commit is contained in:
Marcel 2019-08-15 22:54:21 +00:00
commit 68df43045c
13 changed files with 86 additions and 49 deletions

39
Cargo.lock generated
View File

@ -1484,6 +1484,14 @@ dependencies = [
"scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lock_api"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.3.9"
@ -1925,6 +1933,16 @@ dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.3.1"
@ -1964,6 +1982,20 @@ dependencies = [
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
@ -2913,6 +2945,7 @@ name = "veloren-common"
version = "0.3.0"
dependencies = [
"bincode 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"dot_vox 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"find_folder 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2922,6 +2955,7 @@ dependencies = [
"lz4-compress 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)",
"mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2938,6 +2972,7 @@ name = "veloren-server"
version = "0.3.0"
dependencies = [
"chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2991,6 +3026,7 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"msgbox 0.1.1 (git+https://github.com/bekker/msgbox-rs.git)",
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"portpicker 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rodio 0.8.1 (git+https://github.com/desttinghim/rodio.git?rev=dd93f905c1afefaac03c496a666ecab27d3e391b)",
@ -3366,6 +3402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum lock_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed946d4529956a20f2d63ebe1b69996d5a2137c91913fe3ebbeff957f5bca7ff"
"checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc"
"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum lz4-compress 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f966533a922a9bba9e95e594c1fdb3b9bf5fdcdb11e37e51ad84cd76e468b91"
@ -3415,9 +3452,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5"
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
"checksum parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa7767817701cce701d5585b9c4db3cdd02086398322c1d7e8bf5094a96a2ce7"
"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c"
"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
"checksum parking_lot_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cb88cb1cb3790baa6776844f968fea3be44956cf184fa1be5a03341f5491278c"
"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
"checksum percent-encoding 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba4f28a6faf4ffea762ba8f4baef48c61a6db348647c73095034041fc79dd954"
"checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f"

View File

@ -25,3 +25,5 @@ lazy_static = "1.3.0"
lz4-compress = "0.1.1"
hashbrown = { version = "0.5.0", features = ["serde", "nightly"] }
find_folder = "0.3.0"
parking_lot = "0.9.0"
crossbeam = "0.7.2"

View File

@ -1,5 +1,6 @@
use parking_lot::Mutex;
use specs::Entity as EcsEntity;
use std::{collections::VecDeque, ops::DerefMut, sync::Mutex};
use std::{collections::VecDeque, ops::DerefMut};
use vek::*;
pub enum Event {
@ -21,11 +22,11 @@ impl EventBus {
}
pub fn emit(&self, event: Event) {
self.queue.lock().unwrap().push_front(event);
self.queue.lock().push_front(event);
}
pub fn recv_all(&self) -> impl ExactSizeIterator<Item = Event> {
std::mem::replace(self.queue.lock().unwrap().deref_mut(), VecDeque::new()).into_iter()
std::mem::replace(self.queue.lock().deref_mut(), VecDeque::new()).into_iter()
}
}
@ -42,6 +43,6 @@ impl<'a> Emitter<'a> {
impl<'a> Drop for Emitter<'a> {
fn drop(&mut self) {
self.bus.queue.lock().unwrap().append(&mut self.events);
self.bus.queue.lock().append(&mut self.events);
}
}

View File

@ -1,3 +1,4 @@
use crossbeam::channel;
use log::warn;
use serde::{de::DeserializeOwned, Serialize};
use std::{
@ -8,7 +9,7 @@ use std::{
net::{Shutdown, SocketAddr, TcpListener, TcpStream},
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc,
Arc,
},
thread,
time::Duration,
@ -34,8 +35,8 @@ impl From<bincode::Error> for Error {
}
}
impl From<mpsc::TryRecvError> for Error {
fn from(_error: mpsc::TryRecvError) -> Self {
impl From<channel::TryRecvError> for Error {
fn from(_error: channel::TryRecvError) -> Self {
Error::ChannelFailure
}
}
@ -90,8 +91,8 @@ impl<S: PostMsg, R: PostMsg> PostOffice<S, R> {
}
pub struct PostBox<S: PostMsg, R: PostMsg> {
send_tx: mpsc::Sender<S>,
recv_rx: mpsc::Receiver<Result<R, Error>>,
send_tx: channel::Sender<S>,
recv_rx: channel::Receiver<Result<R, Error>>,
worker: Option<thread::JoinHandle<()>>,
running: Arc<AtomicBool>,
error: Option<Error>,
@ -108,8 +109,8 @@ impl<S: PostMsg, R: PostMsg> PostBox<S, R> {
let running = Arc::new(AtomicBool::new(true));
let worker_running = running.clone();
let (send_tx, send_rx) = mpsc::channel();
let (recv_tx, recv_rx) = mpsc::channel();
let (send_tx, send_rx) = channel::unbounded();
let (recv_tx, recv_rx) = channel::unbounded();
let worker = thread::spawn(move || Self::worker(stream, send_rx, recv_tx, worker_running));
@ -154,7 +155,7 @@ impl<S: PostMsg, R: PostMsg> PostBox<S, R> {
loop {
match self.recv_rx.try_recv() {
Ok(Ok(msg)) => new.push(msg),
Err(mpsc::TryRecvError::Empty) => break,
Err(channel::TryRecvError::Empty) => break,
Err(e) => {
self.error = Some(e.into());
break;
@ -171,8 +172,8 @@ impl<S: PostMsg, R: PostMsg> PostBox<S, R> {
fn worker(
mut stream: TcpStream,
send_rx: mpsc::Receiver<S>,
recv_tx: mpsc::Sender<Result<R, Error>>,
send_rx: channel::Receiver<S>,
recv_tx: channel::Sender<Result<R, Error>>,
running: Arc<AtomicBool>,
) {
let mut outgoing_chunks = VecDeque::new();
@ -215,7 +216,7 @@ impl<S: PostMsg, R: PostMsg> PostBox<S, R> {
.map(|chunk| chunk.to_vec())
.for_each(|chunk| outgoing_chunks.push_back(chunk))
}
Err(mpsc::TryRecvError::Empty) => break,
Err(channel::TryRecvError::Empty) => break,
// Worker error
Err(e) => {
let _ = recv_tx.send(Err(e.into()));

View File

@ -20,3 +20,4 @@ serde_derive = "1.0.98"
rand = "0.7.0"
chrono = "0.4.7"
hashbrown = { version = "0.5.0", features = ["serde", "nightly"] }
crossbeam = "0.7.2"

View File

@ -25,16 +25,12 @@ use common::{
vol::Vox,
vol::{ReadVol, VolSize},
};
use crossbeam::channel;
use hashbrown::HashSet;
use log::debug;
use rand::Rng;
use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity};
use std::{
i32,
net::SocketAddr,
sync::{mpsc, Arc},
time::Duration,
};
use std::{i32, net::SocketAddr, sync::Arc, time::Duration};
use uvth::{ThreadPool, ThreadPoolBuilder};
use vek::*;
use world::{ChunkSupplement, World};
@ -65,8 +61,8 @@ pub struct Server {
clients: Clients,
thread_pool: ThreadPool,
chunk_tx: mpsc::Sender<(Vec2<i32>, (TerrainChunk, ChunkSupplement))>,
chunk_rx: mpsc::Receiver<(Vec2<i32>, (TerrainChunk, ChunkSupplement))>,
chunk_tx: channel::Sender<(Vec2<i32>, (TerrainChunk, ChunkSupplement))>,
chunk_rx: channel::Receiver<(Vec2<i32>, (TerrainChunk, ChunkSupplement))>,
pending_chunks: HashSet<Vec2<i32>>,
server_settings: ServerSettings,
@ -84,7 +80,7 @@ impl Server {
/// Create a new server bound to the given socket.
pub fn bind<A: Into<SocketAddr>>(addrs: A, settings: ServerSettings) -> Result<Self, Error> {
let (chunk_tx, chunk_rx) = mpsc::channel();
let (chunk_tx, chunk_rx) = channel::unbounded();
let mut state = State::default();
state

View File

@ -58,3 +58,4 @@ rodio = { git = "https://github.com/desttinghim/rodio.git", rev = "dd93f905c1afe
crossbeam = "0.7.2"
heaptrack = "0.3.0"
hashbrown = { version = "0.5.0", features = ["serde", "nightly"] }
parking_lot = "0.9.0"

View File

@ -6,10 +6,11 @@ use crossbeam::{
queue::SegQueue,
sync::ShardedLock,
};
use parking_lot::{Condvar, Mutex};
use rodio::{Decoder, Device, Sink};
use std::fs::File;
use std::io::BufReader;
use std::sync::{Arc, Condvar, Mutex};
use std::sync::Arc;
use std::thread;
trait AudioConfig {
@ -136,7 +137,7 @@ impl AudioPlayer {
fn set_playing(&self, playing: bool) {
*self.event_loop.playing.write().unwrap() = playing;
let &(ref lock, ref condvar) = &*self.event_loop.condvar;
let mut started = lock.lock().unwrap();
let mut started = lock.lock();
*started = playing;
if playing {
condvar.notify_one();
@ -157,10 +158,10 @@ impl MonoMode for AudioPlayer {
thread::spawn(move || {
let block = || {
let (ref lock, ref condvar) = *condition;
let mut started = lock.lock().unwrap();
let mut started = lock.lock();
*started = false;
while !*started {
started = condvar.wait(started).unwrap();
condvar.wait(&mut started);
}
};
let mut playback = MonoEmitter::new(&Settings::load().audio);

View File

@ -1,12 +1,10 @@
use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC};
use std::time::SystemTime;
use crate::DISCORD_INSTANCE;
use std::sync::mpsc::Sender;
use std::sync::{mpsc, Mutex};
use crossbeam::channel::{unbounded, Receiver, Sender};
use discord_rpc_sdk::{DiscordUser, EventHandlers, RichPresence, RPC};
use parking_lot::Mutex;
use std::thread;
use std::thread::JoinHandle;
use std::time::SystemTime;
/// Connects to the discord application where Images and more resides
/// can be viewed at https://discordapp.com/developers/applications/583662036194689035/rich-presence/assets
@ -30,7 +28,7 @@ pub struct DiscordState {
}
pub fn run() -> Mutex<DiscordState> {
let (tx, rx) = mpsc::channel();
let (tx, rx) = unbounded();
Mutex::new(DiscordState {
tx,

View File

@ -9,7 +9,7 @@ extern crate lazy_static;
pub mod discord;
#[cfg(feature = "discord")]
use std::sync::Mutex;
use parking_lot::Mutex;
#[macro_use]
pub mod ui;

View File

@ -1,12 +1,8 @@
use client::{error::Error as ClientError, Client};
use common::comp;
use crossbeam::channel::{unbounded, Receiver, TryRecvError};
use log::info;
use std::{
net::ToSocketAddrs,
sync::mpsc::{channel, Receiver, TryRecvError},
thread,
time::Duration,
};
use std::{net::ToSocketAddrs, thread, time::Duration};
#[cfg(feature = "discord")]
use crate::{discord, discord::DiscordUpdate};
@ -38,7 +34,7 @@ impl ClientInit {
) -> Self {
let (server_address, default_port, prefer_ipv6) = connection_args;
let (tx, rx) = channel();
let (tx, rx) = unbounded();
thread::spawn(move || {
// Sleep the thread to wait for the single-player server to start up.

View File

@ -8,9 +8,10 @@ use common::{
vol::{SampleVol, VolSize},
volumes::vol_map_2d::VolMap2dErr,
};
use crossbeam::channel;
use frustum_query::frustum::Frustum;
use hashbrown::HashMap;
use std::{i32, ops::Mul, sync::mpsc, time::Duration};
use std::{i32, ops::Mul, time::Duration};
use vek::*;
struct TerrainChunk {
@ -56,8 +57,8 @@ pub struct Terrain {
// The mpsc sender and receiver used for talking to meshing worker threads.
// We keep the sender component for no reason other than to clone it and send it to new workers.
mesh_send_tmp: mpsc::Sender<MeshWorkerResponse>,
mesh_recv: mpsc::Receiver<MeshWorkerResponse>,
mesh_send_tmp: channel::Sender<MeshWorkerResponse>,
mesh_recv: channel::Receiver<MeshWorkerResponse>,
mesh_todo: HashMap<Vec2<i32>, ChunkMeshState>,
}
@ -65,7 +66,7 @@ impl Terrain {
pub fn new() -> Self {
// Create a new mpsc (Multiple Produced, Single Consumer) pair for communicating with
// worker threads that are meshing chunks.
let (send, recv) = mpsc::channel();
let (send, recv) = channel::unbounded();
Self {
chunks: HashMap::default(),

View File

@ -1,11 +1,11 @@
use client::Client;
use common::clock::Clock;
use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError};
use log::info;
use portpicker::pick_unused_port;
use server::{Event, Input, Server, ServerSettings};
use std::{
net::SocketAddr,
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
thread::{self, JoinHandle},
time::Duration,
};
@ -28,7 +28,7 @@ pub struct Singleplayer {
impl Singleplayer {
pub fn new(client: Option<&Client>) -> (Self, SocketAddr) {
let (sender, receiver) = channel();
let (sender, receiver) = unbounded();
let sock = SocketAddr::from((
[127, 0, 0, 1],