-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7971 from usamoi/main
refactor(hashtable): adaptive string hash table
- Loading branch information
Showing
58 changed files
with
5,007 additions
and
2,602 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::alloc::AllocError; | ||
use std::alloc::Allocator; | ||
use std::alloc::GlobalAlloc; | ||
use std::alloc::Layout; | ||
use std::ptr::null_mut; | ||
use std::ptr::NonNull; | ||
|
||
use super::je_allocator::JEAllocator; | ||
use super::system_allocator::SystemAllocator; | ||
|
||
#[global_allocator] | ||
pub static GLOBAL_ALLOCATOR: GlobalAllocator = GlobalAllocator; | ||
|
||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct GlobalAllocator; | ||
|
||
type Fallback = JEAllocator<SystemAllocator>; | ||
|
||
unsafe impl Allocator for GlobalAllocator { | ||
#[inline(always)] | ||
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
Fallback::new(Default::default()).allocate(layout) | ||
} | ||
|
||
#[inline(always)] | ||
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { | ||
Fallback::new(Default::default()).deallocate(ptr, layout) | ||
} | ||
|
||
#[inline(always)] | ||
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
Fallback::new(Default::default()).allocate_zeroed(layout) | ||
} | ||
|
||
#[inline(always)] | ||
unsafe fn grow( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
Fallback::new(Default::default()).grow(ptr, old_layout, new_layout) | ||
} | ||
|
||
#[inline(always)] | ||
unsafe fn grow_zeroed( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
Fallback::new(Default::default()).grow_zeroed(ptr, old_layout, new_layout) | ||
} | ||
|
||
#[inline(always)] | ||
unsafe fn shrink( | ||
&self, | ||
ptr: NonNull<u8>, | ||
old_layout: Layout, | ||
new_layout: Layout, | ||
) -> Result<NonNull<[u8]>, AllocError> { | ||
Fallback::new(Default::default()).shrink(ptr, old_layout, new_layout) | ||
} | ||
} | ||
|
||
unsafe impl GlobalAlloc for GlobalAllocator { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if let Ok(ptr) = GlobalAllocator.allocate(layout) { | ||
ptr.as_ptr() as *mut u8 | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
let ptr = NonNull::new(ptr).unwrap_unchecked(); | ||
GlobalAllocator.deallocate(ptr, layout); | ||
} | ||
|
||
#[inline] | ||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
if let Ok(ptr) = GlobalAllocator.allocate_zeroed(layout) { | ||
ptr.as_ptr() as *mut u8 | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
use std::cmp::Ordering::*; | ||
let ptr = NonNull::new(ptr).unwrap_unchecked(); | ||
let new_layout = Layout::from_size_align(new_size, layout.align()).unwrap(); | ||
match layout.size().cmp(&new_size) { | ||
Less => { | ||
if let Ok(ptr) = GlobalAllocator.grow(ptr, layout, new_layout) { | ||
ptr.as_ptr() as *mut u8 | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
Greater => { | ||
if let Ok(ptr) = GlobalAllocator.shrink(ptr, layout, new_layout) { | ||
ptr.as_ptr() as *mut u8 | ||
} else { | ||
null_mut() | ||
} | ||
} | ||
Equal => ptr.as_ptr() as *mut u8, | ||
} | ||
} | ||
} |
Oops, something went wrong.
ac00014
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
databend – ./
databend.vercel.app
databend-git-main-databend.vercel.app
databend.rs
databend-databend.vercel.app