Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize pool #36

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions fastrace/benches/object_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BatchSize;
use criterion::Criterion;
use fastrace::util::object_pool::Pool;
use fastrace::util::object_pool::GlobalVecPool;

fn bench_alloc_vec(c: &mut Criterion) {
let mut bgroup = c.benchmark_group("Vec::with_capacity");

for cap in &[1, 10, 100, 1000, 10000, 100000] {
let vec_pool: Pool<Vec<usize>> = Pool::new(Vec::new, Vec::clear);
let mut puller = vec_pool.puller(512);
fastrace::util::object_pool::enable_reuse_in_current_thread();
static VEC_POOL: GlobalVecPool<usize> = GlobalVecPool::new();
let mut puller = VEC_POOL.new_local(512);
bgroup.bench_function(format!("object-pool/{}", cap), |b| {
b.iter_batched(
|| (),
|_| {
let mut vec = puller.pull();
let mut vec = puller.take();
if vec.capacity() < *cap {
vec.reserve(*cap);
}
Expand Down
3 changes: 0 additions & 3 deletions fastrace/src/collector/global_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::collector::TraceId;
use crate::local::local_collector::LocalSpansInner;
use crate::local::raw_span::RawKind;
use crate::local::raw_span::RawSpan;
use crate::util::object_pool;
use crate::util::spsc::Receiver;
use crate::util::spsc::Sender;
use crate::util::spsc::{self};
Expand Down Expand Up @@ -246,8 +245,6 @@ impl GlobalCollector {
}

fn handle_commands(&mut self) {
object_pool::enable_reuse_in_current_thread();

debug_assert!(self.start_collects.is_empty());
debug_assert!(self.drop_collects.is_empty());
debug_assert!(self.commit_collects.is_empty());
Expand Down
42 changes: 19 additions & 23 deletions fastrace/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,48 @@ pub mod tree;
use std::borrow::Cow;
use std::cell::RefCell;

use once_cell::sync::Lazy;

use crate::collector::CollectTokenItem;
use crate::local::raw_span::RawSpan;
use crate::util::object_pool::Pool;
use crate::util::object_pool::Puller;
use crate::util::object_pool::Reusable;
use crate::util::object_pool::GlobalVecPool;
use crate::util::object_pool::LocalVecPool;
use crate::util::object_pool::ReusableVec;

static RAW_SPANS_POOL: Lazy<Pool<Vec<RawSpan>>> = Lazy::new(|| Pool::new(Vec::new, Vec::clear));
static COLLECT_TOKEN_ITEMS_POOL: Lazy<Pool<Vec<CollectTokenItem>>> =
Lazy::new(|| Pool::new(Vec::new, Vec::clear));
#[allow(clippy::type_complexity)]
static PROPERTIES_POOL: Lazy<Pool<Vec<(Cow<'static, str>, Cow<'static, str>)>>> =
Lazy::new(|| Pool::new(Vec::new, Vec::clear));
static RAW_SPANS_POOL: GlobalVecPool<RawSpan> = GlobalVecPool::new();
static COLLECT_TOKEN_ITEMS_POOL: GlobalVecPool<CollectTokenItem> = GlobalVecPool::new();
static PROPERTIES_POOL: GlobalVecPool<(Cow<'static, str>, Cow<'static, str>)> =
GlobalVecPool::new();

thread_local! {
static RAW_SPANS_PULLER: RefCell<Puller<'static, Vec<RawSpan>>> = RefCell::new(RAW_SPANS_POOL.puller(512));
static COLLECT_TOKEN_ITEMS_PULLER: RefCell<Puller<'static, Vec<CollectTokenItem>>> = RefCell::new(COLLECT_TOKEN_ITEMS_POOL.puller(512));
static RAW_SPANS_PULLER: RefCell<LocalVecPool<RawSpan>> = RefCell::new(RAW_SPANS_POOL.new_local(512));
static COLLECT_TOKEN_ITEMS_PULLER: RefCell<LocalVecPool<CollectTokenItem>> = RefCell::new(COLLECT_TOKEN_ITEMS_POOL.new_local(512));
#[allow(clippy::type_complexity)]
static PROPERTIES_PULLER: RefCell<Puller<'static, Vec<(Cow<'static, str>, Cow<'static, str>)>>> = RefCell::new(PROPERTIES_POOL.puller(512));
static PROPERTIES_PULLER: RefCell<LocalVecPool<(Cow<'static, str>, Cow<'static, str>)>> = RefCell::new(PROPERTIES_POOL.new_local(512));
}

pub type RawSpans = Reusable<'static, Vec<RawSpan>>;
pub type CollectToken = Reusable<'static, Vec<CollectTokenItem>>;
pub type Properties = Reusable<'static, Vec<(Cow<'static, str>, Cow<'static, str>)>>;
pub type RawSpans = ReusableVec<RawSpan>;
pub type CollectToken = ReusableVec<CollectTokenItem>;
pub type Properties = ReusableVec<(Cow<'static, str>, Cow<'static, str>)>;

impl Default for RawSpans {
fn default() -> Self {
RAW_SPANS_PULLER
.try_with(|puller| puller.borrow_mut().pull())
.unwrap_or_else(|_| Reusable::new(&*RAW_SPANS_POOL, vec![]))
.try_with(|puller| puller.borrow_mut().take())
.unwrap_or_else(|_| Self::new(&RAW_SPANS_POOL, Vec::new()))
}
}

impl Default for Properties {
fn default() -> Self {
PROPERTIES_PULLER
.try_with(|puller| puller.borrow_mut().pull())
.unwrap_or_else(|_| Reusable::new(&*PROPERTIES_POOL, vec![]))
.try_with(|puller| puller.borrow_mut().take())
.unwrap_or_else(|_| Self::new(&PROPERTIES_POOL, Vec::new()))
}
}

fn new_collect_token(items: impl IntoIterator<Item = CollectTokenItem>) -> CollectToken {
let mut token = COLLECT_TOKEN_ITEMS_PULLER
.try_with(|puller| puller.borrow_mut().pull())
.unwrap_or_else(|_| Reusable::new(&*COLLECT_TOKEN_ITEMS_POOL, vec![]));
.try_with(|puller| puller.borrow_mut().take())
.unwrap_or_else(|_| CollectToken::new(&COLLECT_TOKEN_ITEMS_POOL, Vec::new()));
token.extend(items);
token
}
Expand Down
Loading