You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust程序在Core i9 Ubuntu上运行性能逊于Core i7 macOS

Rust xdevs模拟器跨平台性能反常问题

问题概述

测试Rust编写的xdevs模拟器时出现不符合硬件规格的性能差异:

  • 搭载2.5GHz四核Intel Core i7的MacBook Pro(macOS Monterey):模拟耗时0.8-0.99秒
  • 搭载Intel Core i9-10900X(3.70GHz)的Ubuntu 20.04工作站:模拟耗时约2.7秒

编译时已启用**链接时优化(LTO)**和target-cpu=native参数,两台设备使用相同版本rustc(1.66.0),补充工具链配置及Release模式运行详情后仍无法定位原因。

测试运行步骤:

git clone https://github.com/iscar-ucm/xdevs.rs.git
cd xdevs.rs
cargo run --release HO 200 200

设备rustc配置

Mac设备

➜  xdevs.rs git:(main) rustc --version
rustc 1.66.0 (69f9c33d7 2022-12-12)
➜  xdevs.rs git:(main) rustc -C target-cpu=native --print cfg
debug_assertions
panic="unwind"
target_arch="x86_64"
target_endian="little"
target_env=""
target_family="unix"
target_feature="aes"
target_feature="avx"
target_feature="avx2"
target_feature="bmi1"
target_feature="bmi2"
target_feature="fma"
target_feature="fxsr"
target_feature="lzcnt"
target_feature="pclmulqdq"
target_feature="popcnt"
target_feature="rdrand"
target_feature="sse"
target_feature="sse2"
target_feature="sse3"
target_feature="sse4.1"
target_feature="sse4.2"
target_feature="ssse3"
target_feature="xsave"
target_feature="xsaveopt"
target_has_atomic="128"
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_os="macos"
target_pointer_width="64"
target_vendor="apple"
unix

Ubuntu设备

rcardenas@celsius:~/xdevs.rs$ rustc --version
rustc 1.66.0 (69f9c33d7 2022-12-12)
rcardenas@celsius:~/xdevs.rs$ rustc -C target-cpu=native --print cfg
debug_assertions
panic="unwind"
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="adx"
target_feature="aes"
target_feature="avx"
target_feature="avx2"
target_feature="bmi1"
target_feature="bmi2"
target_feature="fma"
target_feature="fxsr"
target_feature="lzcnt"
target_feature="pclmulqdq"
target_feature="popcnt"
target_feature="rdrand"
target_feature="rdseed"
target_feature="sse"
target_feature="sse2"
target_feature="sse3"
target_feature="sse4.1"
target_feature="sse4.2"
target_feature="ssse3"
target_feature="xsave"
target_feature="xsavec"
target_feature="xsaveopt"
target_feature="xsaves"
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix

Release模式运行结果

Mac设备

➜  xdevs.rs git:(main) RUSTFLAGS="-C target-cpu=native" cargo run --verbose --release HO 200 200
   Compiling xdevs v0.1.1 (/Users/rcardenas/xdevs.rs)
     Running `rustc --crate-name xdevs --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=8d925d6ed50d8948 -C extra-filename=-8d925d6ed50d8948 --out-dir /Users/rcardenas/xdevs.rs/target/release/deps -L dependency=/Users/rcardenas/xdevs.rs/target/release/deps -C target-cpu=native`
     Running `rustc --crate-name xdevs --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=7179874cf29eab82 -C extra-filename=-7179874cf29eab82 --out-dir /Users/rcardenas/xdevs.rs/target/release/deps -L dependency=/Users/rcardenas/xdevs.rs/target/release/deps --extern xdevs=/Users/rcardenas/xdevs.rs/target/release/deps/libxdevs-8d925d6ed50d8948.rlib -C target-cpu=native`
    Finished release [optimized] target(s) in 1.87s
     Running `target/release/xdevs HO 200 200`
Model creation time: 99.400995ms
Simulator creation time: 66ns
Simulation time: 840.980883ms

Ubuntu设备

rcardenas@celsius:~/xdevs.rs$ RUSTFLAGS="-C target-cpu=native" cargo run --verbose --release HO 200 200
   Compiling xdevs v0.1.1 (/home/rcardenas/xdevs.rs)
     Running `rustc --crate-name xdevs --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=f8b9c1c6a690ac7c -C extra-filename=-f8b9c1c6a690ac7c --out-dir /home/rcardenas/xdevs.rs/target/release/deps -L dependency=/home/rcardenas/xdevs.rs/target/release/deps -C target-cpu=native`
     Running `rustc --crate-name xdevs --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=1a0bd9ecd09448cd -C extra-filename=-1a0bd9ecd09448cd --out-dir /home/rcardenas/xdevs.rs/target/release/deps -L dependency=/home/rcardenas/xdevs.rs/target/release/deps --extern xdevs=/home/rcardenas/xdevs.rs/target/release/deps/libxdevs-f8b9c1c6a690ac7c.rlib -C target-cpu=native`
    Finished release [optimized] target(s) in 0.96s
     Running `target/release/xdevs HO 200 200`
Model creation time: 69.743935ms
Simulator creation time: 121ns
Simulation time: 2.782144931s

排查方向与解决方案

1. 内存分配器差异

macOS默认使用系统分配器,Linux上rustc 1.66默认使用jemalloc,但部分环境可能切换到系统分配器。可尝试两种调整方式:

  • 强制使用系统分配器:
RUSTFLAGS="-C target-cpu=native -C allocator_system" cargo run --release HO 200 200
  • 显式指定jemalloc:
    在Cargo.toml添加依赖:
[dependencies]
jemallocator = "0.5"

在src/main.rs开头添加:

use jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

重新编译运行。

2. CPU频率调度

Linux工作站可能处于节能模式,CPU未达到满睿频。检查当前CPU频率:

cat /proc/cpuinfo | grep "cpu MHz"

设置性能模式:

sudo cpupower frequency-set --governor performance

再次运行测试对比耗时。

3. 编译优化强化

虽然已启用target-cpu=native,可尝试全量LTO进一步优化:

RUSTFLAGS="-C target-cpu=native -C opt-level=3 -C lto=fat" cargo run --release HO 200 200

4. 系统调用与IO分析

使用跟踪工具对比跨平台系统调用差异:

  • Linux:
strace -c target/release/xdevs HO 200 200
  • macOS:
sudo dtruss -c target/release/xdevs HO 200 200

重点关注调用次数多、耗时高的系统调用。

5. 缓存行为分析

使用perf工具分析Ubuntu设备的缓存命中率:

sudo perf stat -e cache-misses,cache-references target/release/xdevs HO 200 200

若缓存命中率低,说明模拟器内存访问模式可能更适配Mac的缓存架构,可针对性优化内存布局。

内容的提问来源于stack exchange,提问作者Román Cárdenas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 20:20:36