Skip to content

Commit

Permalink
update changlog
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Sep 21, 2024
1 parent e3b9fad commit 35b2e5f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - yyyy-mm-dd

## [0.1.10] - 2024-09-21

- Add `consume()` to `StringBuilder` and `Buffer` to take ownership of the internal buffer instead of copying it.

## [0.1.9] - 2024-09-13

- Fix usage of abort instead of panic.
Expand Down
39 changes: 21 additions & 18 deletions benchmarks/string_builder.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import benchmark
import pathlib
import time
from gojo.strings import StringBuilder
from gojo.bytes.buffer import Buffer

alias SAMPLE_TEXT = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""

Expand All @@ -23,14 +24,25 @@ fn benchmark_string_builder[batches: Int]():
_ = str(new_builder)


fn benchmark_bytes_buffer[batches: Int]():
var buffer = Buffer(capacity=batches * len(SAMPLE_TEXT))
for _ in range(batches):
_ = buffer.write_string(SAMPLE_TEXT)
_ = str(buffer)
fn benchmark_consume_and_str() raises:
var builder = StringBuilder()
var path = str(pathlib._dir_of_current_file()) + "/data/test_big_file.txt"
with open(path, "r") as file:
var data = file.read()
for _ in range(10):
_ = builder.write_string(data)

var start = time.perf_counter_ns()
var result = str(builder)
print("Stringify buffer: ", time.perf_counter_ns() - start)

start = time.perf_counter_ns()
result = builder.consume()
print("Consume buffer: ", time.perf_counter_ns() - start)
_ = result

fn main():

fn main() raises:
# There's a performance penalty for benchmark concat bc it also includes
# the building of the list of strings it concatenates. Trying to build it at comptime takes a loooong time.
print("Running benchmark_concat - 100 batches")
Expand All @@ -41,10 +53,6 @@ fn main():
report = benchmark.run[benchmark_string_builder[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 100 batches")
report = benchmark.run[benchmark_bytes_buffer[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 1000 batches")
report = benchmark.run[benchmark_concat[1000]](max_iters=20)
report.print(benchmark.Unit.ms)
Expand All @@ -53,10 +61,6 @@ fn main():
report = benchmark.run[benchmark_string_builder[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 1000 batches")
report = benchmark.run[benchmark_bytes_buffer[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 10000 batches")
report = benchmark.run[benchmark_concat[10000]](max_iters=2)
report.print(benchmark.Unit.ms)
Expand All @@ -65,6 +69,5 @@ fn main():
report = benchmark.run[benchmark_string_builder[10000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 10000 batches")
report = benchmark.run[benchmark_bytes_buffer[10000]](max_iters=20)
report.print(benchmark.Unit.ms)
print("Running benchmark_consume_and_str")
benchmark_consume_and_str()
2 changes: 1 addition & 1 deletion mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels = ["conda-forge", "https://conda.modular.com/max"]
description = "Experiments in porting over Golang stdlib into Mojo."
name = "gojo"
platforms = ["osx-arm64", "linux-64"]
version = "0.1.9"
version = "0.1.10"

[tasks]
tests = "bash scripts/tests.sh"
Expand Down
14 changes: 8 additions & 6 deletions src/gojo/bytes/buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,17 @@ struct Buffer(
"""
return self.as_string_slice()

@deprecated("Buffer.render() has been deprecated. Use Buffer.as_string_slice() or call str() instead.")
fn render(self) -> String:
fn consume(inout self) -> String:
"""
Return a StringSlice view of the data owned by the builder.
Transfers the buffer's data to a string and resets the buffer. Effectively consuming the Buffer.
Returns:
The string representation of the string builder. Returns an empty string if the string builder is empty.
The string representation of the buffer. Returns an empty string if the buffer is empty.
"""
return self.as_string_slice()
var result = String(self._data, self._size)
self._data = UnsafePointer[UInt8]()
self._size = 0
return result

fn write(inout self, src: Span[UInt8]) -> (Int, Error):
"""
Expand Down Expand Up @@ -280,7 +282,7 @@ struct Buffer(
self.last_read = OP_INVALID

fn _read(inout self, inout dest: UnsafePointer[UInt8], capacity: Int) -> (Int, Error):
"""Reads the next len(dest) bytes from the buffer or until the buffer
"""Reads the next `len(dest)` bytes from the buffer or until the buffer
is drained. The return value `bytes_read` is the number of bytes read.
If the buffer has no data to return, err is `io.EOF` (unless `len(dest)` is zero);
Expand Down
15 changes: 8 additions & 7 deletions src/gojo/strings/builder.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,17 @@ struct StringBuilder[growth_factor: Float32 = 2](
"""
return self.as_string_slice()

@deprecated(
"StringBuilder.render() has been deprecated. Use StringBuilder.as_string_slice() or call str() instead."
)
fn render(ref [_]self) -> String:
"""Return a StringSlice view of the data owned by the builder.
fn consume(inout self) -> String:
"""
Transfers the string builder's data to a string and resets the string builder. Effectively consuming the string builder.
Returns:
The string representation of the string builder. Returns an empty string if the string builder is empty.
The string representation of the string builder. Returns an empty string if the buffer is empty.
"""
return self.as_string_slice()
var result = String(self._data, self._size)
self._data = UnsafePointer[UInt8]()
self._size = 0
return result

fn _resize(inout self, capacity: Int) -> None:
"""Resizes the string builder buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ context:

package:
name: "gojo"
version: 0.1.9
version: 0.1.10

source:
- path: .
Expand Down

0 comments on commit 35b2e5f

Please sign in to comment.