Skip to content

Commit

Permalink
Reduce number of Tokio workers (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Jul 5, 2023
1 parent 8d45398 commit 61aba80
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 258 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ comrak = {version = "0.18.0", features = ["shortcodes"]}
css-color-parser = "0.1.2"
dirs = "4.0.0"
emojis = "~0.5.2"
futures = "0.3"
gethostname = "0.4.1"
html5ever = "0.26.0"
image = "0.24.5"
Expand Down
14 changes: 12 additions & 2 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use url::Url;

use matrix_sdk::{
encryption::verification::SasVerification,
room::Joined,
room::{Joined, Room as MatrixRoom},
ruma::{
events::{
reaction::ReactionEvent,
Expand Down Expand Up @@ -644,6 +644,13 @@ fn emoji_map() -> CompletionMap<String, &'static Emoji> {
return emojis;
}

#[derive(Default)]
pub struct SyncInfo {
pub spaces: Vec<MatrixRoom>,
pub rooms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
}

pub struct ChatStore {
pub cmds: ProgramCommands,
pub worker: Requester,
Expand All @@ -654,6 +661,7 @@ pub struct ChatStore {
pub settings: ApplicationSettings,
pub need_load: HashSet<OwnedRoomId>,
pub emojis: CompletionMap<String, &'static Emoji>,
pub sync_info: SyncInfo,
}

impl ChatStore {
Expand All @@ -663,12 +671,14 @@ impl ChatStore {
settings,

cmds: crate::commands::setup_commands(),
emojis: emoji_map(),

names: Default::default(),
rooms: Default::default(),
presences: Default::default(),
verifications: Default::default(),
need_load: Default::default(),
emojis: emoji_map(),
sync_info: Default::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ fn main() -> IambResult<()> {

let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.worker_threads(2)
.thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
Expand Down
127 changes: 80 additions & 47 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::cmp::{Ord, Ordering, PartialOrd};
use std::ops::Deref;
use std::sync::Arc;
use std::time::{Duration, Instant};

use matrix_sdk::{
Expand All @@ -10,7 +12,6 @@ use matrix_sdk::{
OwnedRoomId,
RoomId,
},
DisplayName,
};

use modalkit::tui::{
Expand Down Expand Up @@ -78,6 +79,8 @@ use self::{room::RoomState, welcome::WelcomeState};
pub mod room;
pub mod welcome;

type MatrixRoomInfo = Arc<(MatrixRoom, Option<Tags>)>;

const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);

#[inline]
Expand Down Expand Up @@ -380,10 +383,13 @@ impl WindowOps<IambInfo> for IambWindow {
match self {
IambWindow::Room(state) => state.draw(area, buf, focused, store),
IambWindow::DirectList(state) => {
let dms = store.application.worker.direct_messages();
let mut items = dms
let mut items = store
.application
.sync_info
.dms
.clone()
.into_iter()
.map(|(id, name, tags)| DirectItem::new(id, name, tags, store))
.map(|room_info| DirectItem::new(room_info, store))
.collect::<Vec<_>>();
items.sort();

Expand Down Expand Up @@ -416,10 +422,13 @@ impl WindowOps<IambInfo> for IambWindow {
.render(area, buf, state);
},
IambWindow::RoomList(state) => {
let joined = store.application.worker.active_rooms();
let mut items = joined
let mut items = store
.application
.sync_info
.rooms
.clone()
.into_iter()
.map(|(room, name, tags)| RoomItem::new(room, name, tags, store))
.map(|room_info| RoomItem::new(room_info, store))
.collect::<Vec<_>>();
items.sort();

Expand All @@ -432,9 +441,13 @@ impl WindowOps<IambInfo> for IambWindow {
.render(area, buf, state);
},
IambWindow::SpaceList(state) => {
let spaces = store.application.worker.spaces();
let items =
spaces.into_iter().map(|(room, name)| SpaceItem::new(room, name, store));
let items = store
.application
.sync_info
.spaces
.clone()
.into_iter()
.map(|room| SpaceItem::new(room, store));
state.set(items.collect());
state.draw(area, buf, focused, store);

Expand Down Expand Up @@ -639,44 +652,53 @@ impl Window<IambInfo> for IambWindow {

#[derive(Clone)]
pub struct RoomItem {
room: MatrixRoom,
tags: Option<Tags>,
room_info: MatrixRoomInfo,
name: String,
}

impl RoomItem {
fn new(
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
let name = name.to_string();
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
let room = &room_info.deref().0;
let room_id = room.room_id();

let info = store.application.get_room_info(room_id.to_owned());
info.name = name.clone().into();
info.tags = tags.clone();
let name = info.name.clone().unwrap_or_default();
info.tags = room_info.deref().1.clone();

if let Some(alias) = room.canonical_alias() {
store.application.names.insert(alias.to_string(), room_id.to_owned());
}

RoomItem { room, tags, name }
RoomItem { room_info, name }
}

#[inline]
fn room(&self) -> &MatrixRoom {
&self.room_info.deref().0
}

#[inline]
fn room_id(&self) -> &RoomId {
self.room().room_id()
}

#[inline]
fn tags(&self) -> &Option<Tags> {
&self.room_info.deref().1
}
}

impl PartialEq for RoomItem {
fn eq(&self, other: &Self) -> bool {
self.room.room_id() == other.room.room_id()
self.room_id() == other.room_id()
}
}

impl Eq for RoomItem {}

impl Ord for RoomItem {
fn cmp(&self, other: &Self) -> Ordering {
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
}
}

Expand All @@ -694,7 +716,7 @@ impl ToString for RoomItem {

impl ListItem<IambInfo> for RoomItem {
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
if let Some(tags) = &self.tags {
if let Some(tags) = &self.tags() {
let style = selected_style(selected);
let mut spans = vec![Span::styled(self.name.as_str(), style)];

Expand All @@ -707,7 +729,7 @@ impl ListItem<IambInfo> for RoomItem {
}

fn get_word(&self) -> Option<String> {
self.room.room_id().to_string().into()
self.room_id().to_string().into()
}
}

Expand All @@ -718,29 +740,37 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
ctx: &ProgramContext,
_: &mut ProgramStore,
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
room_prompt(self.room.room_id(), act, ctx)
room_prompt(self.room_id(), act, ctx)
}
}

#[derive(Clone)]
pub struct DirectItem {
room: MatrixRoom,
tags: Option<Tags>,
room_info: MatrixRoomInfo,
name: String,
}

impl DirectItem {
fn new(
room: MatrixRoom,
name: DisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
let name = name.to_string();
fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
let room_id = room_info.deref().0.room_id().to_owned();
let name = store.application.get_room_info(room_id).name.clone().unwrap_or_default();

DirectItem { room_info, name }
}

#[inline]
fn room(&self) -> &MatrixRoom {
&self.room_info.deref().0
}

store.application.set_room_name(room.room_id(), name.as_str());
#[inline]
fn room_id(&self) -> &RoomId {
self.room().room_id()
}

DirectItem { room, tags, name }
#[inline]
fn tags(&self) -> &Option<Tags> {
&self.room_info.deref().1
}
}

Expand All @@ -752,7 +782,7 @@ impl ToString for DirectItem {

impl ListItem<IambInfo> for DirectItem {
fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
if let Some(tags) = &self.tags {
if let Some(tags) = &self.tags() {
let style = selected_style(selected);
let mut spans = vec![Span::styled(self.name.as_str(), style)];

Expand All @@ -765,21 +795,21 @@ impl ListItem<IambInfo> for DirectItem {
}

fn get_word(&self) -> Option<String> {
self.room.room_id().to_string().into()
self.room_id().to_string().into()
}
}

impl PartialEq for DirectItem {
fn eq(&self, other: &Self) -> bool {
self.room.room_id() == other.room.room_id()
self.room_id() == other.room_id()
}
}

impl Eq for DirectItem {}

impl Ord for DirectItem {
fn cmp(&self, other: &Self) -> Ordering {
tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room()))
}
}

Expand All @@ -796,7 +826,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
ctx: &ProgramContext,
_: &mut ProgramStore,
) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
room_prompt(self.room.room_id(), act, ctx)
room_prompt(self.room_id(), act, ctx)
}
}

Expand All @@ -807,11 +837,14 @@ pub struct SpaceItem {
}

impl SpaceItem {
fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
let name = name.to_string();
fn new(room: MatrixRoom, store: &mut ProgramStore) -> Self {
let room_id = room.room_id();

store.application.set_room_name(room_id, name.as_str());
let name = store
.application
.get_room_info(room_id.to_owned())
.name
.clone()
.unwrap_or_default();

if let Some(alias) = room.canonical_alias() {
store.application.names.insert(alias.to_string(), room_id.to_owned());
Expand Down
5 changes: 3 additions & 2 deletions src/windows/room/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ impl<'a> StatefulWidget for Space<'a> {
let items = members
.into_iter()
.filter_map(|id| {
let (room, name, tags) =
let (room, _, tags) =
self.store.application.worker.get_room(id.clone()).ok()?;
let room_info = std::sync::Arc::new((room, tags));

if id != state.room_id {
Some(RoomItem::new(room, name, tags, self.store))
Some(RoomItem::new(room_info, self.store))
} else {
None
}
Expand Down

0 comments on commit 61aba80

Please sign in to comment.