Skip to main content
Log in

Mojo function

write_buffered

write_buffered[W: Writer, //, *Ts: Writable, *, buffer_size: Int = 4096, use_heap: Bool = False](mut writer: W, args: VariadicPack[origin, Writable, Ts], *, sep: StringSlice[StaticConstantOrigin] = StringSlice(""), end: StringSlice[StaticConstantOrigin] = StringSlice(""))

Use a buffer on the stack to minimize expensive calls to the writer. When the buffer would overflow it writes to the writer passed in. You can also add seperators between the args, and end characters. The default stack space used for the buffer is 4096 bytes which matches the default arm64 and x86-64 page size, you can modify this e.g. when writing a large amount of data to a file.

Example

import sys
from utils import write_buffered

fn print_err_buffered[*Ts: Writable](
*args: *Ts, sep: StringLiteral, end: StringLiteral
):
var stderr = sys.stderr
write_buffered(stdout, args, sep=sep, end=end)

# Buffer before allocating a string
var string = String()
write_buffered(string, args, sep=sep, end=end)


print_err_buffered(3, "total", "args", sep=",", end="[end]")
import sys
from utils import write_buffered

fn print_err_buffered[*Ts: Writable](
*args: *Ts, sep: StringLiteral, end: StringLiteral
):
var stderr = sys.stderr
write_buffered(stdout, args, sep=sep, end=end)

# Buffer before allocating a string
var string = String()
write_buffered(string, args, sep=sep, end=end)


print_err_buffered(3, "total", "args", sep=",", end="[end]")
3, total, args[end]
3, total, args[end]

.

Parameters:

  • W (Writer): The type of the Writer to write to.
  • *Ts (Writable): The types of each arg to write. Each type must satisfy Writable.
  • buffer_size (Int): How many bytes to write to a buffer before writing out to the writer (default 4096).
  • use_heap (Bool): Buffer to the heap, first calculating the total byte size of all the args and then allocating only once. buffer_size is not used in this case as it's dynamically calculated. (default False).

Args:

  • writer (W): The Writer to write to.
  • args (VariadicPack[origin, Writable, Ts]): A VariadicPack of Writable arguments.
  • sep (StringSlice[StaticConstantOrigin]): The separator used between elements.
  • end (StringSlice[StaticConstantOrigin]): The String to write after printing the elements.

write_buffered[W: Writer, T: WritableCollectionElement, //, buffer_size: Int = 4096](mut writer: W, values: List[T, hint_trivial_type], *, sep: StringSlice[StaticConstantOrigin] = StringSlice(""))

Use a buffer on the stack to minimize expensive calls to the writer. You can also add seperators between the values. The default stack space used for the buffer is 4096 bytes which matches the default arm64 and x86-64 page size, you can modify this e.g. when writing a large amount of data to a file.

Example

import sys
from utils import write_buffered

var string = String()
var values = List[String]("3", "total", "args")
write_buffered(string, values, sep=",")
import sys
from utils import write_buffered

var string = String()
var values = List[String]("3", "total", "args")
write_buffered(string, values, sep=",")
3, total, args
3, total, args

.

Parameters:

  • W (Writer): The type of the Writer to write to.
  • T (WritableCollectionElement): The Writable type of the List.
  • buffer_size (Int): How many bytes to write to a buffer before writing out to the writer (default 4096).

Args:

  • writer (W): The Writer to write to.
  • values (List[T, hint_trivial_type]): A List of Writable arguments.
  • sep (StringSlice[StaticConstantOrigin]): The separator used between elements.