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

Reimplement ClearTable using the stored TypeInfo. #19349

Merged
merged 1 commit into from
Nov 22, 2024
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
32 changes: 4 additions & 28 deletions rust/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,10 +1017,6 @@ where

fn to_view<'a>(key: Self::FfiKey) -> View<'a, Self>;

unsafe fn free(m: RawMap, prototype: MapValue);

unsafe fn clear(m: RawMap, prototype: MapValue);

unsafe fn insert(m: RawMap, key: View<'_, Self>, value: MapValue) -> bool;

unsafe fn get(
Expand Down Expand Up @@ -1052,16 +1048,6 @@ macro_rules! generate_map_key_impl {
$from_ffi(key)
}

#[inline]
unsafe fn free(m: RawMap, prototype: MapValue) {
unsafe { [< proto2_rust_map_free_ $key >](m, prototype) }
}

#[inline]
unsafe fn clear(m: RawMap, prototype: MapValue) {
unsafe { [< proto2_rust_map_clear_ $key >](m, prototype) }
}

#[inline]
unsafe fn insert(
m: RawMap,
Expand Down Expand Up @@ -1126,13 +1112,13 @@ where

unsafe fn map_free(_private: Private, map: &mut Map<Key, Self>) {
unsafe {
Key::free(map.as_raw(Private), Self::get_prototype());
proto2_rust_map_free(map.as_raw(Private));
}
}

fn map_clear(mut map: MapMut<Key, Self>) {
unsafe {
Key::clear(map.as_raw(Private), Self::get_prototype());
proto2_rust_map_clear(map.as_raw(Private));
}
}

Expand Down Expand Up @@ -1193,23 +1179,13 @@ where

macro_rules! impl_map_primitives {
(@impl $(($rust_type:ty, $cpp_type:ty) => [
$free_thunk:ident,
$clear_thunk:ident,
$insert_thunk:ident,
$get_thunk:ident,
$iter_get_thunk:ident,
$remove_thunk:ident,
]),* $(,)?) => {
$(
extern "C" {
pub fn $free_thunk(
m: RawMap,
prototype: MapValue,
);
pub fn $clear_thunk(
m: RawMap,
prototype: MapValue,
);
pub fn $insert_thunk(
m: RawMap,
key: $cpp_type,
Expand All @@ -1235,8 +1211,6 @@ macro_rules! impl_map_primitives {
paste!{
impl_map_primitives!(@impl $(
($rust_type, $cpp_type) => [
[< proto2_rust_map_free_ $rust_type >],
[< proto2_rust_map_clear_ $rust_type >],
[< proto2_rust_map_insert_ $rust_type >],
[< proto2_rust_map_get_ $rust_type >],
[< proto2_rust_map_iter_get_ $rust_type >],
Expand All @@ -1260,6 +1234,8 @@ extern "C" {
fn proto2_rust_thunk_UntypedMapIterator_increment(iter: &mut UntypedMapIterator);

pub fn proto2_rust_map_new(key_prototype: MapValue, value_prototype: MapValue) -> RawMap;
pub fn proto2_rust_map_free(m: RawMap);
pub fn proto2_rust_map_clear(m: RawMap);
pub fn proto2_rust_map_size(m: RawMap) -> usize;
pub fn proto2_rust_map_iter(m: RawMap) -> UntypedMapIterator;
}
Expand Down
85 changes: 33 additions & 52 deletions rust/cpp_kernel/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,6 @@ size_t KeySize() {
}
}

template <typename Key>
void ClearMap(internal::UntypedMapBase* m, bool reset_table,
MapValue prototype) {
internal::MapNodeSizeInfoT size_info = GetSizeInfo(KeySize<Key>(), prototype);
if (internal::RustMapHelper::IsGlobalEmptyTable(m)) return;
uint8_t bits = 0;
if constexpr (std::is_same<Key, google::protobuf::rust::PtrAndLen>::value) {
bits |= internal::RustMapHelper::kKeyIsString;
}
if (prototype.tag == MapValueTag::kString) {
bits |= internal::RustMapHelper::kValueIsString;
} else if (prototype.tag == MapValueTag::kMessage) {
bits |= internal::RustMapHelper::kValueIsProto;
}
internal::RustMapHelper::ClearTable(
m, internal::RustMapHelper::ClearInput{size_info, bits, reset_table,
/* destroy_node = */ nullptr});
}

} // namespace
} // namespace rust
} // namespace protobuf
Expand Down Expand Up @@ -331,39 +312,39 @@ google::protobuf::internal::UntypedMapIterator proto2_rust_map_iter(
return m->begin();
}

#define DEFINE_KEY_SPECIFIC_MAP_OPERATIONS(cpp_type, suffix) \
void proto2_rust_map_free_##suffix(google::protobuf::internal::UntypedMapBase* m, \
google::protobuf::rust::MapValue prototype) { \
google::protobuf::rust::ClearMap<cpp_type>(m, /* reset_table = */ false, prototype); \
delete m; \
} \
void proto2_rust_map_clear_##suffix(google::protobuf::internal::UntypedMapBase* m, \
google::protobuf::rust::MapValue prototype) { \
google::protobuf::rust::ClearMap<cpp_type>(m, /* reset_table = */ true, prototype); \
} \
bool proto2_rust_map_insert_##suffix(google::protobuf::internal::UntypedMapBase* m, \
cpp_type key, \
google::protobuf::rust::MapValue value) { \
return google::protobuf::rust::Insert(m, key, value); \
} \
\
bool proto2_rust_map_get_##suffix( \
google::protobuf::internal::UntypedMapBase* m, google::protobuf::rust::MapValue prototype, \
cpp_type key, google::protobuf::rust::MapValue* value) { \
return google::protobuf::rust::Get(m, prototype, key, value); \
} \
\
bool proto2_rust_map_remove_##suffix(google::protobuf::internal::UntypedMapBase* m, \
google::protobuf::rust::MapValue prototype, \
cpp_type key) { \
return google::protobuf::rust::Remove(m, prototype, key); \
} \
\
void proto2_rust_map_iter_get_##suffix( \
const google::protobuf::internal::UntypedMapIterator* iter, \
google::protobuf::rust::MapValue prototype, cpp_type* key, \
google::protobuf::rust::MapValue* value) { \
return google::protobuf::rust::IterGet(iter, prototype, key, value); \
void proto2_rust_map_free(google::protobuf::internal::UntypedMapBase* m) {
m->ClearTable(false, nullptr);
delete m;
}

void proto2_rust_map_clear(google::protobuf::internal::UntypedMapBase* m) {
m->ClearTable(true, nullptr);
}

#define DEFINE_KEY_SPECIFIC_MAP_OPERATIONS(cpp_type, suffix) \
bool proto2_rust_map_insert_##suffix(google::protobuf::internal::UntypedMapBase* m, \
cpp_type key, \
google::protobuf::rust::MapValue value) { \
return google::protobuf::rust::Insert(m, key, value); \
} \
\
bool proto2_rust_map_get_##suffix( \
google::protobuf::internal::UntypedMapBase* m, google::protobuf::rust::MapValue prototype, \
cpp_type key, google::protobuf::rust::MapValue* value) { \
return google::protobuf::rust::Get(m, prototype, key, value); \
} \
\
bool proto2_rust_map_remove_##suffix(google::protobuf::internal::UntypedMapBase* m, \
google::protobuf::rust::MapValue prototype, \
cpp_type key) { \
return google::protobuf::rust::Remove(m, prototype, key); \
} \
\
void proto2_rust_map_iter_get_##suffix( \
const google::protobuf::internal::UntypedMapIterator* iter, \
google::protobuf::rust::MapValue prototype, cpp_type* key, \
google::protobuf::rust::MapValue* value) { \
return google::protobuf::rust::IterGet(iter, prototype, key, value); \
}

