Skip to content

Commit

Permalink
stuff and some tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Dec 7, 2023
1 parent 067b53d commit dbc8622
Show file tree
Hide file tree
Showing 65 changed files with 602 additions and 258 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ mod-a.json
server/bench-client
server/target
server/central-conf.json
server/Cargo.lock
server/Cargo.lock
server/game/flamegraph.svg
server/game/perf.data
server/game/perf.data.old
server/game/test-flamegraph.sh
server/game/test-bench.sh
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@ file(GLOB_RECURSE SOURCES
# Include winsock
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_compile_definitions(WIN32_LEAN_AND_MEAN=1) # geode moment
add_definitions(/FI"winsock2.h")
add_definitions(/FI"WinSock2.h")
endif()

# i am crying so hard right now
if (CMAKE_HOST_SYSTEM MATCHES "Linux" AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_compile_options("-march=skylake")
endif()

# disable iterator debugging if building in debug
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(_HAS_ITERATOR_DEBUGGING=0)
# enable exceptions on android
if (ANDROID)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
endif()

add_library(${PROJECT_NAME} SHARED ${SOURCES})

# enable extra warnings
if (CMAKE_HOST_SYSTEM STREQUAL "Windows")
else()
# i hate this language okay?
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-c++11-compat -Wno-c++14-compat -Wno-c++17-compat -Wno-old-style-cast -Wno-implicit-int-float-conversion -Wno-global-constructors -Wno-pre-c++20-compat-pedantic -Wno-exit-time-destructors -Wno-reserved-identifier -Wno-reserved-macro-identifier -Wno-dollar-in-identifier-extension -Wno-ctad-maybe-unsupported -Wno-unsafe-buffer-usage -Wno-newline-eof -Wno-shadow -Wno-inconsistent-missing-destructor-override -Wno-float-conversion -Wno-shorten-64-to-32 -Wno-sign-conversion -Wno-suggest-destructor-override -Wno-suggest-override -Wno-overloaded-virtual -Wno-unused-parameter -Wno-undefined-func-template -Wno-non-virtual-dtor -Wno-sign-compare -Wno-header-hygiene)
endif()

if (NOT DEFINED ENV{GEODE_SDK})
message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode")
else()
Expand Down
10 changes: 5 additions & 5 deletions server/central/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::HashMap,
fs::{File, OpenOptions},
path::Path,
};
Expand Down Expand Up @@ -47,8 +47,8 @@ fn default_userlist_mode() -> UserlistMode {
UserlistMode::None
}

