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

feat(mmap_allocator): change the mmap allocator inner allcator from GlobalAllocator to JEAllocator #9981

Merged
merged 1 commit into from
Feb 11, 2023
Merged
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
23 changes: 13 additions & 10 deletions src/common/base/src/mem_allocator/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::mem_allocator::JEAllocator;

/// mmap allocator.
/// This is used for some hash tables.
/// T is a fallback inner allocator for some memory special case.
/// For better performance, we use jemalloc as the inner allocator.
#[derive(Debug, Clone, Copy, Default)]
pub struct MmapAllocator<T> {
allocator: T,
pub struct MmapAllocator {
allocator: JEAllocator,
}

impl<T> MmapAllocator<T> {
pub fn new(allocator: T) -> Self {
Self { allocator }
impl MmapAllocator {
pub fn new() -> Self {
Self {
allocator: JEAllocator::default(),
}
}
}

Expand All @@ -46,7 +49,7 @@ pub mod linux {

const THRESHOLD: usize = 64 << 20;

impl<T: Allocator> MmapAllocator<T> {
impl MmapAllocator {
#[inline(always)]
fn mmap_alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(layout.align() <= page_size());
Expand Down Expand Up @@ -128,7 +131,7 @@ pub mod linux {
}
}

unsafe impl<T: Allocator> Allocator for MmapAllocator<T> {
unsafe impl Allocator for MmapAllocator {
#[inline(always)]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
if layout.align() > page_size() {
Expand Down Expand Up @@ -310,7 +313,7 @@ pub mod not_linux {

use super::MmapAllocator;

unsafe impl<T: Allocator> Allocator for MmapAllocator<T> {
unsafe impl Allocator for MmapAllocator {
#[inline(always)]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.allocator.allocate(layout)
Expand Down
3 changes: 1 addition & 2 deletions src/common/hashtable/src/hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::alloc::Allocator;
use std::intrinsics::unlikely;
use std::mem::MaybeUninit;

use common_base::mem_allocator::GlobalAllocator;
use common_base::mem_allocator::MmapAllocator;

use super::container::HeapContainer;
Expand All @@ -29,7 +28,7 @@ use super::traits::Keyable;
use super::utils::ZeroEntry;
use crate::FastHash;

pub struct Hashtable<K, V, A = MmapAllocator<GlobalAllocator>>
pub struct Hashtable<K, V, A = MmapAllocator>
where
K: Keyable,
A: Allocator + Clone,
Expand Down
9 changes: 2 additions & 7 deletions src/common/hashtable/src/lookup_hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ use std::alloc::Allocator;
use std::mem;
use std::mem::MaybeUninit;

use common_base::mem_allocator::GlobalAllocator;
use common_base::mem_allocator::MmapAllocator;

use crate::table0::Entry;
use crate::HashtableLike;

pub struct LookupHashtable<
K: Sized,
const CAPACITY: usize,
V,
A: Allocator + Clone = MmapAllocator<GlobalAllocator>,
> {
pub struct LookupHashtable<K: Sized, const CAPACITY: usize, V, A: Allocator + Clone = MmapAllocator>
{
flags: Box<[bool; CAPACITY], A>,
data: Box<[Entry<K, V>; CAPACITY], A>,
len: usize,
Expand Down
3 changes: 1 addition & 2 deletions src/common/hashtable/src/short_string_hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::num::NonZeroU64;
use std::ptr::NonNull;

use bumpalo::Bump;
use common_base::mem_allocator::GlobalAllocator;
use common_base::mem_allocator::MmapAllocator;

use super::container::HeapContainer;
Expand All @@ -38,7 +37,7 @@ use crate::table_empty::TableEmpty;
use crate::table_empty::TableEmptyIter;
use crate::table_empty::TableEmptyIterMut;

pub struct ShortStringHashtable<K, V, A = MmapAllocator<GlobalAllocator>>
pub struct ShortStringHashtable<K, V, A = MmapAllocator>
where
K: UnsizedKeyable + ?Sized,
A: Allocator + Clone,
Expand Down
3 changes: 1 addition & 2 deletions src/common/hashtable/src/stack_hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::alloc::Allocator;
use std::intrinsics::unlikely;
use std::mem::MaybeUninit;

use common_base::mem_allocator::GlobalAllocator;
use common_base::mem_allocator::MmapAllocator;

use super::container::StackContainer;
Expand All @@ -27,7 +26,7 @@ use super::table0::Table0IterMut;
use super::traits::Keyable;
use super::utils::ZeroEntry;

pub struct StackHashtable<K, V, const N: usize = 16, A = MmapAllocator<GlobalAllocator>>
pub struct StackHashtable<K, V, const N: usize = 16, A = MmapAllocator>
where
K: Keyable,
A: Allocator + Clone,
Expand Down
3 changes: 1 addition & 2 deletions src/common/hashtable/src/string_hashtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::marker::PhantomData;
use std::mem::MaybeUninit;

use bumpalo::Bump;
use common_base::mem_allocator::GlobalAllocator;
use common_base::mem_allocator::MmapAllocator;

use super::container::HeapContainer;
Expand All @@ -37,7 +36,7 @@ use crate::table_empty::TableEmptyIterMut;
/// Simple unsized hashtable is used for storing unsized keys in arena. It can be worked with HashMethodSerializer.
/// Different from `ShortStringHashTable`, it doesn't use adpative sub hashtable to store key values via key size.
/// It can be considered as a minimal hashtable implementation of ShortStringHashTable
pub struct StringHashtable<K, V, A = MmapAllocator<GlobalAllocator>>
pub struct StringHashtable<K, V, A = MmapAllocator>
where
K: UnsizedKeyable + ?Sized,
A: Allocator + Clone,
Expand Down