Skip to content

Commit

Permalink
Switch bytes struct to resize dynamically rather than enforcing size …
Browse files Browse the repository at this point in the history
…limit
  • Loading branch information
thatstoasty committed Mar 24, 2024
1 parent 0ef31dd commit 69fc09b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
17 changes: 10 additions & 7 deletions gojo/builtins/_bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct Bytes(Stringable, Sized, CollectionElement):
# If the internal vector was resized to a smaller size than what was already written, the write position should be moved back.
if new_size < self.write_position:
self.write_position = new_size

fn available(self) -> Int:
return len(self._vector) - self.write_position

Expand Down Expand Up @@ -118,13 +118,13 @@ struct Bytes(Stringable, Sized, CollectionElement):
return Bytes(new_vector)

fn __iadd__(inout self: Self, other: Self):
# # Up the capacity if the the length of the internal vectors exceeds the current capacity. We are not checking the numbers of bytes written to the Bytes structs.
# var length_of_self = len(self._vector)
# var length_of_other = len(other._vector)
# Up the capacity if the the length of the internal vectors exceeds the current capacity. We are not checking the numbers of bytes written to the Bytes structs.
var length_of_self = len(self)
var length_of_other = len(other)

# var added_size = length_of_self + length_of_other
# if self._vector.capacity < added_size:
# self._vector.reserve(added_size * 2)
var added_size = length_of_self + length_of_other
if self.size() < added_size:
self.resize(added_size * 2)

# Copy over data starting from the write position.
for i in range(len(other)):
Expand All @@ -146,6 +146,9 @@ struct Bytes(Stringable, Sized, CollectionElement):
Args:
value: The value to append.
"""
# If the vector is full, resize it.
if self.write_position == self.size():
self.resize(self.size() * 2)
self[self.write_position] = value

fn extend(inout self, value: String):
Expand Down
6 changes: 3 additions & 3 deletions gojo/bytes/buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ struct Buffer(
# FIXME: It seems like reslicing in go can extend the length of the slice. Doens't work like that for my get slice impl.
# Instead, just add bytes of len(n) to the end of the buffer for now.
# self.buf = self.buf[: l + n]
self.buf += Bytes(n)
self.buf.resize(self.buf.size() + n)
return buffer_already_used, True

return 0, False
Expand Down Expand Up @@ -197,7 +197,7 @@ struct Buffer(
# FIXME: It seems like reslicing in go can extend the length of the slice. Doens't work like that for my get slice impl.
# Instead, just add bytes of len(n) to the end of the buffer for now.
# self.buf = self.buf[: m + n]
self.buf += Bytes(n)
self.buf.resize(self.buf.size() + n)
return write_at

fn Grow(inout self, n: Int):
Expand Down Expand Up @@ -230,7 +230,7 @@ struct Buffer(
write_at, ok = self.try_grow_by_reslice(len(src))
if not ok:
write_at = self.grow(len(src))

return Result(copy(self.buf, src, write_at), None)

fn write_string(inout self, src: String) -> Result[Int]:
Expand Down
2 changes: 1 addition & 1 deletion gojo/strings/builder.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct StringBuilder(Stringable, Sized, io.Writer, io.ByteWriter, io.StringWrite
string if the string builder is empty.
"""
# Don't need to add a null terminator because we can pass the length of the string.
return StringRef(self._vector._vector.data.value, len(self._vector))
return str(self._vector)

fn write(inout self, src: Bytes) -> Result[Int]:
"""
Expand Down
11 changes: 7 additions & 4 deletions tests/test_performance.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from goodies import STDWriter


fn test_string_builder() raises:
print("Testing string concatenation performance")
print("Testing string builder performance")
# Create a string from the buffer
var builder = StringBuilder()
for i in range(100):
Expand All @@ -24,6 +24,7 @@ fn test_string_builder() raises:
var builder_execution_time = now() - builder_start_time

# Create a string using the + operator
print("Testing string concatenation performance")
var vec = DynamicVector[String]()
for i in range(100):
vec.push_back(
Expand All @@ -43,6 +44,7 @@ fn test_string_builder() raises:
var concat_execution_time = now() - concat_start_time

# Create a string using a bytes buffer
print("Testing bytes buffer performance")
var buf = buffer.new_buffer()
for i in range(100):
_ = buf.write_string(
Expand All @@ -57,11 +59,12 @@ fn test_string_builder() raises:

var buffer_start_time = now()
var buffer_output = str(buf)
print(len(buffer_output))
var buffer_execution_time = now() - buffer_start_time

print("StringBuilder: ", "(", builder_execution_time, "ns)")
print("String +: ", "(", concat_execution_time, "ns)")
print("Bytes Buffer: ", "(", buffer_execution_time, "ns)")
print("StringBuilder:", "(", builder_execution_time, "ns)")
print("String concat:", "(", concat_execution_time, "ns)")
print("Bytes Buffer:", "(", buffer_execution_time, "ns)")
print(
"Performance difference: ",
str(concat_execution_time - builder_execution_time) + "ns",
Expand Down

0 comments on commit 69fc09b

Please sign in to comment.