fn default_userlist() -> HashSet<i32> {
HashSet::new()
fn default_userlist() -> Vec<i32> {
Vec::new()
}

fn default_tps() -> u32 {
Expand Down Expand Up @@ -121,9 +121,9 @@ pub struct ServerConfig {
#[serde(default = "default_userlist_mode")]
pub userlist_mode: UserlistMode,
#[serde(default = "default_userlist")]
pub userlist: HashSet<i32>,
pub userlist: Vec<i32>,
#[serde(default = "default_userlist")]
pub no_chat_list: HashSet<i32>,
pub no_chat_list: Vec<i32>,

// game stuff
#[serde(default = "default_tps")]
Expand Down
2 changes: 1 addition & 1 deletion server/game-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ struct PacketAttributes {
encrypted: bool,
}

/// Implements `Packet`, `PacketMetadata` and the function `header() -> PacketHeader` for the given struct.
/// Implements `Packet`, `PacketMetadata` and the function `const fn header() -> PacketHeader` for the given struct.
/// You must also pass additional attributes with `#[packet]`, specifically packet ID and whether the packet should be encrypted.
/// Example:
/// ```rust
Expand Down
14 changes: 14 additions & 0 deletions server/game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ anyhow = "1.0.75"
array-init = "2.1.0"
bytebuffer = "2.2.0"
crypto_box = { version = "0.9.1", features = ["std", "chacha20"] }
nohash-hasher = "0.2.0"
parking_lot = "0.12.1"
reqwest = "0.11.22"
rustc-hash = "1.1.0"
serde = { version = "1.0.193", features = ["serde_derive"] }
serde_json = "1.0.108"
tokio = { version = "1.34.0", features = ["full"] }

[dev-dependencies]
criterion = "0.5.1"
rand = "0.8.5"

[[bench]]
name = "globed-bench"
path = "benchmarks/bench.rs"
harness = false

[[test]]
name = "globed-tests"
path = "tests/test.rs"
133 changes: 133 additions & 0 deletions server/game/benchmarks/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#![allow(clippy::wildcard_imports)]
use bytebuffer::{ByteBuffer, ByteReader};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use globed_game_server::{data::*, managers::PlayerManager};
use rand::RngCore;

fn buffers(c: &mut Criterion) {
let data = PlayerAccountData {
account_id: 234_234_234,
name: FastString::from_str("hit his is my name"),
icons: PlayerIconData::default(),
special_user_data: Some(SpecialUserData {
name_color: Color3B { r: 10, g: 100, b: 200 },
}),
};

c.bench_function("alloca-byte-buffer", |b| {
b.iter(black_box(|| {
alloca::with_alloca(PlayerAccountData::ENCODED_SIZE * 64, |data_| {
let stackarray = unsafe {
let ptr = data_.as_mut_ptr().cast::<u8>();
std::slice::from_raw_parts_mut(ptr, std::mem::size_of_val(data_))
};

let mut buf = FastByteBuffer::new(stackarray);
for _ in 0..64 {
buf.write_value(&data);
}

// let mut buf = ByteReader::from_bytes(stackarray);
// for _ in 0..64 {
// assert_eq!(buf.read_value::<PlayerAccountData>().unwrap().account_id, data.account_id);
// }
});
}));
});

c.bench_function("fast-byte-buffer", |b| {
b.iter(black_box(|| {
let mut stackarray = [0u8; PlayerAccountData::ENCODED_SIZE * 64];
let mut buf = FastByteBuffer::new(&mut stackarray);
for _ in 0..64 {
buf.write_value(&data);
}

// let mut buf = ByteReader::from_bytes(&stackarray);
// for _ in 0..64 {
// assert_eq!(buf.read_value::<PlayerAccountData>().unwrap().account_id, data.account_id);
// }
}));
});

c.bench_function("slow-byte-buffer", |b| {
b.iter(black_box(|| {
let mut buffer = ByteBuffer::new();
for _ in 0..64 {
buffer.write_value(&data);
}
}));
});
}

fn structs(c: &mut Criterion) {
let mut data = [0u8; 2048];
rand::thread_rng().fill_bytes(&mut data);

c.bench_function("encode-audio-frame", |b| {
b.iter(black_box(|| {
let data = FastEncodedAudioFrame {
data: data.to_vec().into(),
};
let mut stack_array = [0u8; 3000 * 8];
let mut buf = FastByteBuffer::new(&mut stack_array);
for _ in 0..8 {
buf.write_value(&data);
}

let written_data = buf.as_bytes();

for _ in 0..128 {
let mut reader = ByteReader::from_bytes(written_data);

let abc = reader.read_value::<FastEncodedAudioFrame>().unwrap();
assert_eq!(abc.data.len(), written_data.len());
}
}));
});
}

fn managers(c: &mut Criterion) {
c.bench_function("player-manager", |b| {
b.iter(black_box(|| {
let mut manager = PlayerManager::new();

for level_id in 0..100 {
for account_id in 0..10 {
manager.add_to_level(level_id, level_id * 10 + account_id);
manager.set_player_data(level_id * 10 + account_id, &PlayerData {});
}
}

let mut total_players = 0;
for i in 0..100 {
let count = manager.get_player_count_on_level(i).unwrap_or(0);
assert_eq!(count, 10);

let total = manager.for_each_player_on_level(
i,
|_, _, p| {
*p += 1;
true
},
&mut total_players,
);

assert_eq!(total, count);
}

assert_eq!(total_players, 1000);

for level_id in 0..100 {
for account_id in 0..10 {
manager.remove_from_level(level_id, level_id * 10 + account_id);
manager.remove_player(level_id * 10 + account_id);
}
}
}));
});
}

criterion_group!(benches, buffers, structs, managers);
// criterion_group!(benches, structs);
criterion_main!(benches);
Loading

0 comments on commit dbc8622

Please sign in to comment.