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
6 changes: 6 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -545,17 +545,22 @@ cc_library(
":memory",
":native_type",
":type_kind",
"//internal:string_pool",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:die_if_null",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_absl//absl/types:variant",
Expand All @@ -577,6 +582,7 @@ cc_test(
":type",
":type_kind",
"//internal:testing",
"//internal:testing_descriptor_pool",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/hash:hash_testing",
Expand Down
1 change: 0 additions & 1 deletion common/types/function_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class FunctionType;

namespace common_internal {
struct FunctionTypeData;
class FunctionTypePool;
} // namespace common_internal

class FunctionType final {
Expand Down
29 changes: 29 additions & 0 deletions common/types/function_type_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 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 "common/types/function_type_pool.h"

#include "absl/types/span.h"
#include "common/type.h"

namespace cel::common_internal {

FunctionType FunctionTypePool::InternFunctionType(const Type& result,
absl::Span<const Type> args) {
return *function_types_.lazy_emplace(
AsTuple(result, args),
[&](const auto& ctor) { ctor(FunctionType(arena_, result, args)); });
}

} // namespace cel::common_internal
102 changes: 102 additions & 0 deletions common/types/function_type_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2024 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.

// IWYU pragma: private

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_FUNCTION_TYPE_POOL_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_FUNCTION_TYPE_POOL_H_

#include <cstddef>
#include <cstring>
#include <functional>
#include <tuple>

#include "absl/algorithm/container.h"
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_set.h"
#include "absl/hash/hash.h"
#include "absl/log/die_if_null.h"
#include "absl/types/span.h"
#include "common/type.h"
#include "google/protobuf/arena.h"

namespace cel::common_internal {

// `FunctionTypePool` is a thread unsafe interning factory for `FunctionType`.
class FunctionTypePool final {
public:
explicit FunctionTypePool(absl::Nonnull<google::protobuf::Arena*> arena)
: arena_(ABSL_DIE_IF_NULL(arena)) {} // Crash OK

// Returns a `FunctionType` which has the provided parameters, interning as
// necessary.
FunctionType InternFunctionType(const Type& result,
absl::Span<const Type> args);

private:
using FunctionTypeTuple =
std::tuple<std::reference_wrapper<const Type>, absl::Span<const Type>>;

static FunctionTypeTuple AsTuple(const FunctionType& function_type) {
return AsTuple(function_type.result(), function_type.args());
}

static FunctionTypeTuple AsTuple(const Type& result,
absl::Span<const Type> args) {
return FunctionTypeTuple{std::cref(result), args};
}

struct Hasher {
using is_transparent = void;

size_t operator()(const FunctionType& data) const {
return (*this)(AsTuple(data));
}

size_t operator()(const FunctionTypeTuple& tuple) const {
return absl::Hash<FunctionTypeTuple>{}(tuple);
}
};

struct Equaler {
using is_transparent = void;

bool operator()(const FunctionType& lhs, const FunctionType& rhs) const {
return (*this)(AsTuple(lhs), AsTuple(rhs));
}

bool operator()(const FunctionType& lhs,
const FunctionTypeTuple& rhs) const {
return (*this)(AsTuple(lhs), rhs);
}

bool operator()(const FunctionTypeTuple& lhs,
const FunctionType& rhs) const {
return (*this)(lhs, AsTuple(rhs));
}

bool operator()(const FunctionTypeTuple& lhs,
const FunctionTypeTuple& rhs) const {
return std::get<0>(lhs) == std::get<0>(rhs) &&
absl::c_equal(std::get<1>(lhs), std::get<1>(rhs));
}
};

absl::Nonnull<google::protobuf::Arena*> const arena_;
absl::flat_hash_set<FunctionType, Hasher, Equaler> function_types_;
};

} // namespace cel::common_internal

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPES_FUNCTION_TYPE_POOL_H_
1 change: 0 additions & 1 deletion common/types/list_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class ListType;

namespace common_internal {
struct ListTypeData;
class ListTypePool;
} // namespace common_internal

class ListType final {
Expand Down
29 changes: 29 additions & 0 deletions common/types/list_type_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 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 "common/types/list_type_pool.h"

#include "common/type.h"

namespace cel::common_internal {

ListType ListTypePool::InternListType(const Type& element) {
if (element.IsDyn()) {
return ListType();
}
return *list_types_.lazy_emplace(
element, [&](const auto& ctor) { ctor(ListType(arena_, element)); });
}

} // namespace cel::common_internal
80 changes: 80 additions & 0 deletions common/types/list_type_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 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.

// IWYU pragma: private

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_LIST_TYPE_POOL_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_LIST_TYPE_POOL_H_

#include <cstddef>

#include "absl/base/nullability.h"
#include "absl/container/flat_hash_set.h"
#include "absl/hash/hash.h"
#include "absl/log/die_if_null.h"
#include "common/type.h"
#include "google/protobuf/arena.h"

namespace cel::common_internal {

// `ListTypePool` is a thread unsafe interning factory for `ListType`.
class ListTypePool final {
public:
explicit ListTypePool(absl::Nonnull<google::protobuf::Arena*> arena)
: arena_(ABSL_DIE_IF_NULL(arena)) {} // Crash OK

// Returns a `ListType` which has the provided parameters, interning as
// necessary.
ListType InternListType(const Type& element);

private:
struct Hasher {
using is_transparent = void;

size_t operator()(const ListType& list_type) const {
return (*this)(list_type.element());
}

size_t operator()(const Type& type) const {
return absl::Hash<Type>{}(type);
}
};

struct Equaler {
using is_transparent = void;

bool operator()(const ListType& lhs, const ListType& rhs) const {
return (*this)(lhs.element(), rhs.element());
}

bool operator()(const ListType& lhs, const Type& rhs) const {
return (*this)(lhs.element(), rhs);
}

bool operator()(const Type& lhs, const ListType& rhs) const {
return (*this)(lhs, rhs.element());
}

bool operator()(const Type& lhs, const Type& rhs) const {
return lhs == rhs;
}
};

absl::Nonnull<google::protobuf::Arena*> const arena_;
absl::flat_hash_set<ListType, Hasher, Equaler> list_types_;
};

} // namespace cel::common_internal

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPES_LIST_TYPE_POOL_H_
1 change: 0 additions & 1 deletion common/types/map_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class MapType;

namespace common_internal {
struct MapTypeData;
class MapTypePool;
} // namespace common_internal

MapType JsonMapType();
Expand Down
30 changes: 30 additions & 0 deletions common/types/map_type_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 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 "common/types/map_type_pool.h"

#include "common/type.h"

namespace cel::common_internal {

MapType MapTypePool::InternMapType(const Type& key, const Type& value) {
if (key.IsDyn() && value.IsDyn()) {
return MapType();
}
return *map_types_.lazy_emplace(AsTuple(key, value), [&](const auto& ctor) {
ctor(MapType(arena_, key, value));
});
}

} // namespace cel::common_internal
Loading