Skip to content

Commit

Permalink
Merge branch 'rewrite/v3' into feature/keep_alive_system
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonymousBit0111 authored Nov 1, 2024
2 parents ed01a60 + b28fa21 commit e79acde
Show file tree
Hide file tree
Showing 25 changed files with 806 additions and 266 deletions.
Binary file added .etc/r.0.0.mca
Binary file not shown.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ you talked with and what consensus was reached. Unexplained PRs will rarely be a
## Project specific guidelines
Just some rules to try to keep the repo nice and organised
### Branches
#### `dev`
#### `rewrite/v3`
This branch is the main branch. This is where all PRs should be made to. This branch is the most up to
date and should only be merged into with completed features.
#### `feature/feature-name`
Expand Down Expand Up @@ -104,4 +104,4 @@ Please note we have a code of conduct, please follow it in all your interactions

By contributing, you agree that your contributions will be licensed under the project's license.

### [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md)
### [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md)
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ members = [
"src/lib/utils", "src/lib/utils/logging", "src/lib/utils/profiling", "src/lib/utils/general_purpose",
"src/lib/world",
"src/lib/derive_macros",
"src/lib/adapters/nbt",
"src/lib/adapters/mca",
"src/tests",
"src/lib/adapters/nbt", "src/lib/adapters/mca",
"src/tests", "src/lib/adapters/anvil",
]

#================== Lints ==================#
Expand Down Expand Up @@ -142,10 +141,13 @@ ctor = "0.2.8"
libflate = "2.1.0"
flate2 = { version = "1.0.33", features = ["zlib"], default-features = false }
zstd = { version = "0.13.2" }
brotli = "6.0.0"
brotli = "7.0.0"
lzzzz = "1.1.0"
yazi = "0.2.0"

# I/O
tempfile = "3.12.0"
memmap2 = "0.9.5"

