2020-10-08 11:20:09 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
|
|
|
|
#define INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
|
|
|
|
|
2020-10-08 19:03:04 +00:00
|
|
|
#include <cstddef>
|
2022-05-02 16:21:20 +00:00
|
|
|
#include <cstdint>
|
cppgc: Optimize GCInfo setup
In Blink's version of Oilpan, GCInfo objects would reside in .bss and
a table would translate between an index and the .bss address. Upon
retrieving a GCInfoIndex, the slow path merely passes a .bss pointer
to a slow path setup method to create the table mapping.
In cppgc, we set up GCInfo entries directly in the table. This is
slightly faster for actually using GCInfo objects as there's no
indirection between table and .bss, and it also saves one pointer (the
indirection) per type that is set up. The downside of this approach is
that individual components of a GCInfo objects, that are all
type-dependent, need to be passed to the conditional setup method.
Since GCInfo indices must be retrieved on each allocation, this
pollutes the fast path with additional instructions.
However, GCInfo components are actually known at compile-time for many
objects. In such cases, we can use a compile-time static dispatch to
encode the known parameters in different functions. This saves around
40KiB of memory on ChromePublic.apk and also creates a more compact
fast path for allocation.
Bug: chromium:1238884, chromium:1056170
Change-Id: Iedd809a8baefcc02f131d2b2c77d341b0abe43bb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3094007
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76291}
2021-08-13 08:01:26 +00:00
|
|
|
#include <type_traits>
|
2020-10-08 19:03:04 +00:00
|
|
|
|
2020-10-08 11:20:09 +00:00
|
|
|
#include "cppgc/name-provider.h"
|
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
namespace internal {
|
|
|
|
|
2020-10-08 19:03:04 +00:00
|
|
|
#if CPPGC_SUPPORTS_OBJECT_NAMES && defined(__clang__)
|
|
|
|
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 1
|
|
|
|
|
2020-10-09 07:53:31 +00:00
|
|
|
// Provides constexpr c-string storage for a name of fixed |Size| characters.
|
|
|
|
// Automatically appends terminating 0 byte.
|
2020-10-08 19:03:04 +00:00
|
|
|
template <size_t Size>
|
|
|
|
struct NameBuffer {
|
2020-10-09 07:53:31 +00:00
|
|
|
char name[Size + 1]{};
|
2020-10-08 19:03:04 +00:00
|
|
|
|
|
|
|
static constexpr NameBuffer FromCString(const char* str) {
|
|
|
|
NameBuffer result;
|
|
|
|
for (size_t i = 0; i < Size; ++i) result.name[i] = str[i];
|
2020-10-09 07:53:31 +00:00
|
|
|
result.name[Size] = 0;
|
2020-10-08 19:03:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const char* GetTypename() {
|
|
|
|
static constexpr char kSelfPrefix[] =
|
|
|
|
"const char *cppgc::internal::GetTypename() [T =";
|
|
|
|
static_assert(__builtin_strncmp(__PRETTY_FUNCTION__, kSelfPrefix,
|
|
|
|
sizeof(kSelfPrefix) - 1) == 0,
|
|
|
|
"The prefix must match");
|
|
|
|
static constexpr const char* kTypenameStart =
|
|
|
|
__PRETTY_FUNCTION__ + sizeof(kSelfPrefix);
|
|
|
|
static constexpr size_t kTypenameSize =
|
|
|
|
__builtin_strlen(__PRETTY_FUNCTION__) - sizeof(kSelfPrefix) - 1;
|
|
|
|
// NameBuffer is an indirection that is needed to make sure that only a
|
|
|
|
// substring of __PRETTY_FUNCTION__ gets materialized in the binary.
|
|
|
|
static constexpr auto buffer =
|
|
|
|
NameBuffer<kTypenameSize>::FromCString(kTypenameStart);
|
|
|
|
return buffer.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME 0
|
|
|
|
#endif
|
|
|
|
|
2020-10-08 11:20:09 +00:00
|
|
|
struct HeapObjectName {
|
|
|
|
const char* value;
|
|
|
|
bool name_was_hidden;
|
|
|
|
};
|
|
|
|
|
2022-05-02 16:21:20 +00:00
|
|
|
enum class HeapObjectNameForUnnamedObject : uint8_t {
|
|
|
|
kUseClassNameIfSupported,
|
|
|
|
kUseHiddenName,
|
|
|
|
};
|
|
|
|
|
2020-10-08 11:20:09 +00:00
|
|
|
class V8_EXPORT NameTraitBase {
|
|
|
|
protected:
|
|
|
|
static HeapObjectName GetNameFromTypeSignature(const char*);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Trait that specifies how the garbage collector retrieves the name for a
|
|
|
|
// given object.
|
|
|
|
template <typename T>
|
|
|
|
class NameTrait final : public NameTraitBase {
|
|
|
|
public:
|
cppgc: Optimize GCInfo setup
In Blink's version of Oilpan, GCInfo objects would reside in .bss and
a table would translate between an index and the .bss address. Upon
retrieving a GCInfoIndex, the slow path merely passes a .bss pointer
to a slow path setup method to create the table mapping.
In cppgc, we set up GCInfo entries directly in the table. This is
slightly faster for actually using GCInfo objects as there's no
indirection between table and .bss, and it also saves one pointer (the
indirection) per type that is set up. The downside of this approach is
that individual components of a GCInfo objects, that are all
type-dependent, need to be passed to the conditional setup method.
Since GCInfo indices must be retrieved on each allocation, this
pollutes the fast path with additional instructions.
However, GCInfo components are actually known at compile-time for many
objects. In such cases, we can use a compile-time static dispatch to
encode the known parameters in different functions. This saves around
40KiB of memory on ChromePublic.apk and also creates a more compact
fast path for allocation.
Bug: chromium:1238884, chromium:1056170
Change-Id: Iedd809a8baefcc02f131d2b2c77d341b0abe43bb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3094007
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76291}
2021-08-13 08:01:26 +00:00
|
|
|
static constexpr bool HasNonHiddenName() {
|
|
|
|
#if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
|
|
|
|
return true;
|
|
|
|
#elif CPPGC_SUPPORTS_OBJECT_NAMES
|
|
|
|
return true;
|
|
|
|
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
|
|
|
|
return std::is_base_of<NameProvider, T>::value;
|
|
|
|
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
|
|
|
|
}
|
|
|
|
|
2022-05-02 16:21:20 +00:00
|
|
|
static HeapObjectName GetName(
|
|
|
|
const void* obj, HeapObjectNameForUnnamedObject name_retrieval_mode) {
|
|
|
|
return GetNameFor(static_cast<const T*>(obj), name_retrieval_mode);
|
2020-10-08 11:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-05-02 16:21:20 +00:00
|
|
|
static HeapObjectName GetNameFor(const NameProvider* name_provider,
|
|
|
|
HeapObjectNameForUnnamedObject) {
|
|
|
|
// Objects inheriting from `NameProvider` are not considered unnamed as
|
|
|
|
// users already provided a name for them.
|
2021-06-08 14:47:01 +00:00
|
|
|
return {name_provider->GetHumanReadableName(), false};
|
2020-10-08 11:20:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 16:21:20 +00:00
|
|
|
static HeapObjectName GetNameFor(
|
|
|
|
const void*, HeapObjectNameForUnnamedObject name_retrieval_mode) {
|
|
|
|
if (name_retrieval_mode == HeapObjectNameForUnnamedObject::kUseHiddenName)
|
|
|
|
return {NameProvider::kHiddenName, true};
|
|
|
|
|
2020-10-08 19:03:04 +00:00
|
|
|
#if CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
|
|
|
|
return {GetTypename<T>(), false};
|
|
|
|
#elif CPPGC_SUPPORTS_OBJECT_NAMES
|
2020-10-08 11:20:09 +00:00
|
|
|
|
|
|
|
#if defined(V8_CC_GNU)
|
|
|
|
#define PRETTY_FUNCTION_VALUE __PRETTY_FUNCTION__
|
|
|
|
#elif defined(V8_CC_MSVC)
|
|
|
|
#define PRETTY_FUNCTION_VALUE __FUNCSIG__
|
|
|
|
#else
|
|
|
|
#define PRETTY_FUNCTION_VALUE nullptr
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const HeapObjectName leaky_name =
|
|
|
|
GetNameFromTypeSignature(PRETTY_FUNCTION_VALUE);
|
2022-07-18 08:43:11 +00:00
|
|
|
return leaky_name;
|
2020-10-08 11:20:09 +00:00
|
|
|
|
|
|
|
#undef PRETTY_FUNCTION_VALUE
|
|
|
|
|
|
|
|
#else // !CPPGC_SUPPORTS_OBJECT_NAMES
|
|
|
|
return {NameProvider::kHiddenName, true};
|
|
|
|
#endif // !CPPGC_SUPPORTS_OBJECT_NAMES
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-02 16:21:20 +00:00
|
|
|
using NameCallback = HeapObjectName (*)(const void*,
|
|
|
|
HeapObjectNameForUnnamedObject);
|
2020-10-08 11:20:09 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace cppgc
|
|
|
|
|
2020-10-08 19:03:04 +00:00
|
|
|
#undef CPPGC_SUPPORTS_COMPILE_TIME_TYPENAME
|
|
|
|
|
2020-10-08 11:20:09 +00:00
|
|
|
#endif // INCLUDE_CPPGC_INTERNAL_NAME_TRAIT_H_
|