History

Created multiple pointer field iterators based on application Fix bug in vm_string class related to appending null at the end of the current string - This was recently introduced because I didn't test the case :( Scheduler had an invalid usage of std::mutex that was found using the debug version of CRT Fix heinous perf issue in GC where it was taking longer than code execution - Switch from std::queue to std::stack -> perf gain of ~20%

arr committed 9 years ago

Commit 30c4c581 parent 6f29a816 files · +102 −39Browse files

Showing the changes to src/vm/string.cpp (1 of 6 files). Show all

Unified · Split

src/vm/string.cpp+21−10
@@ -123,14 +123,14 @@ namespace · 122 unmodified lines
123123 template<>
124124 void copy_characters_to(char *dest, word_t dest_start, const char *source, word_t source_length)
125125 {
126− assert(dest != nullptr && source != nullptr);
126+ assert(dest != nullptr && source != nullptr && dest != source);
127127 std::memcpy((dest + dest_start), source, source_length);
128128 }
129129
130130 template<>
131131 void copy_characters_to(runtime::rune_t *dest, word_t dest_start, const runtime::rune_t *source, word_t source_length)
132132 {
133− assert(dest != nullptr && source != nullptr);
133+ assert(dest != nullptr && source != nullptr && dest != source);
134134 std::memcpy((dest + dest_start), source, (source_length * sizeof(runtime::rune_t)));
135135 }
136136
@@ -286,17 +286,19 @@ vm_string_t::vm_string_t(const vm_string_t &other, word_t begin_index, word_t en · 149 unmodified lines
286286 // Allocate memory based on character count and size
287287 auto src = other._mem.local;
288288 auto dest = _mem.local;
289− const auto length_in_bytes = _length * _character_size;
290289 if (_is_alloc)
291290 {
292291 src = other._mem.alloc;
293− _mem.alloc = alloc_memory<uint8_t>(_length_max);
292+ _mem.alloc = alloc_memory<uint8_t>(_length_max * _character_size);
294293 dest = _mem.alloc;
295294 }
296295
297296 // Copy over string content starting at the correct index (char vs. rune_t)
297+ const auto length_in_bytes = _length * _character_size;
298298 std::memcpy(dest, (src + (begin_index * _character_size)), length_in_bytes);
299− debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "copy: vm string: %#" PRIxPTR " %d %d %#" PRIxPTR, &other, begin_index, end_index, this);
299+
300+ if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>())
301+ debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "copy: vm string: %#" PRIxPTR " %d %d %#" PRIxPTR, &other, begin_index, end_index, this);
300302 }
301303
302304 vm_string_t::~vm_string_t()
@@ -309,10 +311,11 @@ vm_string_t::~vm_string_t() · 6 unmodified lines
309311 if (_is_alloc)
310312 free_memory(source);
311313
312− debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string");
313−
314314 debug::assign_debug_pointer(&source);
315315 debug::assign_debug_pointer(&_encoded_str);
316+
317+ if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>())
318+ debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string");
316319 }
317320
318321 vm_string_t &vm_string_t::append(const vm_string_t &other)
@@ -324,6 +327,7 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 5 unmodified lines
324327
325328 // Check if the new string has any length at all.
326329 const auto other_length = other._length;
330+ assert(other_length >= 0);
327331 if (other_length == 0)
328332 return *this;
329333
@@ -342,7 +346,7 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 12 unmodified lines
342346
343347 // Define the values for the appended string
344348 const auto new_length = local_length + other_length;
345− const auto new_length_max = (new_length == 0) ? static_cast<word_t>(compute_max_length(_mem.local)) : compute_max_length(new_length);
349+ const auto new_length_max = compute_max_length(new_length);
346350 const auto new_is_rune = local_is_rune || other_is_rune;
347351 uint8_t *new_source = nullptr;
348352
@@ -497,8 +501,15 @@ const char *vm_string_t::str() const · 148 unmodified lines
497501 // If the string is ASCII then return the memory
498502 if (_character_size == sizeof(char))
499503 {
500− _encoded_str = (_is_alloc) ? reinterpret_cast<char *>(_mem.alloc) : reinterpret_cast<char *>(_mem.local);
501− _encoded_str[_length] = '\0';
504+ auto str = (_is_alloc) ? _mem.alloc : _mem.local;
505+ if (_length_max <= _length)
506+ {
507+ auto tmp = alloc_memory<char>(_length + 1); // +1 for null
508+ str = static_cast<uint8_t *>(std::memcpy(tmp, str, _length));
509+ }
510+
511+ _encoded_str = reinterpret_cast<char *>(str);
512+ assert(_encoded_str[_length] == '\0');
502513 return _encoded_str;
503514 }
504515