4.17 Input and output
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Input and output
          • Predefined stream aliases
          • ISO Input and Output Streams
          • Edinburgh-style I/O
          • Switching between Edinburgh and ISO I/O
          • Adding IRI schemas
          • Write onto atoms, code-lists, etc.
          • Fast binary term I/O
            • fast_term_serialized/2
            • fast_write/2
            • fast_read/2
    • Packages

4.17.7 Fast binary term I/O

The predicates in this section provide fast binary I/O of arbitrary Prolog terms, including cyclic terms and terms holding attributed variables. Library library(fastrw) is a SICSTus/Ciao compatible library that extends the core primitives described below.

The binary representation the same as used by PL_record_external(). The use of these primitives instead of using write_canonical/2 has advantages and disadvantages. Below are the main considerations:

  • Using write_canonical/2 allows or exchange of terms with other Prolog systems. The format is stable and, as it is text based, it can be inspected and corrected.
  • Using the binary format improves the performance roughly 3 times.
  • The size of both representations is comparable.
  • The binary format can deal with cycles, sharing and attributes. Special precautions are needed to transfer such terms using write_canonical/2. See term_factorized/3 and copy_term/3.
  • The data is validated before it is decoded, so reading corrupted or crafted data does not compromise the reading Prolog system. Note however that the data can create arbitrary terms, including cyclic terms, terms with attributed variables and large terms. The cost of the validation is between 10% and 50% of decoding time, depending on the size of the term.
fast_term_serialized(?Term, ?String)
(De-)serialize Term to/from String. When deserializing, this predicate fails silently if String is not a valid serialization.
fast_write(+Output, +Term)
Write Term using the fast serialization format to the Output stream. Output must be a binary stream.
fast_read(+Input, -Term)
Read Term using the fast serialization format from the Input stream. Input must be a binary stream. Raises a syntax error if the input is not a valid serialization.