From 6d9bf58a851509f42a49dd8633dcf0fda11fbeea Mon Sep 17 00:00:00 2001 From: amizing25 Date: Sun, 9 Mar 2025 05:09:09 +0700 Subject: [PATCH] perf: remove `lazy_static` --- Cargo.toml | 45 +++++++++----------------- gameserver/Cargo.toml | 40 ++++++++++++----------- gameserver/src/net/handlers/scene.rs | 12 ++----- gameserver/src/net/session.rs | 16 +++++---- sdkserver/Cargo.toml | 27 ++++++++-------- sdkserver/src/config/mod.rs | 4 +-- sdkserver/src/config/version_config.rs | 28 ++++++++-------- sdkserver/src/services/dispatch.rs | 4 +-- sdkserver/versions.json | 8 ----- 9 files changed, 78 insertions(+), 106 deletions(-) delete mode 100644 sdkserver/versions.json diff --git a/Cargo.toml b/Cargo.toml index 2580b5c..679a56e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,19 +6,22 @@ resolver = "3" version = "0.1.0" [workspace.dependencies] -anyhow = "1.0.81" -ansi_term = "0.12.1" -atomic_refcell = "0.1.13" -lazy_static = "1.4.0" - +# Framework +tokio = { version = "1.36.0", features = ["full"] } axum = "0.8.1" axum-server = "0.7.1" tower-http = "0.6.2" +# JSON +serde = { version = "1.0.197", features = ["derive"] } +serde_json = "1.0.114" + +# Logging +tracing = "0.1.40" +ansi_term = "0.12.1" env_logger = "0.11.3" -rbase64 = "2.0.3" -rand = "0.9.0" +# Cryptography rsa = { version = "0.9.6", features = [ "sha1", "nightly", @@ -26,42 +29,26 @@ rsa = { version = "0.9.6", features = [ "serde", "sha2", ] } +rand = "0.9.0" +# Serialization prost = "0.13.5" prost-types = "0.13.5" prost-build = "0.13.5" +rbase64 = "2.0.3" +# Utilities paste = "1.0.14" -sysinfo = "0.33.1" notify = "8.0.0" notify-debouncer-mini = "0.6.0" +anyhow = "1.0.81" -hex = "0.4.3" - -serde = { version = "1.0.197", features = ["derive"] } -serde_json = "1.0.114" - -tokio = { version = "1.36.0", features = ["full"] } -tokio-util = { version = "0.7.10", features = ["io"] } - -tracing = "0.1.40" -tracing-futures = "0.2.5" -tracing-log = { version = "0.2.0", features = ["std", "log-tracer"] } -tracing-subscriber = { version = "0.3.18", features = [ - "env-filter", - "registry", - "std", - "tracing", - "tracing-log", -] } -tracing-bunyan-formatter = "0.3.9" - +# Local proto = { path = "proto/" } proto-derive = { path = "proto/proto-derive" } mhy-kcp = { path = "kcp/", features = ["tokio"] } common = { path = "common/" } - [profile.release] strip = true # Automatically strip symbols from the binary. opt-level = "z" # Optimize for size. diff --git a/gameserver/Cargo.toml b/gameserver/Cargo.toml index 5264800..08a91df 100644 --- a/gameserver/Cargo.toml +++ b/gameserver/Cargo.toml @@ -4,33 +4,35 @@ edition = "2024" version.workspace = true [dependencies] -ansi_term.workspace = true -anyhow.workspace = true -atomic_refcell.workspace = true -env_logger.workspace = true -hex.workspace = true -lazy_static.workspace = true -paste.workspace = true -rbase64.workspace = true -notify.workspace = true -notify-debouncer-mini.workspace = true +# Framework +tokio.workspace = true +# JSON serde.workspace = true serde_json.workspace = true -tokio.workspace = true -tokio-util.workspace = true - +# Logging tracing.workspace = true -tracing-futures.workspace = true -tracing-log.workspace = true -tracing-subscriber.workspace = true -tracing-bunyan-formatter.workspace = true +ansi_term.workspace = true +env_logger.workspace = true +# Encoding / Serialization prost.workspace = true -proto.workspace = true -proto-derive.workspace = true +rbase64.workspace = true +# Cryptography rand.workspace = true + +# Utilities +paste.workspace = true +notify.workspace = true +notify-debouncer-mini.workspace = true + +# Error handling +anyhow.workspace = true + +# Local mhy-kcp.workspace = true common.workspace = true +proto.workspace = true +proto-derive.workspace = true diff --git a/gameserver/src/net/handlers/scene.rs b/gameserver/src/net/handlers/scene.rs index aa4468f..bd137ec 100644 --- a/gameserver/src/net/handlers/scene.rs +++ b/gameserver/src/net/handlers/scene.rs @@ -4,9 +4,7 @@ use common::{ resources::GAME_RES, sr_tools::{AvatarJson, Position}, }; -use lazy_static::lazy_static; use scene_entity_info::Entity; -use tokio::sync::Mutex; use crate::util::{self}; @@ -117,10 +115,6 @@ pub async fn on_get_scene_map_info_cs_req( } } -lazy_static! { - static ref NEXT_SCENE_SAVE: Mutex = Mutex::new(0); -} - pub async fn on_scene_entity_move_cs_req( session: &mut PlayerSession, req: &SceneEntityMoveCsReq, @@ -131,14 +125,12 @@ pub async fn on_scene_entity_move_cs_req( return; }; - let mut timestamp = NEXT_SCENE_SAVE.lock().await; - - if util::cur_timestamp_ms() <= *timestamp { + if util::cur_timestamp_ms() <= session.next_scene_save { return; } // save every 5 sec - *timestamp = util::cur_timestamp_ms() + (5 * 1000); + session.next_scene_save = util::cur_timestamp_ms() + (5 * 1000); for entity in &req.entity_motion_list { if entity.entity_id != 0 { diff --git a/gameserver/src/net/session.rs b/gameserver/src/net/session.rs index 2b9875a..c956102 100644 --- a/gameserver/src/net/session.rs +++ b/gameserver/src/net/session.rs @@ -14,12 +14,12 @@ use proto::{AvatarSync, CmdID, CmdPlayerType, PlayerSyncScNotify}; use tokio::{ io::AsyncWrite, net::UdpSocket, - sync::{Mutex, watch}, + sync::{watch, Mutex}, }; use crate::util; -use super::{NetPacket, packet::CommandHandler}; +use super::{packet::CommandHandler, NetPacket}; struct RemoteEndPoint { socket: Arc, @@ -33,6 +33,7 @@ pub struct PlayerSession { pub shutdown_tx: watch::Sender<()>, pub shutdown_rx: watch::Receiver<()>, pub json_data: OnceLock, + pub next_scene_save: u64, } impl PlayerSession { @@ -40,14 +41,17 @@ impl PlayerSession { let (shutdown_tx, shutdown_rx) = watch::channel(()); Self { token, - kcp: Arc::new(Mutex::new(Kcp::new(conv, token, false, RemoteEndPoint { - socket, - addr, - }))), + kcp: Arc::new(Mutex::new(Kcp::new( + conv, + token, + false, + RemoteEndPoint { socket, addr }, + ))), start_time: util::cur_timestamp_secs(), json_data: OnceLock::new(), shutdown_rx, shutdown_tx, + next_scene_save: 0, } } diff --git a/sdkserver/Cargo.toml b/sdkserver/Cargo.toml index 682ac1d..ae7a8e2 100644 --- a/sdkserver/Cargo.toml +++ b/sdkserver/Cargo.toml @@ -4,29 +4,28 @@ version = "0.1.0" edition = "2024" [dependencies] -anyhow.workspace = true -env_logger.workspace = true - -tower-http = { workspace = true, features = ["cors"]} +# Framework +tokio.workspace = true +tower-http = { workspace = true, features = ["cors"] } axum.workspace = true axum-server.workspace = true -lazy_static.workspace = true - +# JSON serde.workspace = true serde_json.workspace = true -tokio.workspace = true -tokio-util.workspace = true - +# Logging tracing.workspace = true -tracing-futures.workspace = true -tracing-log.workspace = true -tracing-subscriber.workspace = true -tracing-bunyan-formatter.workspace = true ansi_term.workspace = true +env_logger.workspace = true +# Encoding / Serialization prost.workspace = true rbase64.workspace = true -proto.workspace = true + +# Error handling +anyhow.workspace = true + +# Local common.workspace = true +proto.workspace = true diff --git a/sdkserver/src/config/mod.rs b/sdkserver/src/config/mod.rs index 9b7de45..8acca4a 100644 --- a/sdkserver/src/config/mod.rs +++ b/sdkserver/src/config/mod.rs @@ -1,3 +1 @@ -mod version_config; - -pub use version_config::INSTANCE as versions; +pub mod version_config; diff --git a/sdkserver/src/config/version_config.rs b/sdkserver/src/config/version_config.rs index 9289d4f..97b500d 100644 --- a/sdkserver/src/config/version_config.rs +++ b/sdkserver/src/config/version_config.rs @@ -1,10 +1,8 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::OnceLock}; -use lazy_static::lazy_static; use serde::Deserialize; -use serde_json::from_str; -const DEFAULT_VERSIONS: &str = include_str!("../../versions.json"); +const DEFAULT_VERSIONS: &str = include_str!("../../../versions.json"); #[derive(Deserialize)] pub struct VersionConfig { @@ -14,16 +12,16 @@ pub struct VersionConfig { // pub lua_version: String, } -lazy_static! { - pub static ref INSTANCE: HashMap = { - let local_config = std::path::Path::new("versions.json"); - let data = if local_config.exists() { - std::fs::read_to_string("versions.json").unwrap() - } else { - std::fs::write("versions.json", DEFAULT_VERSIONS).unwrap(); - DEFAULT_VERSIONS.to_string() - }; +pub fn instance() -> &'static HashMap { + static INSTANCE: OnceLock> = OnceLock::new(); + INSTANCE.get_or_init(|| { + const CONFIG_PATH: &str = "versions.json"; - from_str(&data).unwrap() - }; + let data = std::fs::read_to_string(CONFIG_PATH).unwrap_or_else(|_| { + std::fs::write(CONFIG_PATH, DEFAULT_VERSIONS).expect("Failed to create versions file"); + DEFAULT_VERSIONS.to_string() + }); + + serde_json::from_str(&data).unwrap_or_else(|e| panic!("Failed to parse versions.json: {e}")) + }) } diff --git a/sdkserver/src/services/dispatch.rs b/sdkserver/src/services/dispatch.rs index e07c026..a4e4dd1 100644 --- a/sdkserver/src/services/dispatch.rs +++ b/sdkserver/src/services/dispatch.rs @@ -1,4 +1,4 @@ -use crate::config::versions; +use crate::config::version_config; use axum::extract::Query; use prost::Message; use proto::{DispatchRegionData, Gateserver, RegionEntry}; @@ -34,7 +34,7 @@ pub struct QueryGatewayParameters { #[tracing::instrument] pub async fn query_gateway(parameters: Query) -> String { - let rsp = if let Some(config) = versions.get(¶meters.version) { + let rsp = if let Some(config) = version_config::instance().get(¶meters.version) { Gateserver { retcode: 0, ip: String::from("127.0.0.1"), diff --git a/sdkserver/versions.json b/sdkserver/versions.json deleted file mode 100644 index 6753cc6..0000000 --- a/sdkserver/versions.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "OSBETAWin3.1.51": { - "asset_bundle_url": "https://autopatchos.starrails.com/asb/BetaLive/output_9573347_b03981f01966", - "ex_resource_url": "https://autopatchos.starrails.com/design_data/BetaLive/output_9589567_9c50629b0369", - "lua_url": "https://autopatchos.starrails.com/lua/BetaLive/output_9567078_0e2b6acf6a2f", - "ifix_url": "https://autopatchos.starrails.com/ifix/BetaLive/output_0_40d2ce0253" - } -}