DEFINE_KEY_SPECIFIC_MAP_OPERATIONS(int32_t, i32)
Expand Down
68 changes: 29 additions & 39 deletions src/google/protobuf/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace internal {

NodeBase* const kGlobalEmptyTable[kGlobalEmptyTableSize] = {};

void UntypedMapBase::ClearTable(const ClearInput input) {
void UntypedMapBase::ClearTableImpl(bool reset, void (*destroy)(NodeBase*)) {
ABSL_DCHECK_NE(num_buckets_, kGlobalEmptyTableSize);

if (alloc_.arena() == nullptr) {
Expand All @@ -41,53 +41,43 @@ void UntypedMapBase::ClearTable(const ClearInput input) {
NodeBase* next = node->next;
absl::PrefetchToLocalCacheNta(next);
destroy_node(node);
SizedDelete(node, SizeFromInfo(input.size_info));
SizedDelete(node, type_info_.node_size);
node = next;
}
}
};
switch (input.destroy_bits) {
case 0:
loop([](NodeBase*) {});
break;
case kKeyIsString:
loop([](NodeBase* node) {
static_cast<std::string*>(node->GetVoidKey())->~basic_string();
});
break;
case kValueIsString:
loop([size_info = input.size_info](NodeBase* node) {
static_cast<std::string*>(node->GetVoidValue(size_info))
->~basic_string();
});
break;
case kKeyIsString | kValueIsString:
loop([size_info = input.size_info](NodeBase* node) {
static_cast<std::string*>(node->GetVoidKey())->~basic_string();
static_cast<std::string*>(node->GetVoidValue(size_info))
->~basic_string();
});
break;
case kValueIsProto:
loop([size_info = input.size_info](NodeBase* node) {
static_cast<MessageLite*>(node->GetVoidValue(size_info))
->DestroyInstance();
});
break;
case kKeyIsString | kValueIsProto:
loop([size_info = input.size_info](NodeBase* node) {

const auto dispatch_key = [&](auto value_handler) {
if (type_info_.key_type < TypeKind::kString) {
return loop(value_handler);
} else if (type_info_.key_type == TypeKind::kString) {
return loop([=](NodeBase* node) {
static_cast<std::string*>(node->GetVoidKey())->~basic_string();
static_cast<MessageLite*>(node->GetVoidValue(size_info))
->DestroyInstance();
value_handler(node);
});
break;
case kUseDestructFunc:
loop(input.destroy_node);
break;
} else {
ABSL_CHECK(destroy != nullptr);
return loop(destroy);
}
};

if (type_info_.value_type < TypeKind::kString) {
dispatch_key([](NodeBase*) {});
} else if (type_info_.value_type == TypeKind::kString) {
dispatch_key([&](NodeBase* node) {
GetValue<std::string>(node)->~basic_string();
});
} else if (type_info_.value_type == TypeKind::kMessage) {
dispatch_key([&](NodeBase* node) {
GetValue<MessageLite>(node)->DestroyInstance();
});
} else {
ABSL_CHECK(destroy != nullptr);
loop(destroy);
}
}

if (input.reset_table) {
if (reset) {
std::fill(table_, table_ + num_buckets_, nullptr);
num_elements_ = 0;
index_of_first_non_null_ = num_buckets_;
Expand Down
Loading
Loading