try hard to reduce arena usage

This commit is contained in:
Joao Paulo Magalhaes
2025-09-30 16:33:06 +01:00
parent 6be1380ea5
commit 4abe5e3429
3 changed files with 50 additions and 24 deletions

View File

@@ -3550,29 +3550,51 @@ csubstr ParseEngine<EventHandler>::_filter_scalar_dquot(substr s)
//-----------------------------------------------------------------------------
template<class EventHandler>
csubstr ParseEngine<EventHandler>::_move_scalar_left_and_add_newline(substr s)
{
if(s.is_sub(m_buf))
{
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, s.str > m_buf.str);
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, s.str-1 >= m_buf.str);
if(s.len)
memmove(s.str - 1, s.str, s.len);
--s.str;
s.str[s.len] = '\n';
++s.len;
return s;
}
else
{
substr dst = m_evt_handler->alloc_arena(s.len + 1);
if(s.len)
memcpy(dst.str, s.str, s.len);
dst[s.len] = '\n';
return dst;
}
}
template<class EventHandler>
csubstr ParseEngine<EventHandler>::_filter_scalar_literal(substr s, size_t indentation, BlockChomp_e chomp)
{
_c4dbgpf("filtering block literal scalar: s=[{}]~~~{}~~~", s.len, s);
FilterResult r = this->filter_scalar_block_literal_in_place(s, s.len, indentation, chomp);
csubstr result;
if(C4_LIKELY(r.valid()))
{
_c4dbgpf("filtering block literal scalar: success! s=[{}]~~~{}~~~", r.get().len, r.get());
return r.get();
result = r.get();
}
else
{
_c4dbgpf("filtering block literal scalar: not enough space: needs {}, have {}", r.required_len(), s.len);
substr dst = m_evt_handler->alloc_arena(r.required_len(), &s);
if(dst.str)
{
FilterResult rsd = this->filter_scalar_block_literal(s, dst, indentation, chomp);
_RYML_CB_CHECK(m_evt_handler->m_stack.m_callbacks, rsd.valid());
_c4dbgpf("filtering block literal scalar: success! s=[{}]~~~{}~~~", rsd.get().len, rsd.get());
return rsd.get();
}
return dst;
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, r.required_len() == s.len + 1);
// this can only happen when adding a single newline in clip mode.
// so we shift left the scalar by one place
result = _move_scalar_left_and_add_newline(s);
}
_c4dbgpf("filtering block literal scalar: success! s=[{}]~~~{}~~~", result.len, result);
return result;
}
@@ -3582,24 +3604,21 @@ csubstr ParseEngine<EventHandler>::_filter_scalar_folded(substr s, size_t indent
{
_c4dbgpf("filtering block folded scalar: s=[{}]~~~{}~~~", s.len, s);
FilterResult r = this->filter_scalar_block_folded_in_place(s, s.len, indentation, chomp);
csubstr result;
if(C4_LIKELY(r.valid()))
{
_c4dbgpf("filtering block folded scalar: success! s=[{}]~~~{}~~~", r.get().len, r.get());
return r.get();
result = r.get();
}
else
{
_c4dbgpf("filtering block folded scalar: not enough space: needs {}, have {}", r.required_len(), s.len);
substr dst = m_evt_handler->alloc_arena(r.required_len(), &s);
if(dst.str)
{
FilterResult rsd = this->filter_scalar_block_folded(s, dst, indentation, chomp);
_RYML_CB_CHECK(m_evt_handler->m_stack.m_callbacks, rsd.valid());
_c4dbgpf("filtering block folded scalar: success! s=[{}]~~~{}~~~", rsd.get().len, rsd.get());
return rsd.get();
}
return dst;
_RYML_CB_ASSERT(m_evt_handler->m_stack.m_callbacks, r.required_len() == s.len + 1);
// this can only happen when adding a single newline in clip mode.
// so we shift left the scalar by one place
result = _move_scalar_left_and_add_newline(s);
}
_c4dbgpf("filtering block folded scalar: success! s=[{}]~~~{}~~~", result.len, result);
return result;
}

View File

@@ -536,6 +536,7 @@ public: // exposed for testing
csubstr _filter_scalar_dquot(substr s);
csubstr _filter_scalar_literal(substr s, size_t indentation, BlockChomp_e chomp);
csubstr _filter_scalar_folded(substr s, size_t indentation, BlockChomp_e chomp);
csubstr _move_scalar_left_and_add_newline(substr s);
csubstr _maybe_filter_key_scalar_plain(ScannedScalar const& sc, size_t indendation);
csubstr _maybe_filter_val_scalar_plain(ScannedScalar const& sc, size_t indendation);

View File

@@ -541,7 +541,10 @@ const IntEventsCase test_cases[] = {
{
e(BSTR),
e(BDOC),
e(VAL_|SCLR|LITL|AREN, 0, 4, "abc\n"), // result has additional newline
// instead of this (in the arena):
//e(VAL_|SCLR|LITL|AREN, 0, 4, "abc\n"), // result has additional newline
// we get this (shifted left by 1, in the source code)
e(VAL_|SCLR|LITL, 1, 4, "abc\n"), // result has additional newline
e(EDOC|PSTR),
e(ESTR),
}),
@@ -553,7 +556,10 @@ const IntEventsCase test_cases[] = {
{
e(BSTR),
e(BDOC),
e(VAL_|SCLR|FOLD|AREN, 0, 4, "abc\n"), // result has additional newline
// instead of this (in the arena):
//e(VAL_|SCLR|FOLD|AREN, 0, 4, "abc\n"), // result has additional newline
// we get this (shifted left by 1, in the source code)
e(VAL_|SCLR|FOLD, 1, 4, "abc\n"), // result has additional newline
e(EDOC|PSTR),
e(ESTR),
}),