Skip to content
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
27 changes: 27 additions & 0 deletions cel-c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,33 @@ cc_test(
],
)

cc_library(
name = "string_pool",
srcs = ["string_pool.cc"],
hdrs = ["string_pool.h"],
visibility = ["//visibility:public"],
deps = [
":arena",
":assert",
":config",
":string_view",
"//cel-c/internal:malloc",
"//cel-c/internal:string_pool",
],
)

cc_test(
name = "string_pool_test",
srcs = ["string_pool_test.cc"],
deps = [
":alloc",
":arena",
":string_pool",
":string_view",
"@googletest//:gtest_main",
],
)

cc_library(
name = "string_view",
srcs = [
Expand Down
15 changes: 15 additions & 0 deletions cel-c/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,21 @@ cc_library(
],
)

cc_library(
name = "string_pool",
hdrs = ["string_pool.h"],
deps = [
":flat_hash_set",
"//cel-c:alloc",
"//cel-c:arena",
"//cel-c:assert",
"//cel-c:config",
"//cel-c:string_view",
"//cel-c:string_view_absl",
"@abseil-cpp//absl/hash",
],
)

cc_test(
name = "string_test",
srcs = ["string_test.cc"],
Expand Down
86 changes: 86 additions & 0 deletions cel-c/internal/string_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2025 Google LLC
//
// 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
//
// https://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.

#ifndef THIRD_PARTY_CEL_C_INTERNAL_STRING_POOL_H_
#define THIRD_PARTY_CEL_C_INTERNAL_STRING_POOL_H_

#include <cstddef>
#include <cstring>

#include "absl/hash/hash.h"
#include "cel-c/alloc.h"
#include "cel-c/arena.h"
#include "cel-c/assert.h"
#include "cel-c/config.h"
#include "cel-c/internal/flat_hash_set.h"
#include "cel-c/string_view.h"
#include "cel-c/string_view_absl.h"

class cel_StringPool {
public:
explicit cel_StringPool(cel_Arena* cel_nonnull arena) : arena_(arena) {
_cel_FlatHashSet_Construct(&strings_, &Hasher, &Equaler);
}

~cel_StringPool() {
_cel_FlatHashSet_Destruct(&strings_, cel_DefaultAllocator);
}

cel_StringPool(const cel_StringPool&) = delete;
cel_StringPool(cel_StringPool&&) = delete;
cel_StringPool& operator=(const cel_StringPool&) = delete;
cel_StringPool& operator=(cel_StringPool&&) = delete;

[[nodiscard]]
bool Intern(cel_StringView in, cel_StringView* cel_nonnull out) {
CEL_ASSERT_NOT_NULL(out);
if (cel_StringView_Empty(in)) {
*out = cel_StringView_From("");
return true;
}
_cel_FlatHashSetMutableNode(strings_) ent = nullptr;
if (_cel_FlatHashSet_Insert(&strings_, cel_DefaultAllocator, &in, &ent)) {
const size_t len = cel_StringView_Size(in);
*ent = in;
char* str =
reinterpret_cast<char*>(cel_Arena_Malloc(arena_, len, nullptr));
if (CEL_UNLIKELY(str == nullptr)) {
_cel_FlatHashSet_Erase(&strings_, ent);
return false;
}
memcpy(str, cel_StringView_Data(in), len);
*out = *ent = cel_StringView_FromArray(str, len);
} else {
if (CEL_UNLIKELY(ent == nullptr)) {
return false;
}
*out = *ent;
}
return true;
}

private:
static size_t Hasher(const cel_StringView* str) {
return absl::HashOf(cel_StringView_ToAbsl(*str));
}

static bool Equaler(const cel_StringView* lhs, const cel_StringView* rhs) {
return cel_StringView_Equals(*lhs, *rhs);
}

cel_Arena* cel_nonnull const arena_;
_cel_FlatHashSet(cel_StringView) strings_;
};

#endif // THIRD_PARTY_CEL_C_INTERNAL_STRING_POOL_H_
50 changes: 50 additions & 0 deletions cel-c/string_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2026 Google LLC
//
// 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
//
// https://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.

#include "cel-c/string_pool.h"

#include "cel-c/arena.h"
#include "cel-c/assert.h"
#include "cel-c/config.h"
#include "cel-c/internal/malloc.h"
#include "cel-c/internal/string_pool.h"
#include "cel-c/string_view.h"

extern "C" cel_StringPool* cel_nullable
cel_StringPool_New(cel_Arena* cel_nonnull arena) {
CEL_ASSERT_NOT_NULL(arena);
cel_StringPool* string_pool = reinterpret_cast<cel_StringPool*>(
_cel_Malloc(sizeof(cel_StringPool), nullptr));
if (CEL_LIKELY(string_pool != nullptr)) {
::new (static_cast<void*>(string_pool)) cel_StringPool(arena);
}
return string_pool;
}

extern "C" void cel_StringPool_Delete(
cel_StringPool* cel_nullable string_pool) {
if (string_pool == nullptr) {
return;
}
string_pool->~cel_StringPool();
_cel_FreeSized(string_pool, sizeof(*string_pool));
}

extern "C" bool cel_StringPool_Intern(cel_StringPool* cel_nonnull string_pool,
cel_StringView in,
cel_StringView* cel_nonnull out) {
CEL_ASSERT_NOT_NULL(string_pool);
CEL_ASSERT_NOT_NULL(out);
return string_pool->Intern(in, out);
}
31 changes: 23 additions & 8 deletions cel-c/string_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

#include <stdbool.h> // IWYU pragma: keep

#include "cel-c/alloc.h"
#ifdef __cplusplus
#include <memory>
#endif

#include "cel-c/arena.h"
#include "cel-c/config.h"
#include "cel-c/string_view.h"
Expand All @@ -28,20 +31,32 @@ typedef struct cel_StringPool cel_StringPool;

CEL_ATTRIBUTE_NODISCARD
CEL_ATTRIBUTE_NOTHROW
CEL_EXTERN CEL_NULLABLE(cel_StringPool*)
cel_StringPool_New(CEL_NONNULL(cel_Allocator*) alloc,
CEL_NONNULL(cel_Arena*) arena);
CEL_EXTERN cel_StringPool* cel_nullable
cel_StringPool_New(cel_Arena* cel_nonnull arena);

CEL_ATTRIBUTE_NOTHROW
CEL_EXTERN void cel_StringPool_Delete(CEL_NULLABLE(cel_StringPool*)
string_pool);
CEL_EXTERN void cel_StringPool_Delete(cel_StringPool* cel_nullable string_pool);

CEL_ATTRIBUTE_NODISCARD
CEL_ATTRIBUTE_NOTHROW
CEL_EXTERN bool cel_StringPool_Intern(CEL_NONNULL(cel_StringPool*) string_pool,
CEL_EXTERN bool cel_StringPool_Intern(cel_StringPool* cel_nonnull string_pool,
cel_StringView in,
CEL_NONNULL(cel_StringView*) out);
cel_StringView* cel_nonnull out);

CEL_END_DECLS

#ifdef __cplusplus
struct cel_StringPoolDeleter {
CEL_INLINE void operator()(cel_StringPool* cel_nullable pool) const noexcept {
cel_StringPool_Delete(pool);
}
CEL_INLINE void operator()(
const cel_StringPool* cel_nullable pool) const noexcept {
(*this)(const_cast<cel_StringPool*>(pool));
}
};
using cel_StringPoolPtr =
std::unique_ptr<cel_StringPool, cel_StringPoolDeleter>;
#endif

#endif // THIRD_PARTY_CEL_C_STRING_POOL_H_
59 changes: 59 additions & 0 deletions cel-c/string_pool_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2026 Google LLC
//
// 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
//
// https://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.

#include "cel-c/string_pool.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "cel-c/alloc.h"
#include "cel-c/arena.h"
#include "cel-c/string_view.h"

namespace {

using ::testing::NotNull;

TEST(StringPool, InternEmpty) {
cel_ArenaPtr arena(cel_Arena_New(cel_DefaultAllocator));
ASSERT_THAT(arena, NotNull());
cel_StringPoolPtr string_pool(cel_StringPool_New(arena.get()));
ASSERT_THAT(string_pool, NotNull());
cel_StringView str1 = cel_StringView_From("");
cel_StringView str2 = cel_StringView_From("");
ASSERT_TRUE(cel_StringPool_Intern(string_pool.get(), str1, &str1));
ASSERT_TRUE(cel_StringPool_Intern(string_pool.get(), str2, &str2));
EXPECT_EQ(cel_StringView_Data(str1), cel_StringView_Data(str2));
EXPECT_EQ(cel_StringView_Size(str1), cel_StringView_Size(str2));
EXPECT_TRUE(cel_StringView_Equals(str1, cel_StringView_From("")));
EXPECT_TRUE(cel_StringView_Equals(str2, cel_StringView_From("")));
}

TEST(StringPool, Intern) {
cel_ArenaPtr arena(cel_Arena_New(cel_DefaultAllocator));
ASSERT_THAT(arena, NotNull());
cel_StringPoolPtr string_pool(cel_StringPool_New(arena.get()));
ASSERT_THAT(string_pool, NotNull());
cel_StringView str1 = cel_StringView_From("Hello, World!");
cel_StringView str2 = cel_StringView_From("Hello, World!");
ASSERT_TRUE(cel_StringPool_Intern(string_pool.get(), str1, &str1));
ASSERT_TRUE(cel_StringPool_Intern(string_pool.get(), str2, &str2));
EXPECT_EQ(cel_StringView_Data(str1), cel_StringView_Data(str2));
EXPECT_EQ(cel_StringView_Size(str1), cel_StringView_Size(str2));
EXPECT_TRUE(
cel_StringView_Equals(str1, cel_StringView_From("Hello, World!")));
EXPECT_TRUE(
cel_StringView_Equals(str2, cel_StringView_From("Hello, World!")));
}

} // namespace
Loading