v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-forwarding-table.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_STRING_FORWARDING_TABLE_H_
6#define V8_OBJECTS_STRING_FORWARDING_TABLE_H_
7
9
10// Has to be the last include (doesn't have include guards):
12
13namespace v8 {
14namespace internal {
15
16// Mapping from forwarding indices (stored in a string's hash field) to
17// internalized strings/external resources.
18// The table is used to handle string transitions (temporarily until the next
19// full GC, during which actual string transitions happen) that overwrite the
20// string buffer. In particular these are Internalization and Externalization.
21// The table is organised in "blocks". As writes only append new entries, the
22// organisation in blocks allows lock-free writes. We need a lock only for
23// growing the table (adding more blocks). When the vector holding the blocks
24// needs to grow, we keep a copy of the old vector alive to allow concurrent
25// reads while the vector is relocated.
27 public:
28 // Capacity for the first block.
29 static constexpr int kInitialBlockSize = 16;
31 static constexpr int kInitialBlockSizeHighestBit =
33 // Initial capacity in the block vector.
34 static constexpr int kInitialBlockVectorCapacity = 4;
35 static constexpr Tagged<Smi> unused_element() { return Smi::FromInt(0); }
36 static constexpr Tagged<Smi> deleted_element() { return Smi::FromInt(1); }
37
38 explicit StringForwardingTable(Isolate* isolate);
40
41 inline int size() const;
42 inline bool empty() const;
43 // Returns the index of the added record.
44 int AddForwardString(Tagged<String> string, Tagged<String> forward_to);
45 template <typename T>
47 int AddExternalResourceAndHash(Tagged<String> string, T* resource,
48 uint32_t raw_hash);
49 void UpdateForwardString(int index, Tagged<String> forward_to);
50 // Returns true when the resource was set. When an external resource is
51 // already set for the record, false is returned and the resource not stored.
52 // The caller is responsible for disposing the resource.
53 template <typename T>
55 bool TryUpdateExternalResource(int index, T* resource);
56 Tagged<String> GetForwardString(PtrComprCageBase cage_base, int index) const;
57 static Address GetForwardStringAddress(Isolate* isolate, int index);
59 int index) const;
60 static uint32_t GetRawHashStatic(Isolate* isolate, int index);
61 v8::String::ExternalStringResourceBase* GetExternalResource(
62 int index, bool* is_one_byte) const;
63
64 template <typename Func>
66 // Dispose all external resources stored in the table.
67 void TearDown();
68 void Reset();
71
72 class Record;
73
74 private:
75 class Block;
76 class BlockVector;
77
78 // Returns the block for a given index and sets the index within this block
79 // as out parameter.
80 static inline uint32_t BlockForIndex(int index, uint32_t* index_in_block_out);
81 static inline uint32_t IndexInBlock(int index, uint32_t block);
82 static inline uint32_t CapacityForBlock(uint32_t block);
83
85 // Ensure that |block| exists in the BlockVector already. If not, a new block
86 // is created (with capacity double the capacity of the last block) and
87 // inserted into the BlockVector. The BlockVector itself might grow (to double
88 // the capacity).
89 BlockVector* EnsureCapacity(uint32_t block);
90
93 // We need a vector of BlockVectors to keep old BlockVectors alive when we
94 // grow the table, due to concurrent reads that may still hold a pointer to
95 // them. |block_vector_sotrage_| is only accessed while we grow with the mutex
96 // held. All regular access go through |block_|, which holds a pointer to the
97 // current BlockVector.
99 std::atomic<int> next_free_index_;
100 base::Mutex grow_mutex_;
101};
102
103} // namespace internal
104} // namespace v8
105
106#include "src/objects/object-macros-undef.h"
107
108#endif // V8_OBJECTS_STRING_FORWARDING_TABLE_H_
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static uint32_t IndexInBlock(int index, uint32_t block)
bool TryUpdateExternalResource(int index, T *resource)
static constexpr Tagged< Smi > unused_element()
int AddExternalResourceAndHash(Tagged< String > string, T *resource, uint32_t raw_hash)
static Address GetForwardStringAddress(Isolate *isolate, int index)
void UpdateForwardString(int index, Tagged< String > forward_to)
V8_EXPORT_PRIVATE uint32_t GetRawHash(PtrComprCageBase cage_base, int index) const
v8::String::ExternalStringResourceBase * GetExternalResource(int index, bool *is_one_byte) const
static uint32_t GetRawHashStatic(Isolate *isolate, int index)
static uint32_t CapacityForBlock(uint32_t block)
static uint32_t BlockForIndex(int index, uint32_t *index_in_block_out)
V8_INLINE void IterateElements(Func &&callback)
static constexpr Tagged< Smi > deleted_element()
BlockVector * EnsureCapacity(uint32_t block)
Tagged< String > GetForwardString(PtrComprCageBase cage_base, int index) const
std::vector< std::unique_ptr< BlockVector > > block_vector_storage_
int AddForwardString(Tagged< String > string, Tagged< String > forward_to)
#define EXPORT_TEMPLATE_DECLARE(export)
TNode< Object > callback
STL namespace.
constexpr bool IsPowerOfTwo(T value)
Definition bits.h:187
constexpr unsigned CountLeadingZeros32(uint32_t value)
Definition bits.h:122
constexpr int kBitsPerInt
Definition globals.h:687
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500