]> Repositorios git - scryer-prolog.git/commitdiff
ENHANCED: Read more characters at once, significantly speeding up library(pio).
authorMarkus Triska <[email protected]>
Fri, 12 Jun 2020 15:29:18 +0000 (17:29 +0200)
committerMarkus Triska <[email protected]>
Sat, 13 Jun 2020 14:25:41 +0000 (16:25 +0200)
src/clause_types.rs
src/lib/pio.pl
src/machine/system_calls.rs

index bc8a69eaac2895343389baa7489c92d055db1fc1..58a0bae3d519d1f696a33be06170cd52e5e9c2b4 100644 (file)
@@ -184,6 +184,7 @@ pub enum SystemClauseType {
     FlushOutput,
     GetByte,
     GetChar,
+    GetNChars,
     GetCode,
     GetSingleChar,
     ResetAttrVarState,
@@ -353,6 +354,7 @@ impl SystemClauseType {
             &SystemClauseType::FlushOutput => clause_name!("$flush_output"),
             &SystemClauseType::GetByte => clause_name!("$get_byte"),
             &SystemClauseType::GetChar => clause_name!("$get_char"),
+            &SystemClauseType::GetNChars => clause_name!("$get_n_chars"),
             &SystemClauseType::GetCode => clause_name!("$get_code"),
             &SystemClauseType::GetSingleChar => clause_name!("$get_single_char"),
             &SystemClauseType::ResetAttrVarState => clause_name!("$reset_attr_var_state"),
@@ -549,6 +551,7 @@ impl SystemClauseType {
             ("$fetch_global_var_with_offset", 3) => Some(SystemClauseType::FetchGlobalVarWithOffset),
             ("$get_byte", 2) => Some(SystemClauseType::GetByte),
             ("$get_char", 2) => Some(SystemClauseType::GetChar),
+            ("$get_n_chars", 3) => Some(SystemClauseType::GetNChars),
             ("$get_code", 2) => Some(SystemClauseType::GetCode),
             ("$get_single_char", 1) => Some(SystemClauseType::GetSingleChar),
             ("$points_to_cont_reset_marker", 1) => {
index 4132752b97f7fa76df47e3b670ec382883ec4e9d..89dfc5668a82c2e8fe9a0571d8e4c91dbc13fc00 100644 (file)
@@ -22,67 +22,21 @@ phrase_from_file(NT, File, Options) :-
         ;   Type = text
         ),
         setup_call_cleanup(open(File, read, Stream, [reposition(true)|Options]),
-                           (   stream_to_lazy_list(pio:Type, Stream, Xs),
+                           (   stream_to_lazy_list(Stream, Xs),
                                phrase(NT, Xs) ),
                            close(Stream))
    ).
 
 
-stream_to_lazy_list(Type_3, Stream, Xs) :-
+stream_to_lazy_list(Stream, Xs) :-
         stream_property(Stream, position(Pos)),
-        freeze(Xs, reader_step(Type_3, Stream, Pos, Xs)).
+        freeze(Xs, reader_step(Stream, Pos, Xs)).
 
-reader_step(Type_3, Stream, Pos, Xs0) :-
+reader_step(Stream, Pos, Xs0) :-
         set_stream_position(Stream, Pos),
         (   at_end_of_stream(Stream)
         ->  Xs0 = []
-        ;   % phrase(call(call(Type_3,Stream)), Xs0,Xs), % conforming call
-            call(Type_3, Stream, Cs,[]), % effective call
+        ;   '$get_n_chars'(Stream, 4096, Cs),
             partial_string(Cs, Xs0, Xs),
-            stream_to_lazy_list(Type_3, Stream, Xs)
+            stream_to_lazy_list(Stream, Xs)
         ).
-
-binary(Stream, Xs0, Xs) :- get_pending_bytes(Stream, Xs0, Xs).
-text(Stream, Xs0, Xs)   :- get_pending_chars(Stream, Xs0, Xs).
-
-
-get_pending_chars(Stream, Chs0,Chs) :-
-        n_get_chars(4096, Stream, Chs, Chs0,Chs).
-
-% EOF means: If EOF == [], then EOF has definitely been reached, otherwise
-% it is unknown and the argument remains uninstantiated.
-
-% To improve performance, the following predicates should be replaced
-% by a fast Rust implementation that reads a number of characters (or
-% bytes) at once.
-
-% Files that do not contain 0-bytes can even be mmapped to memory.
-
-n_get_chars(N0, Stream, EOF, Chs0,Chs) :-
-        N0 > 0,
-        N1 is N0-1,
-        get_char(Stream, Ch),
-        (   Ch == end_of_file
-        ->  Chs0 = Chs,
-            EOF = []
-        ;   Chs0 = [Ch|Chs1],
-            n_get_chars(N1, Stream, EOF, Chs1,Chs)
-        ).
-n_get_chars(0, _, _, Chs,Chs).
-
-
-get_pending_bytes(Stream, Chs0,Chs) :-
-        n_get_bytes(4096, Stream, Chs, Chs0,Chs).
-
-n_get_bytes(N0, Stream, EOF, Chs0,Chs) :-
-        N0 > 0,
-        N1 is N0-1,
-        get_byte(Stream, Byte),
-        (   Byte == -1
-        ->  Chs0 = Chs,
-            EOF = []
-        ;   char_code(Ch, Byte),
-            Chs0 = [Ch|Chs1],
-            n_get_bytes(N1, Stream, EOF, Chs1,Chs)
-        ).
-n_get_bytes(0, _, _, Chs,Chs).
index 2ff4b7d939e1e6a085419f16b9b7c74d4049cf93..7a05049ffab0b0f69ffcc7bfc85861abb26d7f59 100644 (file)
@@ -2256,6 +2256,64 @@ impl MachineState {
                     }
                 }
             }
+            &SystemClauseType::GetNChars => {
+                let stream =
+                    self.get_stream_or_alias(self[temp_v!(1)], indices, "get_n_chars", 3)?;
+
+                let num =
+                    match Number::try_from((self[temp_v!(2)], &self.heap)) {
+                        Ok(Number::Fixnum(n)) => {
+                            usize::try_from(n).unwrap()
+                        }
+                        Ok(Number::Integer(n)) => {
+                            match n.to_usize() {
+                                Some(u) => { u }
+                                _ => { self.fail = true; return Ok(()); }
+                            }
+                        }
+                        _ => { unreachable!() }
+                    };
+
+                let chars = {
+                   let mut str = String::new();
+
+                   if stream.options.stream_type == StreamType::Binary {
+                        let mut mstream = stream.clone();
+                        for _ in 0..num {
+                            let mut b = [0u8; 1];
+
+                            match mstream.read(&mut b) {
+                                Ok(1) => {
+                                    str.push(b[0] as char);
+                                }
+                                _ => {
+                                    break;
+                                }
+                            }
+                        }
+                    } else {
+                        let mut iter = self.open_parsing_stream(stream.clone(),
+                                                                "get_n_chars",
+                                                                2,
+                                                                )?;
+
+                        for _ in 0..num {
+                             let result = iter.next();
+
+                             match result {
+                                 Some(Ok(c)) => {
+                                    str.push(c);
+                                 }
+                                 _ => { break;
+                                 }
+                              }
+                         }
+                    }
+                    str
+                };
+                let str = self.heap.put_complete_string(&chars);
+                self.unify(self[temp_v!(3)], str);
+            }
             &SystemClauseType::GetCode => {
                 let mut stream =
                     self.get_stream_or_alias(self[temp_v!(1)], indices, "get_code", 2)?;