Skip to content

Commit fa25d08

Browse files
egahlinslowhog
authored andcommitted
8236196: Improve string pooling
Reviewed-by: mgronlun, rehn, ahgross, jwilhelm, rhalade
1 parent afd852c commit fa25d08

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/hotspot/share/jfr/writers/jfrWriterHost.inline.hpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ template <typename T>
7171
inline void WriterHost<BE, IE, WriterPolicyImpl>::write(const T* value, size_t len) {
7272
assert(value != NULL, "invariant");
7373
assert(len > 0, "invariant");
74-
u1* const pos = ensure_size(sizeof(T) * len);
74+
// Might need T + 1 size
75+
u1* const pos = ensure_size(sizeof(T) * len + len);
7576
if (pos) {
7677
this->set_current_pos(write(value, len, pos));
7778
}
@@ -122,7 +123,8 @@ template <typename T>
122123
inline void WriterHost<BE, IE, WriterPolicyImpl>::be_write(const T* value, size_t len) {
123124
assert(value != NULL, "invariant");
124125
assert(len > 0, "invariant");
125-
u1* const pos = ensure_size(sizeof(T) * len);
126+
// Might need T + 1 size
127+
u1* const pos = ensure_size(sizeof(T) * len + len);
126128
if (pos) {
127129
this->set_current_pos(BE::be_write(value, len, pos));
128130
}
@@ -135,10 +137,17 @@ inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, Th
135137
_compressed_integers(compressed_integers()) {
136138
}
137139

140+
// Extra size added as a safety cushion when dimensioning memory.
141+
// With varint encoding, the worst case is
142+
// associated with writing negative values.
143+
// For example, writing a negative s1 (-1)
144+
// will encode as 0xff 0x0f (2 bytes).
145+
static const size_t size_safety_cushion = 1;
146+
138147
template <typename BE, typename IE, typename WriterPolicyImpl >
139148
template <typename StorageType>
140149
inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, size_t size) :
141-
WriterPolicyImpl(storage, size),
150+
WriterPolicyImpl(storage, size + size_safety_cushion),
142151
_compressed_integers(compressed_integers()) {
143152
}
144153

@@ -148,30 +157,19 @@ inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(Thread* thread) :
148157
_compressed_integers(compressed_integers()) {
149158
}
150159

151-
// Extra size added as a safety cushion when dimensioning memory.
152-
// With varint encoding, the worst case is
153-
// associated with writing negative values.
154-
// For example, writing a negative s1 (-1)
155-
// will encode as 0xff 0x0f (2 bytes).
156-
// In this example, the sizeof(T) == 1 and length == 1,
157-
// but the implementation will need to dimension
158-
// 2 bytes for the encoding.
159-
// Hopefully, negative values should be relatively rare.
160-
static const size_t size_safety_cushion = 1;
161-
162160
template <typename BE, typename IE, typename WriterPolicyImpl>
163-
inline u1* WriterHost<BE, IE, WriterPolicyImpl>::ensure_size(size_t requested) {
161+
inline u1* WriterHost<BE, IE, WriterPolicyImpl>::ensure_size(size_t requested_size) {
164162
if (!this->is_valid()) {
165163
// cancelled
166164
return NULL;
167165
}
168-
if (this->available_size() < requested + size_safety_cushion) {
169-
if (!this->accommodate(this->used_size(), requested + size_safety_cushion)) {
166+
if (this->available_size() < requested_size) {
167+
if (!this->accommodate(this->used_size(), requested_size)) {
170168
assert(!this->is_valid(), "invariant");
171169
return NULL;
172170
}
173171
}
174-
assert(requested + size_safety_cushion <= this->available_size(), "invariant");
172+
assert(requested_size <= this->available_size(), "invariant");
175173
return this->current_pos();
176174
}
177175

0 commit comments

Comments
 (0)