Agent Skills: Rust Optimization Skill

当用户要求"优化 Rust 代码"、"提高性能"、"减少内存使用"、"并行计算"、"缓存策略"、"HiGHS 求解器优化"、"数值计算优化"、"Moka 缓存"、"Rayon 并行"、"避免克隆"、"减少分配"、"SIMD 优化"、"工作窃取"、"异步性能"、"零成本抽象"、"内存优化"、"并发优化",或者提到"性能优化"、"Rust优化"、"优化"、"配方计算性能"时使用此技能。用于优化 Rust 代码性能、内存管理、使用 Rayon 并行处理、实现 Moka 缓存策略或处理饲料配方优化系统中的数值计算和线性规划。

UncategorizedID: cacr92/wereply/rust-optimization

Install this agent skill to your local

pnpm dlx add-skill https://github.com/cacr92/WeReply/tree/HEAD/.trae/skills/rust-optimization

Skill Files

Browse the full folder contents for rust-optimization.

Download Skill

Loading file tree…

.trae/skills/rust-optimization/SKILL.md

Skill Metadata

Name
rust-optimization
Description
当用户要求Rust性能优化、缓存策略、并行计算、内存优化或线性规划加速时使用。

Rust Optimization Skill

适用范围

  • Rust 性能与内存优化
  • 缓存/并行计算策略
  • 线性规划与数值计算加速

关键规则(Critical Rules)

  • 高频静态数据优先缓存,使用 moka
  • CPU 密集任务使用 rayontokio::task::spawn_blocking
  • 避免无意义 clone,优先借用切片与引用
  • 异步上下文中避免阻塞调用

快速模板

Moka 缓存

use moka::future::Cache;
use std::time::Duration;

pub struct MaterialCache {
    cache: Cache<String, crate::material::material::Material>,
}

impl MaterialCache {
    pub fn new() -> Self {
        Self {
            cache: Cache::builder()
                .max_capacity(1000)
                .time_to_live(Duration::from_secs(3600))
                .build(),
        }
    }
}

Rayon 并行

use rayon::prelude::*;

let totals: Vec<f64> = materials
    .par_iter()
    .map(|m| m.price)
    .collect();

阻塞计算下沉

let result = tokio::task::spawn_blocking(move || heavy_calc(input))
    .await?;

优化要点

  • 大量查询前先裁剪数据范围
  • 频繁计算值可缓存,避免重复计算
  • 共享只读配置使用 Arc,可变状态用 tokio::sync

检查清单

  • [ ] 是否存在重复计算可缓存
  • [ ] CPU 密集任务是否并行化
  • [ ] 异步路径无阻塞调用
  • [ ] clone 明确且必要