ENHANCED: partial_string/3 no longer creates atoms

As a consequence, resulting strings are now quickly reclaimed on
backtracking.

This addresses #2912.

Test case:

    :- use_module(library(iso_ext)).
    :- use_module(library(lists)).

    ab(a).
    ab(b).

Sample query:

    ?- length(Ls, 1_000_000),
       maplist(ab, Ls),
       partial_string(Ls, Es0, []),
       Es0 == Ls.
       Ls = "aaaaaaaaaaaaaaaaaaa ...", Es0 = "aaaaaaaaaaaaaaaaaaa ..."
    ;  Ls = "aaaaaaaaaaaaaaaaaaa ...", Es0 = "aaaaaaaaaaaaaaaaaaa ..."
    ;  Ls = "aaaaaaaaaaaaaaaaaaa ...", Es0 = "aaaaaaaaaaaaaaaaaaa ..."
    ;  Ls = "aaaaaaaaaaaaaaaaaaa ...", Es0 = "aaaaaaaaaaaaaaaaaaa ..."
    ;  Ls = "aaaaaaaaaaaaaaaaaaa ...", Es0 = "aaaaaaaaaaaaaaaaaaa ..."
    ;  ... .

running in constant memory.
This commit is contained in:
Markus Triska
2025-04-25 22:16:44 +02:00
committed by Mark Thom
parent e9bb41f0d6
commit d51cbd67e0
2 changed files with 26 additions and 25 deletions

View File

@@ -2479,30 +2479,32 @@ impl Machine {
#[inline(always)]
pub(crate) fn create_partial_string(&mut self) {
let atom = cell_as_atom!(self.deref_register(1));
let a1 = self.deref_register(1);
if atom == atom!("") {
self.machine_st.fail = true;
return;
}
if let Some(str_like) = self.machine_st.value_to_str_like(a1) {
let str = match str_like {
AtomOrString::String(string) => string,
_ => {
unreachable!()
}
};
let pstr_loc_cell = step_or_resource_error!(
self.machine_st,
self.machine_st.heap.allocate_pstr(&*atom.as_str())
);
let pstr_loc_cell =
step_or_resource_error!(self.machine_st, self.machine_st.heap.allocate_pstr(&str));
let tail_loc = self.machine_st.heap.cell_len();
let tail_loc = self.machine_st.heap.cell_len();
step_or_resource_error!(
self.machine_st,
self.machine_st.heap.push_cell(heap_loc_as_cell!(tail_loc))
);
step_or_resource_error!(
self.machine_st,
self.machine_st.heap.push_cell(heap_loc_as_cell!(tail_loc))
);
unify!(self.machine_st, self.machine_st.registers[2], pstr_loc_cell);
unify!(self.machine_st, self.machine_st.registers[2], pstr_loc_cell);
if !self.machine_st.fail {
let tail = self.machine_st.registers[3];
unify!(self.machine_st, tail, heap_loc_as_cell!(tail_loc));
if !self.machine_st.fail {
let tail = self.machine_st.registers[3];
unify!(self.machine_st, tail, heap_loc_as_cell!(tail_loc));
}
}
}