mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-11-16 07:47:46 +00:00
config: unsafe { .get_unchecked() }
-> get_or_init()
only 0.13% faster over 1 billion accesses, not worth it
This commit is contained in:
parent
2853e8ed5f
commit
f2f4fe7157
1 changed files with 22 additions and 17 deletions
|
@ -5,23 +5,26 @@ use tracing::{error,info,warn,debug,trace};
|
|||
use disk::Toml;
|
||||
use crate::constants::{
|
||||
CUPRATE,
|
||||
CUPRATE_PORT,
|
||||
CUPRATE_CONFIG,
|
||||
DEFAULT_RPC_PORT,
|
||||
DEFAULT_CONFIG,
|
||||
};
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
collections::BTreeSet,
|
||||
path::PathBuf,
|
||||
sync::OnceLock,
|
||||
};
|
||||
use std::net::IpAddr;
|
||||
use std::collections::BTreeSet;
|
||||
use std::path::PathBuf;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Statics
|
||||
static CONFIG: OnceCell<Config> = OnceCell::new();
|
||||
static CONFIG_CELL: OnceLock<Config> = OnceLock::new();
|
||||
#[inline(always)]
|
||||
#[allow(non_snake_case)]
|
||||
/// Acquire our runtime configuration.
|
||||
pub fn config() -> &'static Config {
|
||||
// SAFETY:
|
||||
pub fn CONFIG() -> &'static Config {
|
||||
// INVARIANT:
|
||||
// This should always get initialized in the
|
||||
// `.builder()` below, by `main()` during init.
|
||||
unsafe { CONFIG.get_unchecked() }
|
||||
CONFIG_CELL.get_or_init(|| unreachable!("CONFIG was not initialized at main()"))
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Constants
|
||||
|
@ -95,16 +98,20 @@ impl Default for ConfigBuilder {
|
|||
}
|
||||
|
||||
impl ConfigBuilder {
|
||||
/// Initialize the global `CONFIG` that lives for the rest of the program.
|
||||
///
|
||||
/// ## Panics
|
||||
/// This panics if this function is called more than once.
|
||||
pub fn init(
|
||||
log_level: Option<tracing::Level>,
|
||||
cli: Option<Self>,
|
||||
) -> &'static Config {
|
||||
todo!()
|
||||
// SAFETY: we only set `CONFIG` here.
|
||||
CONFIG_CELL.set(todo!() /* Self::init_inner() */).unwrap();
|
||||
CONFIG()
|
||||
}
|
||||
|
||||
// INVARIANT: must be called once and only once.
|
||||
// Sets `CONFIG`, and returns a ref.
|
||||
pub fn build_and_set(self) -> &'static Config {
|
||||
fn init_inner(self) -> Config {
|
||||
let ConfigBuilder {
|
||||
ip,
|
||||
port,
|
||||
|
@ -281,9 +288,7 @@ impl ConfigBuilder {
|
|||
info!("Authorization: {}", AUTH.get().is_some());
|
||||
info!("{DASH} Configuration");
|
||||
|
||||
// SAFETY: unwrap is okay, we only set `CONFIG` here.
|
||||
CONFIG.set(c).unwrap();
|
||||
config()
|
||||
c
|
||||
}
|
||||
|
||||
// Read from disk, or create a default.
|
||||
|
|
Loading…
Reference in a new issue