Aamir
Aamir
MModular
Created by Aamir on 3/7/2024 in #questions
Need Opinion! Best Practices for Directory Structure and Unit Testing in Mojo Library?
👍🏼
4 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Just finalized the string builder library! Let me know if anyone of you see any improvement areas! https://github.com/maniartech/mojo-stringbuilder
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Nope, I didn't do that!
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Great, good to hear that!
36 replies
MModular
Created by Aamir on 2/28/2024 in #questions
Mojo Source Code Repository
Just wondering about the priorities related to Unicode support in Mojo? If possible can you share any update on that?
10 replies
MModular
Created by Aamir on 2/28/2024 in #questions
Mojo Source Code Repository
okay
10 replies
MModular
Created by Aamir on 2/28/2024 in #questions
Mojo Source Code Repository
I see. I am developing strings library. And wanted to see the best practices by looking into Strings code! Is there any way I can get access to that part?
10 replies
MModular
Created by wooxus on 2/24/2024 in #questions
Anyone tried Zed with Mojo yet?
@wooxus Zed isn't available on windows which is my preferred OS for software development!
3 replies
MModular
Created by sora on 2/24/2024 in #questions
How to disambiguates the call to `f`?
Something like f[A](s) would be good!
2 replies
MModular
Created by benny on 2/24/2024 in #questions
Traits from structs
I see!
16 replies
MModular
Created by benny on 2/24/2024 in #questions
Traits from structs
Traits are just definitions, not implementations. Abstract class (or types) provides mix of implementation as well as definitions!
16 replies
MModular
Created by benny on 2/24/2024 in #questions
Traits from structs
Is this something related to Abstruct Types found in object object programming languages? https://en.wikipedia.org/wiki/Abstract_type
16 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
I updated the code and benchmarks! It now shows that the StringBuilder is around 10x faster than + concatenation! Code https://github.com/maniartech/mojo-strings/blob/master/strings/builder.mojo Benchmark
fn main():
# Create a string from the buffer
var sb = StringBuilder()

for i in range(100):
sb.append("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.")

let t1 = now()
let s = sb.__str__()
let t1delta = now() - t1

# Create a string using the + operator

var vec = DynamicVector[String]()
for i in range(100):
vec.push_back("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.")

let t2 = now()
var s2: String = ""
for i in range(vec.__len__()):
s2 = s2 + vec[i]
let t2delta = now() - t2

