]> Repositorios git - scryer-prolog.git/commitdiff
avoid writing chars to the atom table in several places
authorMark Thom <[email protected]>
Sun, 17 Apr 2022 01:57:26 +0000 (19:57 -0600)
committerMark Thom <[email protected]>
Sun, 17 Apr 2022 18:20:00 +0000 (12:20 -0600)
src/forms.rs
src/heap_print.rs
src/machine/system_calls.rs
src/parser/char_reader.rs

index f1c634bc16c090f1c5cf209dc52f6a67c855c73b..e183a522768582f013cc91f2a04840ecc4312e02 100644 (file)
@@ -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 {
index 4592e50d1d0d9a1efcf7b61d2ddcf978cbf248b5..2aa5cd338caf923712227e90a17874b80a0a2125 100644 (file)
@@ -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
index 6e434782014a6ab01c3e47951474e59bb80cc145..3d27e93a36bf3bc6a3711b16aa804ee766305f06 100644 (file)
@@ -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,
index 9c23371e0cfd4aa1701e4754e9053baa64de0698..dc4d277d5d7d87d345187282e35dcf69192511ed 100644 (file)
@@ -246,33 +246,6 @@ impl<R: Read> CharRead for CharReader<R> {
     }
 }
 
-/*
-impl<R: Seek> BufReader<R> {
-    /// 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<R: Read> Read for CharReader<R> {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
         // // 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<R: Seek> Seek for BufReader<R> {
-    /// 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<R>` 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<u64> {
-        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<u64> {
-        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<T> SizeHint for CharReader<T> {
-    fn lower_bound(&self) -> usize {
-        self.buffer().len()
-    }
-}
-*/
 
 #[cfg(test)]
 mod tests {