Skip to content

Commit eb6b9d5

Browse files
author
Kharif
committed
switched from String to Arc<str> which is faster for constant value strings where cloning is common
1 parent 05c1940 commit eb6b9d5

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::any::TypeId;
55
use std::collections::{HashMap, HashSet};
66
use std::iter::FromIterator;
77
use std::ops;
8+
use std::sync::Arc;
89

910
pub type LocalSpace = vger::defs::LocalSpace;
1011
pub type WorldSpace = vger::defs::WorldSpace;
@@ -19,7 +20,7 @@ pub type WorldToLocal = Transform2D<f32, WorldSpace, LocalSpace>;
1920

2021
#[derive(Clone, Eq, PartialEq)]
2122
pub struct CommandInfo {
22-
pub path: String,
23+
pub path: Arc<str>,
2324
pub key: Option<HotKey>,
2425
}
2526

@@ -81,7 +82,7 @@ pub struct Context {
8182
pub(crate) focused_id: Option<ViewId>,
8283

8384
/// The current title of the window
84-
pub window_title: String,
85+
pub window_title: Arc<str>,
8586

8687
/// Are we fullscreen?
8788
pub fullscreen: bool,

src/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use crate::*;
24

35
/// User interface event.
@@ -29,7 +31,7 @@ pub enum Event {
2931
MouseLeftWindow,
3032

3133
/// Menu command.
32-
Command(String),
34+
Command(Arc<str>),
3335

3436
/// Key press.
3537
Key(Key),

src/views/command.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::*;
2-
use std::any::Any;
2+
use std::{any::Any, sync::Arc};
33

44
#[derive(Clone)]
55
pub struct Command<V, F> {
66
child: V,
7-
name: String,
7+
name: Arc<str>,
88
key: Option<HotKey>,
99
func: F,
1010
}
@@ -14,7 +14,7 @@ where
1414
V: View,
1515
F: Fn(&mut Context) + Clone + 'static,
1616
{
17-
pub fn new(v: V, name: String, key: Option<HotKey>, f: F) -> Self {
17+
pub fn new(v: V, name: Arc<str>, key: Option<HotKey>, f: F) -> Self {
1818
Self {
1919
child: v,
2020
name,
@@ -97,7 +97,7 @@ impl<V, F> private::Sealed for Command<V, F> {}
9797

9898
pub trait DynCommandBase {
9999
fn exec(&self);
100-
fn name(&self) -> String;
100+
fn name(&self) -> Arc<str>;
101101
fn key(&self) -> Option<HotKey>;
102102
}
103103

@@ -337,7 +337,7 @@ impl<V, C> private::Sealed for CommandGroup<V, C> {}
337337

338338
#[derive(Clone)]
339339
pub struct NullCommand {
340-
name: String,
340+
name: Arc<str>,
341341
key: Option<HotKey>,
342342
}
343343

@@ -351,7 +351,7 @@ pub fn command(name: &str) -> NullCommand {
351351

352352
impl DynCommandBase for NullCommand {
353353
fn exec(&self) {}
354-
fn name(&self) -> String {
354+
fn name(&self) -> Arc<str> {
355355
self.name.clone()
356356
}
357357
fn key(&self) -> Option<HotKey> {
@@ -379,7 +379,7 @@ impl NullCommand {
379379

380380
#[derive(Clone)]
381381
pub struct Command2<F> {
382-
name: String,
382+
name: Arc<str>,
383383
key: Option<HotKey>,
384384
func: F,
385385
}
@@ -391,7 +391,7 @@ where
391391
fn exec(&self) {
392392
(self.func)();
393393
}
394-
fn name(&self) -> String {
394+
fn name(&self) -> Arc<str> {
395395
self.name.clone()
396396
}
397397
fn key(&self) -> Option<HotKey> {

src/views/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::*;
2-
use std::any::Any;
2+
use std::{any::Any, sync::Arc};
33

44
/// Struct for the `window_title` modifier.
55
#[derive(Clone)]
66
pub struct TitleView<V> {
77
child: V,
8-
title: String,
8+
title: Arc<str>,
99
}
1010

1111
impl<V> TitleView<V>

src/winit_event_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct EventHandler<T>
157157
where
158158
T: View,
159159
{
160-
title: String,
160+
title: Arc<str>,
161161
running: bool,
162162
// The GPU resources, if running.
163163
context: Option<DrawContext>,
@@ -189,7 +189,7 @@ where
189189
self.running = true;
190190

191191
// Create the main window.
192-
let window_attributes = Window::default_attributes().with_title(&self.title);
192+
let window_attributes = Window::default_attributes().with_title(self.title.to_string());
193193
self.window = match event_loop.create_window(window_attributes) {
194194
Err(e) => {
195195
log::error!("Error creating window: {:?}", e);
@@ -500,7 +500,7 @@ pub fn rui(view: impl View) {
500500

501501
let window_title = String::from("rui");
502502
let mut app = EventHandler {
503-
title: window_title,
503+
title: window_title.into(),
504504
running: false,
505505
context: None,
506506
window: None,

0 commit comments

Comments
 (0)