Skip to content

Commit

Permalink
New internal string format:
Browse files Browse the repository at this point in the history
* maximum size of a string is 2^32-1 now by default on a 64 bits system (can be increased using M_USE_STRING_LARGE_INDEX if needed).
* This reduce consumption of a string_t from 24 bytes to 16 bytes.
* Change the stack representation of a string, so that it lets 15 allocated bytes for the user even in 64 bytes mode. For 32 bits system, it increases to 15 allocated byte for stack representation.
* synthetic performance benchmark shows a little bit of decrease of performance. But performance in real application shows a significant increase (probably due to the reduced size of the type).
*Augment test suite to be sure to test all corner cases of the new representaiton.
  • Loading branch information
P-p-H-d committed May 28, 2024
1 parent bf59c53 commit 67e9c8f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 96 deletions.
16 changes: 10 additions & 6 deletions m-serial-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ m_ser1al_json_write_float(m_serial_write_t serial, const long double data, const
M_INLINE m_serial_return_code_t
m_ser1al_json_write_string(m_serial_write_t serial, const char data[], size_t length)
{
M_ASSERT(length == (m_str1ng_size_t) length);
M_ASSERT_SLOW(length == strlen(data) );
FILE *f = (FILE *)serial->data[0].p;
M_ASSERT(f != NULL && data != NULL);
/* HACK: Build dummy string to reuse m_string_out_str */
m_string_t v2;
uintptr_t ptr = (uintptr_t) data;
v2->u.heap.size = length;
v2->u.heap.alloc = length + 1;
v2->ptr = (char*)ptr;
v2->u.heap.size = (m_str1ng_size_t) length;
v2->u.heap.alloc[sizeof(m_str1ng_size_t)-2] = 1;
v2->u.heap.alloc[sizeof(m_str1ng_size_t)-1] = 31;
v2->u.heap.ptr = (char*)ptr;
m_string_out_str(f, v2);
return M_UNLIKELY(ferror(f)) ? m_core_serial_fail() : M_SERIAL_OK_DONE;
}
Expand Down Expand Up @@ -620,15 +622,17 @@ m_ser1al_str_json_write_float(m_serial_write_t serial, const long double data, c
M_INLINE m_serial_return_code_t
m_ser1al_str_json_write_string(m_serial_write_t serial, const char data[], size_t length)
{
M_ASSERT(length == (m_str1ng_size_t) length);
M_ASSERT_SLOW(length == strlen(data) );
struct m_string_s *f = (struct m_string_s *)serial->data[0].p;
M_ASSERT(f != NULL && data != NULL);
/* HACK: Build dummy string to reuse m_string_get_str */
m_string_t v2;
uintptr_t ptr = (uintptr_t) data;
v2->u.heap.size = length;
v2->u.heap.alloc = length + 1;
v2->ptr = (char*)ptr;
v2->u.heap.size = (m_str1ng_size_t) length;
v2->u.heap.alloc[sizeof(m_str1ng_size_t)-2] = 1;
v2->u.heap.alloc[sizeof(m_str1ng_size_t)-1] = 31;
v2->u.heap.ptr = (char*)ptr;
m_string_get_str(f, v2, true);
return M_SERIAL_OK_DONE;
}
Expand Down
Loading

0 comments on commit 67e9c8f

Please sign in to comment.