print("StringBuilder: ", "(", t1delta, "ns)")
print("String +: ", "(", t2delta, "ns)")
print("Performance difference: ", str(t2delta - t1delta) + "ns", ": StringBuilder is ", str(t2delta // t1delta) + "x faster")
fn main():
# Create a string from the buffer
var sb = StringBuilder()

for i in range(100):
sb.append("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.")

let t1 = now()
let s = sb.__str__()
let t1delta = now() - t1

# Create a string using the + operator

var vec = DynamicVector[String]()
for i in range(100):
vec.push_back("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.")

let t2 = now()
var s2: String = ""
for i in range(vec.__len__()):
s2 = s2 + vec[i]
let t2delta = now() - t2

print("StringBuilder: ", "(", t1delta, "ns)")
print("String +: ", "(", t2delta, "ns)")
print("Performance difference: ", str(t2delta - t1delta) + "ns", ": StringBuilder is ", str(t2delta // t1delta) + "x faster")
Benchmark Result
StringBuilder: ( 58589 ns)
String +: ( 611619 ns)
Performance difference: 553030ns : StringBuilder is 10x faster
StringBuilder: ( 58589 ns)
String +: ( 611619 ns)
Performance difference: 553030ns : StringBuilder is 10x faster
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
What do you mean by "specializing by conformance", can you elaborate? I am not getting the context!
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
That means, calling through __str__ poses significant overhead
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
When I moved to benchmark code into the StringBuilder.__str__ function. I got this result!
StringBuilder: mojojojokokoloko ( 361 ns)
String +: mojojojokokoloko ( 772 ns)
StringBuilder: mojojojokokoloko ( 361 ns)
String +: mojojojokokoloko ( 772 ns)
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Also, when I benchmarked this code against concatenation using + operator. It failed significantly! Benchmark Code
fn main():
# Create a string from the buffer
let t1 = now()
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
sb.append("koko")
sb.append("loko")
_ = str(sb)
let t1delta = now() - t1

# Create a string using the + operator

let t2 = now()
var vec = DynamicVector[String]()
vec.push_back("mojo")
vec.push_back("jojo")
vec.push_back("koko")
vec.push_back("loko")
var s2: String = ""
for i in range(vec.__len__()):
s2 = s2 + vec[i]
let t2delta = now() - t2

print("StringBuilder: ", " (", t1delta, "ns)")
print("String +: ", " (", t2delta, "ns)")
fn main():
# Create a string from the buffer
let t1 = now()
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
sb.append("koko")
sb.append("loko")
_ = str(sb)
let t1delta = now() - t1

# Create a string using the + operator

let t2 = now()
var vec = DynamicVector[String]()
vec.push_back("mojo")
vec.push_back("jojo")
vec.push_back("koko")
vec.push_back("loko")
var s2: String = ""
for i in range(vec.__len__()):
s2 = s2 + vec[i]
let t2delta = now() - t2

print("StringBuilder: ", " (", t1delta, "ns)")
print("String +: ", " (", t2delta, "ns)")
Result
StringBuilder: ( 9148 ns)
String +: ( 661 ns)
StringBuilder: ( 9148 ns)
String +: ( 661 ns)
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Still there are some questions to be answered to craft the perfect solution. Here are some of the points that I am considering! * How does DynamicVector work? One of the area I am concerned about memory resize when new strings are pushed to the StringBuilder. If DynamicVector is not right data structure for this purpose, we can alternatively utilize resizable Buffer. That will give good control over the strategy we employ for memory resizing! * My second concern is how to handle various String types. StringLiteral, String, StringRef etc... What about Stringable?
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
Wow! It works!
struct StringBuilder(Stringable):
var _strings: DynamicVector[String]

fn __init__(inout self:StringBuilder):
self._strings = DynamicVector[String]()

fn __str__(self:StringBuilder) -> String:

let vec = self._strings
var length = 0
for i in range(len(vec)):
length += len(vec[i])

var ptr = DTypePointer[DType.int8].alloc(length + 1)
var offset = 0
for i in range(len(vec)):
let tmp = __get_address_as_lvalue((vec.data + i).value)
# Copy the string into the buffer at the offset
memcpy(ptr.offset(offset), tmp._as_ptr(), len(tmp))
offset += len(tmp)

ptr.store(offset, 0) # Null terminate the string
return StringRef(ptr, length + 1)

fn append(inout self:StringBuilder, s: String):
self._strings.push_back(s)


fn main():
# Create a string from the buffer
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
print(sb) # Prints mojojojo
struct StringBuilder(Stringable):
var _strings: DynamicVector[String]

fn __init__(inout self:StringBuilder):
self._strings = DynamicVector[String]()

fn __str__(self:StringBuilder) -> String:

let vec = self._strings
var length = 0
for i in range(len(vec)):
length += len(vec[i])

var ptr = DTypePointer[DType.int8].alloc(length + 1)
var offset = 0
for i in range(len(vec)):
let tmp = __get_address_as_lvalue((vec.data + i).value)
# Copy the string into the buffer at the offset
memcpy(ptr.offset(offset), tmp._as_ptr(), len(tmp))
offset += len(tmp)

ptr.store(offset, 0) # Null terminate the string
return StringRef(ptr, length + 1)

fn append(inout self:StringBuilder, s: String):
self._strings.push_back(s)


fn main():
# Create a string from the buffer
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
print(sb) # Prints mojojojo
36 replies
MModular
Created by Aamir on 2/23/2024 in #questions
Fastest way to build string from dynamic values!
I don't think that's the case! Here is whole program derived from your contact_str function. Or maybe I am not able to see it!
struct StringBuilder(Stringable):
var _strings: DynamicVector[String]

fn __init__(inout self:StringBuilder):
self._strings = DynamicVector[String]()

fn __str__(self:StringBuilder) -> String:

let vec = self._strings
var length = 0
for i in range(len(vec)):
length += len(vec[i])

var ptr = DTypePointer[DType.int8].alloc(length + 1)
var offset = 0
for i in range(len(vec)):
# Debug and pritn by converting the value to StringRef using pointer!
print(StringRef(vec[i]._as_ptr(), len(vec[i]))) # Prints junk

# Copy the string into the buffer at the offset
memcpy(ptr.offset(offset), vec[i]._as_ptr(), len(vec[i]))
offset += len(vec[i])

ptr.store(offset, 0) # Null terminate the string
return StringRef(ptr, length + 1) # Returs the junk strig

fn append(inout self:StringBuilder, s: String):
self._strings.push_back(s)


fn main():
# Create a string from the buffer
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
print(sb) # Prints junk
struct StringBuilder(Stringable):
var _strings: DynamicVector[String]

fn __init__(inout self:StringBuilder):
self._strings = DynamicVector[String]()

fn __str__(self:StringBuilder) -> String:

let vec = self._strings
var length = 0
for i in range(len(vec)):
length += len(vec[i])

var ptr = DTypePointer[DType.int8].alloc(length + 1)
var offset = 0
for i in range(len(vec)):
# Debug and pritn by converting the value to StringRef using pointer!
print(StringRef(vec[i]._as_ptr(), len(vec[i]))) # Prints junk

# Copy the string into the buffer at the offset
memcpy(ptr.offset(offset), vec[i]._as_ptr(), len(vec[i]))
offset += len(vec[i])

ptr.store(offset, 0) # Null terminate the string
return StringRef(ptr, length + 1) # Returs the junk strig

fn append(inout self:StringBuilder, s: String):
self._strings.push_back(s)


fn main():
# Create a string from the buffer
var sb = StringBuilder()
sb.append("mojo")
sb.append("jojo")
print(sb) # Prints junk
36 replies