# HG changeset patch # User egahlin # Date 1599858543 -10800 # Node ID cb1e375e88a9464056567cf1f419225e0a7fba13 # Parent d0f692037e7bdbae7e5847b51d7a55b0328ae6c1 8236196: Improve string pooling Reviewed-by: mgronlun, rehn, ahgross, jwilhelm, rhalade, mbalao, andrew diff -r d0f692037e7b -r cb1e375e88a9 src/share/vm/jfr/writers/jfrWriterHost.inline.hpp --- a/src/share/vm/jfr/writers/jfrWriterHost.inline.hpp Tue Sep 22 13:09:39 2020 +0300 +++ b/src/share/vm/jfr/writers/jfrWriterHost.inline.hpp Sat Sep 12 00:09:03 2020 +0300 @@ -70,7 +70,8 @@ inline void WriterHost::write(const T* value, size_t len) { assert(value != NULL, "invariant"); assert(len > 0, "invariant"); - u1* const pos = ensure_size(sizeof(T) * len); + // Might need T + 1 size + u1* const pos = ensure_size(sizeof(T) * len + len); if (pos) { this->set_current_pos(write(value, len, pos)); } @@ -124,7 +125,8 @@ inline void WriterHost::be_write(const T* value, size_t len) { assert(value != NULL, "invariant"); assert(len > 0, "invariant"); - u1* const pos = ensure_size(sizeof(T) * len); + // Might need T + 1 size + u1* const pos = ensure_size(sizeof(T) * len + len); if (pos) { this->set_current_pos(BE::be_write(value, len, pos)); } @@ -137,10 +139,17 @@ _compressed_integers(compressed_integers()) { } +// Extra size added as a safety cushion when dimensioning memory. +// With varint encoding, the worst case is +// associated with writing negative values. +// For example, writing a negative s1 (-1) +// will encode as 0xff 0x0f (2 bytes). +static const size_t size_safety_cushion = 1; + template template inline WriterHost::WriterHost(StorageType* storage, size_t size) : - WriterPolicyImpl(storage, size), + WriterPolicyImpl(storage, size + size_safety_cushion), _compressed_integers(compressed_integers()) { } @@ -150,30 +159,19 @@ _compressed_integers(compressed_integers()) { } -// Extra size added as a safety cushion when dimensioning memory. -// With varint encoding, the worst case is -// associated with writing negative values. -// For example, writing a negative s1 (-1) -// will encode as 0xff 0x0f (2 bytes). -// In this example, the sizeof(T) == 1 and length == 1, -// but the implementation will need to dimension -// 2 bytes for the encoding. -// Hopefully, negative values should be relatively rare. -static const size_t size_safety_cushion = 1; - template -inline u1* WriterHost::ensure_size(size_t requested) { +inline u1* WriterHost::ensure_size(size_t requested_size) { if (!this->is_valid()) { // cancelled return NULL; } - if (this->available_size() < requested + size_safety_cushion) { - if (!this->accommodate(this->used_size(), requested + size_safety_cushion)) { + if (this->available_size() < requested_size) { + if (!this->accommodate(this->used_size(), requested_size)) { this->cancel(); return NULL; } } - assert(requested + size_safety_cushion <= this->available_size(), "invariant"); + assert(requested_size <= this->available_size(), "invariant"); return this->current_pos(); }