Skip to content

Commit

Permalink
Add an option to change the size of the pool of candidates
Browse files Browse the repository at this point in the history
This can be changed through the CLI args or in the config file.
  • Loading branch information
jhbabon committed Jan 23, 2023
1 parent ce7d83a commit 7a71b59
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- If the setting `screen.lines` is present in the TOML config file, apply it only on inline mode
- Respect `screen.lines` option from config file when no `--lines` argument is given
- Ensure `-v` flag is in lowercase in CLI help message
### Added
- New argument, `--pool`, and config option `advanced.pool_size` (or `advanced.pool`).
This option can change the size of the pool of candidates that are kept in memory.
Increasing this number might result in the program using too much memory.
This is an advanced feature and should be used with care.

### Changed
- Bump `unicode-segmentation` 1.9.0 to 1.10.0
- Bump `futures` from 0.3.21 to 0.3.25
- Bump `env_logger` from 0.9.0 to 0.10.0
- Bump `libc` from 0.2.129 to 0.2.139

### Fixed
- If the setting `screen.lines` is present in the TOML config file, apply it only on inline mode
- Respect `screen.lines` option from config file when no `--lines` argument is given
- Ensure `-v` flag is in lowercase in CLI help message

## [v2.6.0] 2022-01-23
### Changed
- Update to edition 2021 and set rust-version to 1.58
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Args {
pub lines: Option<usize>,
pub config: Option<String>,
pub search: Option<String>,
pub pool: Option<usize>,
}

/// Arc version of Cfg
Expand All @@ -46,6 +47,9 @@ pub struct Cfg {
pub candidate: CandidateConfig,
#[serde(default)]
pub selection: SelectionConfig,

#[serde(default)]
pub advanced: AdvancedConfig,
}

/// Configuration constructor
Expand Down Expand Up @@ -125,6 +129,10 @@ impl Configurator {
config.initial_query = Some(q);
}

if let Some(pool) = args.pool {
config.advanced.set_pool_size(pool);
}

self.config = Some(config);
}

Expand Down
19 changes: 19 additions & 0 deletions src/config/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const DEFAULT_HEIGHT: usize = 6;
const MIN_HEIGHT: usize = 3;
const MIN_WIDTH: usize = 4;

const DEFAULT_POOL_SIZE: usize = 50000;

#[derive(Deserialize, Clone, Debug, PartialEq)]
enum Mode {
#[serde(rename = "full")]
Expand Down Expand Up @@ -91,6 +93,23 @@ impl ScreenConfig {
}
}

/// Main advanced set of configuration options
#[derive(Deserialize, Clone, Debug, Default)]
pub struct AdvancedConfig {
#[serde(default, alias = "pool")]
pool_size: Option<usize>,
}

impl AdvancedConfig {
pub fn pool_size(&self) -> usize {
self.pool_size.unwrap_or(DEFAULT_POOL_SIZE)
}

pub fn set_pool_size(&mut self, pool_size: usize) {
self.pool_size = Some(pool_size)
}
}

/// Prompt UI component configuration options
///
/// The prompt is where you write the search query
Expand Down
16 changes: 9 additions & 7 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
//! Once a search is done all the results will be sent to the screen.
use crate::common::{Result, Text, TextBuilder};
use crate::config::Config;
use crate::events::Event;
use crate::fuzzy;
use async_std::channel::{Receiver, Sender};
use async_std::prelude::*;
use std::collections::VecDeque;

const BUFFER_LIMIT: usize = 5000;
const POOL_LIMIT: usize = 50000;

/// Run the search engine task
pub async fn task(mut input_recv: Receiver<Event>, output_sender: Sender<Event>) -> Result<()> {
pub async fn task(
config: Config,
mut input_recv: Receiver<Event>,
output_sender: Sender<Event>,
) -> Result<()> {
log::trace!("starting search engine");

let pool_size = config.advanced.pool_size();
let mut pool: VecDeque<Text> = VecDeque::new();
let mut count = 0;
let mut query = String::from("");
Expand All @@ -32,11 +37,8 @@ pub async fn task(mut input_recv: Receiver<Event>, output_sender: Sender<Event>)

// The pool might be full (too many lines in memory)
// so we drop the first line
if pool.len() > POOL_LIMIT {
log::trace!(
"pool limit ({:?}) exceeded, dropping first line",
POOL_LIMIT
);
if pool.len() > pool_size {
log::trace!("pool limit ({:?}) exceeded, dropping first line", pool_size);
let _f = pool.pop_front();
}

Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ OPTIONS:
-c, --config <FILE> Uses a custom config file
-l, --lines <LINES> Number of lines to display in inline mode, including prompt
-s, --search <QUERY> Start searching with the given query
-p, --pool <SIZE> Advanced: size of the pool of candidates to keep in memory.
Default is 50000. Note that increasing this number might
result in the program using too much memory
SUPPORTED KEYS:
- Enter to select the current highlighted match and print it to STDOUT
Expand Down Expand Up @@ -158,6 +161,7 @@ fn parse_args() -> std::result::Result<Args, pico_args::Error> {
search,
lines: pargs.opt_value_from_str(["-l", "--lines"])?,
config: pargs.opt_value_from_str(["-c", "--config"])?,
pool: pargs.opt_value_from_str(["-p", "--pool"])?,
};

let remaining = pargs.finish();
Expand Down
4 changes: 2 additions & 2 deletions src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ where

let screen_task = task::spawn(screen::task(config.clone(), outbox, output_recv));
let person_task = task::spawn(person_input::task(
config,
config.clone(),
inbox,
input_sender.clone(),
output_sender.clone(),
));
let engine_task = task::spawn(engine::task(input_recv, output_sender));
let engine_task = task::spawn(engine::task(config, input_recv, output_sender));
let data_task = task::spawn(data_input::task(stdin, input_sender));

let selection = screen_task.await;
Expand Down

0 comments on commit 7a71b59

Please sign in to comment.