-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib] Faster repr() logic for String, StringRef, UInt, DType and others #3736
Changes from all commits
19b23a1
950d36a
c6c9277
f1472b9
3fbb842
3c2129f
91f0b80
db15abe
348b434
97a7a96
4b67bfc
40e2787
696339c
fb93c6b
7e4da67
3ab5ef0
32a507d
1ee14b1
cb66ec9
958106b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ fn _constrain_unix(): | |
|
||
|
||
@value | ||
struct stat_result(Stringable): | ||
struct stat_result(Stringable, Writable): | ||
"""Object whose fields correspond to the members of the stat structure.""" | ||
|
||
var st_mode: Int | ||
|
@@ -150,30 +150,43 @@ struct stat_result(Stringable): | |
self.st_rdev = st_rdev | ||
self.st_flags = st_flags | ||
|
||
@no_inline | ||
fn write_to[W: Writer](self, inout writer: W): | ||
""" | ||
Formats this path to the provided Writer. | ||
|
||
Parameters: | ||
W: A type conforming to the Writable trait. | ||
|
||
Args: | ||
writer: The object to write to. | ||
""" | ||
writer.write("os.stat_result(") | ||
writer.write("st_mode=", self.st_mode) | ||
writer.write(", st_ino=", self.st_ino) | ||
writer.write(", st_dev=", self.st_dev) | ||
writer.write(", st_nlink=", self.st_nlink) | ||
writer.write(", st_uid=", self.st_uid) | ||
writer.write(", st_gid=", self.st_gid) | ||
writer.write(", st_size=", self.st_size) | ||
writer.write(", st_atime=", str(self.st_atimespec)) | ||
writer.write(", st_mtime=", str(self.st_mtimespec)) | ||
writer.write(", st_ctime=", str(self.st_ctimespec)) | ||
writer.write(", st_birthtime=", str(self.st_birthtimespec)) | ||
writer.write(", st_blocks=", self.st_blocks) | ||
writer.write(", st_blksize=", self.st_blksize) | ||
writer.write(", st_rdev=", self.st_rdev) | ||
writer.write(", st_flags=", self.st_flags) | ||
writer.write(")") | ||
Comment on lines
+164
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: these can all go in the same function call separated by commas. They will be formatted similarly by the Mojo formatter so it won't make much difference in the aesthetics other than the name of the field won't be directly to the left, still it's just a matter of personal preference and not performance currently 🤷♂️ . (This would all become redundant if somebody reserves enough capacity for the whole |
||
|
||
@no_inline | ||
fn __str__(self) -> String: | ||
"""Constructs a string representation of stat_result. | ||
|
||
Returns: | ||
A string representation of stat_result. | ||
""" | ||
var res = String("os.stat_result(") | ||
res += "st_mode=" + str(self.st_mode) | ||
res += ", st_ino=" + str(self.st_ino) | ||
res += ", st_dev=" + str(self.st_dev) | ||
res += ", st_nlink=" + str(self.st_nlink) | ||
res += ", st_uid=" + str(self.st_uid) | ||
res += ", st_gid=" + str(self.st_gid) | ||
res += ", st_size=" + str(self.st_size) | ||
res += ", st_atime=" + str(self.st_atimespec) | ||
res += ", st_mtime=" + str(self.st_mtimespec) | ||
res += ", st_ctime=" + str(self.st_ctimespec) | ||
res += ", st_birthtime=" + str(self.st_birthtimespec) | ||
res += ", st_blocks=" + str(self.st_blocks) | ||
res += ", st_blksize=" + str(self.st_blksize) | ||
res += ", st_rdev=" + str(self.st_rdev) | ||
res += ", st_flags=" + str(self.st_flags) | ||
return res + ")" | ||
return String.write(self) | ||
|
||
fn __repr__(self) -> String: | ||
"""Constructs a representation of stat_result. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion Split this change to
stat_result
to beWritable
along with its tests to a separate PR, please.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of this commit was to have a faster representation of
path.stat()
which usesstat_result
underneath, which was already representable but slow, so to me was in the same scope of "Faster repr() logic" msaelices@950d36aDo you still want me to split those changes into a new PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the scope of the commit, but in the future, I'd recommend teasing apart the two ideas (applying
Writable
to a struct and makingrepr()
faster for a few types). They're orthogonal things in my mind and could have been two separate PRs. It's fine for this one though, but something to consider in the future 😄