# Benchmarking
criterion = { version = "0.5.1", features = ["html_reports"] }
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ To view the roadmap, see [plans.md](assets/plans/plans.md)
> [!IMPORTANT]
> Use pull requests instead of direct pushes.
## Contributing
**Want to contribute to FerrumC?**\
Make sure to check out [CONTRIBUTING.md](CONTRIBUTING.md).\
We would highly recommend you join our [Discord](https://discord.gg/FqT5J8EMjwk).

## 📥 Installation/ 🖥️ Usage
### Use docker

Expand Down
5 changes: 3 additions & 2 deletions src/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ async fn entry() -> Result<()> {
let state = create_state().await?;
let global_state = Arc::new(state);

let all_systems = tokio::spawn(definition::start_all_systems(Arc::clone(&global_state)));

let all_system_handles = tokio::spawn(definition::start_all_systems(global_state.clone()));

// Start the systems and wait until all of them are done
all_systems.await??;
all_system_handles.await??;

// Stop all systems
definition::stop_all_systems(global_state).await?;
Expand Down
25 changes: 13 additions & 12 deletions src/bin/src/systems/definition.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::sync::Arc;
use ferrumc_net::{GlobalState, NetResult};
use futures::stream::FuturesUnordered;
use tracing::{debug, debug_span, info, Instrument};
use async_trait::async_trait;
use crate::systems::keep_alive_system::KeepAliveSystem;
use crate::systems::tcp_listener_system::TcpListenerSystem;
use crate::systems::ticking_system::TickingSystem;
use async_trait::async_trait;
use ferrumc_net::{GlobalState, NetResult};
use futures::stream::FuturesUnordered;
use std::sync::{Arc, LazyLock};
use tracing::{debug, debug_span, info, Instrument};

#[async_trait]
pub trait System: Send + Sync {
Expand All @@ -15,7 +15,9 @@ pub trait System: Send + Sync {
fn name(&self) -> &'static str;
}


static SYSTEMS: LazyLock<Vec<Arc<dyn System>>> = LazyLock::new(|| {
create_systems()
});
pub fn create_systems() -> Vec<Arc<dyn System>> {
vec![
Arc::new(TcpListenerSystem),
Expand All @@ -24,14 +26,14 @@ pub fn create_systems() -> Vec<Arc<dyn System>> {
]
}
pub async fn start_all_systems(state: GlobalState) -> NetResult<()> {
let systems = create_systems();
let handles = FuturesUnordered::new();

for system in systems {
for system in SYSTEMS.iter() {
let name = system.name();

let handle = tokio::spawn(
system
.clone()
.start(state.clone())
.instrument(debug_span!("sys", %name)),
);
Expand All @@ -44,13 +46,12 @@ pub async fn start_all_systems(state: GlobalState) -> NetResult<()> {
}

pub async fn stop_all_systems(state: GlobalState) -> NetResult<()> {
let systems = create_systems();
info!("Stopping all systems...");

for system in systems {
for system in SYSTEMS.iter() {
debug!("Stopping system: {}", system.name());
system.stop(state.clone()).await;
system.clone().stop(state.clone()).await;
}

Ok(())
}
}
27 changes: 27 additions & 0 deletions src/lib/adapters/anvil/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "anvil"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = { workspace = true }
memmap2 = { workspace = true}
ferrumc-utils = { workspace = true}
flate2 = { workspace = true}
yazi = { workspace = true}
lzzzz = { workspace = true}
tracing = { workspace = true}
rayon = { workspace = true}

[dev-dependencies]
fastanvil = "0.31.0"
criterion = { workspace = true }
ferrumc-logging = { workspace = true }

[lints]
workspace = true

[[bench]]
name = "anvil"
path = "benches/anvil.rs"
harness = false
72 changes: 72 additions & 0 deletions src/lib/adapters/anvil/benches/anvil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::fs::File;
use std::path::PathBuf;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use anvil::load_anvil_file;
use ferrumc_utils::root;
use rayon::prelude::*;
use fastanvil::Region;

fn criterion_benchmark(c: &mut Criterion) {
let mut read_all_group = c.benchmark_group("Read All");

read_all_group.bench_function("FerrumC Rayon", |b| {
b.iter(|| {
let file_path = PathBuf::from(root!(".etc/r.0.0.mca"));
let loaded_file = load_anvil_file(file_path).unwrap();
let locations = loaded_file.get_locations();
locations.chunks(96).par_bridge().for_each(|chunk| {
chunk.iter().for_each(|location| {
black_box(loaded_file.get_chunk_from_location(*location));
});
});
});
});

read_all_group.bench_function("FerrumC", |b| {
b.iter(|| {
let file_path = PathBuf::from(root!(".etc/r.0.0.mca"));
let loaded_file = load_anvil_file(file_path).unwrap();
let locations = loaded_file.get_locations();
locations.iter().for_each(|location| {
black_box(loaded_file.get_chunk_from_location(*location));
});
});
});

read_all_group.bench_function("FastAnvil", |b| {
b.iter(|| {
let file = File::open(root!(".etc/r.0.0.mca")).unwrap();
let mut region = Region::from_stream(file).unwrap();
region.iter().for_each(|chunk| {
black_box(chunk.unwrap().data);
});
});
});

read_all_group.finish();

let mut read_one_group = c.benchmark_group("Read One");

read_one_group.bench_function("FerrumC", |b| {
b.iter(|| {
let file_path = PathBuf::from(root!(".etc/r.0.0.mca"));
let loaded_file = load_anvil_file(file_path).unwrap();
black_box(loaded_file.get_chunk(0, 0));
});
});

read_one_group.bench_function("FastAnvil", |b| {
b.iter(|| {
let file = File::open(root!(".etc/r.0.0.mca")).unwrap();
let mut region = Region::from_stream(file).unwrap();
black_box(region.read_chunk(0, 0).unwrap());
});
});

read_one_group.finish();
}



criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
14 changes: 14 additions & 0 deletions src/lib/adapters/anvil/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::path::PathBuf;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum AnvilError {
#[error("File not found: {0}")]
FileNotFound(PathBuf),
#[error("Invalid tables: {0}")]
InvalidTables(PathBuf),
#[error("Unable to read file {0}: {1}")]
UnableToReadFile(PathBuf, std::io::Error),
#[error("Unable to map file {0}: {1}")]
UnableToMapFile(PathBuf, std::io::Error),
}
Loading

0 comments on commit e79acde

Please sign in to comment.