Skip to content

Commit

Permalink
Use parking_lot for wrapping TextureManager
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Mar 20, 2022
1 parent da62bde commit 414079b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 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 egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ epaint = { version = "0.17.0", path = "../epaint", default-features = false }

ahash = "0.7"
nohash-hasher = "0.2"
parking_lot = "0.12"

# Optional:
ron = { version = "0.7", optional = true }
Expand Down
11 changes: 8 additions & 3 deletions egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use epaint::{mutex::*, stats::*, text::Fonts, TessellationOptions, *};

// ----------------------------------------------------------------------------

// NOTE: we always use `parking_lot` here so we can
// use `WrappedTextureManager` from any thread.
#[derive(Clone)]
struct WrappedTextureManager(Arc<RwLock<epaint::TextureManager>>);
struct WrappedTextureManager(Arc<parking_lot::RwLock<epaint::TextureManager>>);

impl Default for WrappedTextureManager {
fn default() -> Self {
Expand All @@ -25,7 +27,7 @@ impl Default for WrappedTextureManager {
);
assert_eq!(font_id, TextureId::default());

Self(Arc::new(RwLock::new(tex_mngr)))
Self(Arc::new(parking_lot::RwLock::new(tex_mngr)))
}
}

Expand Down Expand Up @@ -163,6 +165,9 @@ impl ContextImpl {
#[derive(Clone, Default)]
pub struct Context {
ctx: Arc<RwLock<ContextImpl>>,
// These are not inside of `ContextImpl` because we want to have thread-safe access to them
// even without running with the multi_threaded feature flag.
// See https://github.com/emilk/egui/issues/1379.
repaint_info: Arc<RepaintInfo>,
tex_manager: WrappedTextureManager,
}
Expand Down Expand Up @@ -720,7 +725,7 @@ impl Context {
/// You can show stats about the allocated textures using [`Self::texture_ui`].
///
/// This is safe to call from any thread at any time.
pub fn tex_manager(&self) -> Arc<RwLock<epaint::textures::TextureManager>> {
pub fn tex_manager(&self) -> Arc<parking_lot::RwLock<epaint::textures::TextureManager>> {
self.tex_manager.0.clone()
}

Expand Down
4 changes: 2 additions & 2 deletions epaint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ single_threaded = ["atomic_refcell"]

# Only needed if you plan to use the same fonts from multiple threads.
# It comes with a minor performance impact.
multi_threaded = ["parking_lot"]
multi_threaded = []


[dependencies]
Expand All @@ -63,7 +63,7 @@ atomic_refcell = { version = "0.1", optional = true } # Used
bytemuck = { version = "1.7.2", optional = true, features = ["derive"] }
cint = { version = "^0.2.2", optional = true }
nohash-hasher = "0.2"
parking_lot = { version = "0.12", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios.
parking_lot = "0.12"
serde = { version = "1", optional = true, features = ["derive", "rc"] }

[dev-dependencies]
Expand Down
8 changes: 3 additions & 5 deletions epaint/src/texture_handle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
emath::NumExt,
mutex::{Arc, RwLock},
ImageData, ImageDelta, TextureId, TextureManager,
};
use crate::{emath::NumExt, mutex::Arc, ImageData, ImageDelta, TextureId, TextureManager};

use parking_lot::RwLock;

/// Used to paint images.
///
Expand Down

0 comments on commit 414079b

Please sign in to comment.