Skip to content

Commit 9ff2103

Browse files
committed
perf: use Arc instead of clone for GPT type
1 parent 81d896c commit 9ff2103

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/handler.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use std::sync::mpsc::Sender;
88
use std::{collections::HashMap, thread};
99

1010
use crate::notification::{Notification, NotificationLevel};
11+
use std::sync::Arc;
1112

1213
pub fn handle_key_events(
1314
key_event: KeyEvent,
1415
app: &mut App,
15-
gpt: &GPT,
16+
gpt: Arc<GPT>,
1617
sender: Sender<Event>,
1718
) -> AppResult<()> {
1819
match app.mode {
@@ -43,9 +44,9 @@ pub fn handle_key_events(
4344
app.gpt_messages.push(conv.clone());
4445

4546
let gpt_messages = app.gpt_messages.clone();
46-
let gpt = gpt.clone();
47+
4748
thread::spawn(move || {
48-
let response = gpt.ask(gpt_messages);
49+
let response = gpt.ask(gpt_messages.to_vec());
4950
sender
5051
.send(Event::GPTResponse(match response {
5152
Ok(answer) => answer,

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ use tenere::tui::Tui;
99
use tui::backend::CrosstermBackend;
1010
use tui::Terminal;
1111

12+
use std::sync::Arc;
13+
1214
use clap::crate_version;
1315

1416
fn main() -> AppResult<()> {
1517
cli::cli().version(crate_version!()).get_matches();
1618

1719
let mut app = App::new();
18-
let gpt = GPT::new(app.config.gpt.openai_api_key.clone());
20+
let gpt = Arc::new(GPT::new(app.config.gpt.openai_api_key.clone()));
1921

2022
let backend = CrosstermBackend::new(io::stderr());
2123
let terminal = Terminal::new(backend)?;
@@ -28,7 +30,7 @@ fn main() -> AppResult<()> {
2830
match tui.events.next()? {
2931
Event::Tick => app.tick(),
3032
Event::Key(key_event) => {
31-
handle_key_events(key_event, &mut app, &gpt, tui.events.sender.clone())?
33+
handle_key_events(key_event, &mut app, gpt.clone(), tui.events.sender.clone())?
3234
}
3335
Event::Mouse(_) => {}
3436
Event::Resize(_, _) => {}

0 commit comments

Comments
 (0)