perf: remove lazy_static

This commit is contained in:
amizing25 2025-03-09 05:09:09 +07:00
parent 7d233dc614
commit 6d9bf58a85
9 changed files with 78 additions and 106 deletions

View File

@ -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.

View File

@ -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

View File

@ -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<u64> = 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 {

View File

@ -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<UdpSocket>,
@ -33,6 +33,7 @@ pub struct PlayerSession {
pub shutdown_tx: watch::Sender<()>,
pub shutdown_rx: watch::Receiver<()>,
pub json_data: OnceLock<FreesrData>,
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,
}
}

View File

@ -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

View File

@ -1,3 +1 @@
mod version_config;
pub use version_config::INSTANCE as versions;
pub mod version_config;

View File

@ -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<String, VersionConfig> = {
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<String, VersionConfig> {
static INSTANCE: OnceLock<HashMap<String, VersionConfig>> = 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}"))
})
}

View File

@ -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<QueryGatewayParameters>) -> String {
let rsp = if let Some(config) = versions.get(&parameters.version) {
let rsp = if let Some(config) = version_config::instance().get(&parameters.version) {
Gateserver {
retcode: 0,
ip: String::from("127.0.0.1"),

View File

@ -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"
}
}