From 2d5fe21b05f00bf4af1fd089ffa513d95386b421 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sat, 16 Apr 2022 19:57:26 -0600 Subject: [PATCH] avoid writing chars to the atom table in several places --- src/forms.rs | 12 ++++ src/heap_print.rs | 3 +- src/machine/system_calls.rs | 47 +++++-------- src/parser/char_reader.rs | 129 ------------------------------------ 4 files changed, 31 insertions(+), 160 deletions(-) diff --git a/src/forms.rs b/src/forms.rs index f1c634bc..e183a522 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -364,6 +364,18 @@ pub enum AtomOrString { } impl AtomOrString { + #[inline] + pub fn as_atom(&self, atom_tbl: &mut AtomTable) -> Atom { + match self { + &AtomOrString::Atom(atom) => { + atom + } + AtomOrString::String(string) => { + atom_tbl.build_with(&string) + } + } + } + #[inline] pub fn as_str(&self) -> &str { match self { diff --git a/src/heap_print.rs b/src/heap_print.rs index 4592e50d..2aa5cd33 100644 --- a/src/heap_print.rs +++ b/src/heap_print.rs @@ -737,8 +737,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter> { } self.state_stack.push(TokenOrRedirect::RightCurly); - self.state_stack - .push(TokenOrRedirect::FunctorRedirect(max_depth)); + self.state_stack.push(TokenOrRedirect::FunctorRedirect(max_depth)); self.state_stack.push(TokenOrRedirect::LeftCurly); true diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 6e434782..3d27e93a 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -703,7 +703,7 @@ impl MachineState { pub(crate) fn parse_number_from_string( &mut self, - mut string: String, + string: &str, indices: &IndexStore, stub_gen: impl Fn() -> FunctorStub, ) -> CallResult { @@ -725,10 +725,13 @@ impl MachineState { } } - string.push('.'); + let mut dot_buf: [u8; '.'.len_utf8()] = [0u8]; + '.'.encode_utf8(&mut dot_buf); - let stream = CharReader::new(std::io::Cursor::new(string)); - let mut parser = Parser::new(stream, self); + let cursor = std::io::Cursor::new(string); + let iter = std::io::Read::chain(cursor, std::io::Cursor::new(dot_buf)); + + let mut parser = Parser::new(CharReader::new(iter), self); match parser.read_term(&CompositeOpDir::new(&indices.op_dir, None)) { Err(err) => { @@ -816,7 +819,7 @@ impl MachineState { } } (HeapCellValueTag::Char, c) => { - Some(AtomOrString::Atom(self.atom_tbl.build_with(&c.to_string()))) + Some(AtomOrString::String(c.to_string())) } _ => { if value.is_constant() { @@ -1292,20 +1295,20 @@ impl Machine { let a2 = self.machine_st.store(self.machine_st.deref(self.machine_st.registers[2])); if let Some(str_like) = self.machine_st.value_to_str_like(a2) { - let atom = match str_like { + let atom_cell = match str_like { AtomOrString::Atom(atom) => { - if atom == atom!("[]") { + atom_as_cell!(if atom == atom!("[]") { self.machine_st.atom_tbl.build_with("") } else { atom - } + }) } AtomOrString::String(string) => { - self.machine_st.atom_tbl.build_with(&string) + atom_as_cell!(self.machine_st.atom_tbl.build_with(&string)) } }; - self.machine_st.bind(a1.as_var().unwrap(), atom_as_cell!(atom)); + self.machine_st.bind(a1.as_var().unwrap(), atom_cell); return; } @@ -1423,7 +1426,7 @@ impl Machine { if let Some(atom_or_string) = self.machine_st.value_to_str_like(a1) { self.machine_st.parse_number_from_string( - atom_or_string.to_string(), + atom_or_string.as_str(), &self.indices, stub_gen, )?; @@ -1887,7 +1890,7 @@ impl Machine { } Ok(addrs) => { let string = self.machine_st.codes_to_string(addrs.into_iter(), stub_gen)?; - self.machine_st.parse_number_from_string(string, &self.indices, stub_gen)?; + self.machine_st.parse_number_from_string(string.as_str(), &self.indices, stub_gen)?; } } @@ -3317,14 +3320,7 @@ impl Machine { Err(e) => return Err(e) }; if let Some(address_sink) = self.machine_st.value_to_str_like(address_sink) { - let address_string = match address_sink { - AtomOrString::Atom(atom) => { - String::from(atom.as_str()) - } - AtomOrString::String(string) => { - String::from(string.as_str()) - } - }; + let address_string = address_sink.as_str(); //to_string(); let address: Uri = address_string.parse().unwrap(); let stream = self.runtime.block_on(async { @@ -3408,15 +3404,8 @@ impl Machine { let options = self.machine_st.to_stream_options(alias, eof_action, reposition, stream_type); let src_sink = self.machine_st.store(self.machine_st.deref(self.machine_st.registers[1])); - if let Some(str_like) = self.machine_st.value_to_str_like(src_sink) { - let file_spec = match str_like { - AtomOrString::Atom(atom) => { - atom - } - AtomOrString::String(string) => { - self.machine_st.atom_tbl.build_with(&string) - } - }; + if let Some(file_spec) = self.machine_st.value_to_str_like(src_sink) { + let file_spec = file_spec.as_atom(&mut self.machine_st.atom_tbl); let mut stream = self.machine_st.stream_from_file_spec( file_spec, diff --git a/src/parser/char_reader.rs b/src/parser/char_reader.rs index 9c23371e..dc4d277d 100644 --- a/src/parser/char_reader.rs +++ b/src/parser/char_reader.rs @@ -246,33 +246,6 @@ impl CharRead for CharReader { } } -/* -impl BufReader { - /// Seeks relative to the current position. If the new position lies within the buffer, - /// the buffer will not be flushed, allowing for more efficient seeks. - /// This method does not return the location of the underlying reader, so the caller - /// must track this information themselves if it is required. - #[stable(feature = "bufreader_seek_relative", since = "1.53.0")] - pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> { - let pos = self.pos as u64; - if offset < 0 { - if let Some(new_pos) = pos.checked_sub((-offset) as u64) { - self.pos = new_pos as usize; - return Ok(()); - } - } else { - if let Some(new_pos) = pos.checked_add(offset as u64) { - if new_pos <= self.cap as u64 { - self.pos = new_pos as usize; - return Ok(()); - } - } - } - self.seek(SeekFrom::Current(offset)).map(drop) - } -} -*/ - impl Read for CharReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { // // If we don't have any buffered data and we're doing a massive read @@ -377,108 +350,6 @@ where } } -/* -#[stable(feature = "rust1", since = "1.0.0")] -impl Seek for BufReader { - /// Seek to an offset, in bytes, in the underlying reader. - /// - /// The position used for seeking with [`SeekFrom::Current`]`(_)` is the - /// position the underlying reader would be at if the `BufReader` had no - /// internal buffer. - /// - /// Seeking always discards the internal buffer, even if the seek position - /// would otherwise fall within it. This guarantees that calling - /// [`BufReader::into_inner()`] immediately after a seek yields the underlying reader - /// at the same position. - /// - /// To seek without discarding the internal buffer, use [`BufReader::seek_relative`]. - /// - /// See [`std::io::Seek`] for more details. - /// - /// Note: In the edge case where you're seeking with [`SeekFrom::Current`]`(n)` - /// where `n` minus the internal buffer length overflows an `i64`, two - /// seeks will be performed instead of one. If the second seek returns - /// [`Err`], the underlying reader will be left at the same position it would - /// have if you called `seek` with [`SeekFrom::Current`]`(0)`. - /// - /// [`std::io::Seek`]: Seek - fn seek(&mut self, pos: SeekFrom) -> io::Result { - let result: u64; - if let SeekFrom::Current(n) = pos { - let remainder = (self.cap - self.pos) as i64; - // it should be safe to assume that remainder fits within an i64 as the alternative - // means we managed to allocate 8 exbibytes and that's absurd. - // But it's not out of the realm of possibility for some weird underlying reader to - // support seeking by i64::MIN so we need to handle underflow when subtracting - // remainder. - if let Some(offset) = n.checked_sub(remainder) { - result = self.inner.seek(SeekFrom::Current(offset))?; - } else { - // seek backwards by our remainder, and then by the offset - self.inner.seek(SeekFrom::Current(-remainder))?; - self.discard_buffer(); - result = self.inner.seek(SeekFrom::Current(n))?; - } - } else { - // Seeking with Start/End doesn't care about our buffer length. - result = self.inner.seek(pos)?; - } - self.discard_buffer(); - Ok(result) - } - - /// Returns the current seek position from the start of the stream. - /// - /// The value returned is equivalent to `self.seek(SeekFrom::Current(0))` - /// but does not flush the internal buffer. Due to this optimization the - /// function does not guarantee that calling `.into_inner()` immediately - /// afterwards will yield the underlying reader at the same position. Use - /// [`BufReader::seek`] instead if you require that guarantee. - /// - /// # Panics - /// - /// This function will panic if the position of the inner reader is smaller - /// than the amount of buffered data. That can happen if the inner reader - /// has an incorrect implementation of [`Seek::stream_position`], or if the - /// position has gone out of sync due to calling [`Seek::seek`] directly on - /// the underlying reader. - /// - /// # Example - /// - /// ```no_run - /// use std::{ - /// io::{self, BufRead, BufReader, Seek}, - /// fs::File, - /// }; - /// - /// fn main() -> io::Result<()> { - /// let mut f = BufReader::new(File::open("foo.txt")?); - /// - /// let before = f.stream_position()?; - /// f.read_line(&mut String::new())?; - /// let after = f.stream_position()?; - /// - /// println!("The first line was {} bytes long", after - before); - /// Ok(()) - /// } - /// ``` - fn stream_position(&mut self) -> io::Result { - let remainder = (self.cap - self.pos) as u64; - self.inner.stream_position().map(|pos| { - pos.checked_sub(remainder).expect( - "overflow when subtracting remaining buffer size from inner stream position", - ) - }) - } -} -*/ -/* -impl SizeHint for CharReader { - fn lower_bound(&self) -> usize { - self.buffer().len() - } -} -*/ #[cfg(test)] mod tests { -- 2.54.0