state-res: Get rid of static mut

This commit is contained in:
Jonas Platte 2021-05-14 01:40:23 +02:00
parent fe68bf0e07
commit 6c167fca38
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 13 additions and 22 deletions

View File

@ -7,7 +7,10 @@
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
sync::Arc, sync::{
atomic::{AtomicU64, Ordering::SeqCst},
Arc,
},
}; };
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
@ -27,7 +30,7 @@ use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId};
use ruma_state_res::{Error, Event, EventMap, Result, StateMap, StateResolution}; use ruma_state_res::{Error, Event, EventMap, Result, StateMap, StateResolution};
use serde_json::{json, Value as JsonValue}; use serde_json::{json, Value as JsonValue};
static mut SERVER_TIMESTAMP: u64 = 0; static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0);
fn lexico_topo_sort(c: &mut Criterion) { fn lexico_topo_sort(c: &mut Criterion) {
c.bench_function("lexicographical topological sort", |b| { c.bench_function("lexicographical topological sort", |b| {
@ -360,12 +363,7 @@ pub fn to_pdu_event<S>(
where where
S: AsRef<str>, S: AsRef<str>,
{ {
let ts = unsafe { let ts = SERVER_TIMESTAMP.fetch_add(1, SeqCst);
let ts = SERVER_TIMESTAMP;
// increment the "origin_server_ts" value
SERVER_TIMESTAMP += 1;
ts
};
let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) }; let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) };
let auth_events = auth_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>(); let auth_events = auth_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>();
let prev_events = prev_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>(); let prev_events = prev_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>();

View File

@ -3,7 +3,10 @@
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
sync::{Arc, Once}, sync::{
atomic::{AtomicU64, Ordering::SeqCst},
Arc, Once,
},
}; };
use js_int::uint; use js_int::uint;
@ -27,7 +30,7 @@ pub use event::StateEvent;
pub static LOGGER: Once = Once::new(); pub static LOGGER: Once = Once::new();
static mut SERVER_TIMESTAMP: u64 = 0; static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0);
pub fn do_check( pub fn do_check(
events: &[Arc<StateEvent>], events: &[Arc<StateEvent>],
@ -326,12 +329,7 @@ pub fn to_init_pdu_event(
state_key: Option<&str>, state_key: Option<&str>,
content: JsonValue, content: JsonValue,
) -> Arc<StateEvent> { ) -> Arc<StateEvent> {
let ts = unsafe { let ts = SERVER_TIMESTAMP.fetch_add(1, SeqCst);
let ts = SERVER_TIMESTAMP;
// increment the "origin_server_ts" value
SERVER_TIMESTAMP += 1;
ts
};
let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) }; let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) };
let state_key = state_key.map(ToString::to_string); let state_key = state_key.map(ToString::to_string);
@ -369,12 +367,7 @@ pub fn to_pdu_event<S>(
where where
S: AsRef<str>, S: AsRef<str>,
{ {
let ts = unsafe { let ts = SERVER_TIMESTAMP.fetch_add(1, SeqCst);
let ts = SERVER_TIMESTAMP;
// increment the "origin_server_ts" value
SERVER_TIMESTAMP += 1;
ts
};
let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) }; let id = if id.contains('$') { id.to_string() } else { format!("${}:foo", id) };
let auth_events = auth_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>(); let auth_events = auth_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>();
let prev_events = prev_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>(); let prev_events = prev_events.iter().map(AsRef::as_ref).map(event_id).collect::<Vec<_>>();