From cfb19b5783cb446099cb3e4aff70cc39beec2e88 Mon Sep 17 00:00:00 2001 From: boris Date: Sat, 5 Sep 2026 03:42:21 +0800 Subject: [PATCH] perf: avoid board normalization allocations --- crates/fidc-core/src/instrument.rs | 58 ++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/crates/fidc-core/src/instrument.rs b/crates/fidc-core/src/instrument.rs index 2935c60..454c597 100644 --- a/crates/fidc-core/src/instrument.rs +++ b/crates/fidc-core/src/instrument.rs @@ -21,17 +21,29 @@ impl Instrument { } pub fn minimum_order_quantity(&self) -> u32 { - match self.board.trim().to_ascii_uppercase().as_str() { - "KSH" => 200, - "BJS" | "BJ" | "BJSE" => 100, - _ => self.effective_round_lot(), + let board = self.board.trim(); + if board.eq_ignore_ascii_case("KSH") { + 200 + } else if board.eq_ignore_ascii_case("BJS") + || board.eq_ignore_ascii_case("BJ") + || board.eq_ignore_ascii_case("BJSE") + { + 100 + } else { + self.effective_round_lot() } } pub fn order_step_size(&self) -> u32 { - match self.board.trim().to_ascii_uppercase().as_str() { - "KSH" | "BJS" | "BJ" | "BJSE" => 1, - _ => self.effective_round_lot(), + let board = self.board.trim(); + if board.eq_ignore_ascii_case("KSH") + || board.eq_ignore_ascii_case("BJS") + || board.eq_ignore_ascii_case("BJ") + || board.eq_ignore_ascii_case("BJSE") + { + 1 + } else { + self.effective_round_lot() } } @@ -56,6 +68,38 @@ fn default_status() -> String { "active".to_string() } +#[cfg(test)] +mod tests { + use super::Instrument; + + fn instrument(board: &str, round_lot: u32) -> Instrument { + Instrument { + symbol: "000001.SZ".to_string(), + name: "test".to_string(), + board: board.to_string(), + round_lot, + listed_at: None, + delisted_at: None, + status: "active".to_string(), + } + } + + #[test] + fn order_quantity_rules_are_case_insensitive_without_allocating_normalized_boards() { + let kcb = instrument(" kSh ", 100); + assert_eq!(kcb.minimum_order_quantity(), 200); + assert_eq!(kcb.order_step_size(), 1); + + let bjse = instrument("bjse", 100); + assert_eq!(bjse.minimum_order_quantity(), 100); + assert_eq!(bjse.order_step_size(), 1); + + let main_board = instrument("SZSE", 50); + assert_eq!(main_board.minimum_order_quantity(), 50); + assert_eq!(main_board.order_step_size(), 50); + } +} + mod optional_date_format { use chrono::NaiveDate; use serde::{self, Deserialize, Deserializer, Serializer};