-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Use std.Buffer even less #4665
Use std.Buffer even less #4665
Conversation
ce18696
to
fe69332
Compare
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.
Thanks, I do think these are some nice changes. Some adjustments to make and then it's good to merge.
@@ -967,23 +967,23 @@ pub const Target = struct { | |||
|
|||
pub const stack_align = 16; | |||
|
|||
pub fn zigTriple(self: Target, allocator: *mem.Allocator) ![:0]u8 { | |||
pub fn zigTriple(self: Target, allocator: *mem.Allocator) ![]u8 { |
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.
👍 These I agree to change
823b73f
to
87a8e15
Compare
87a8e15
to
71bdb2d
Compare
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.
almost there
cache_str(ch, g->libc->include_dir); | ||
cache_str(ch, g->libc->sys_include_dir); | ||
cache_str(ch, g->libc->crt_dir); | ||
cache_str(ch, g->libc->msvc_lib_dir); | ||
cache_str(ch, g->libc->kernel32_lib_dir); |
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.
These need the separator, otherwise a/b/
c/d
would hash the same as a/b
/c/d
.
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.
ooo interesting. that that mean that cache_mem
should also mix in the length?
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 believe cache_mem
is used internally as "put these bytes into the hash", whereas the public API functions are more along the lines of "put this thing in the hash and make sure it's separate"
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.
Then perhaps that belongs in the cache_slice
function below and hence answers you question there :)
src/cache_hash.cpp
Outdated
void cache_slice(CacheHash *ch, Slice<const char> slice) { | ||
cache_mem(ch, slice.ptr, slice.len); | ||
} | ||
|
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.
do we need this API? why not use cache_mem directly?
var result_buf = std.ArrayList(u8).init(allocator); | ||
defer result_buf.deinit(); |
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.
This is less efficient use of memory here. Before it only made one byte buffer and resized it in the loop; now it allocates and frees a fresh one every loop iteration. Why not port the code directly using ArrayList?
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.
Depends on your allocator
- For smart allocators that use a freelist, they will essentially do this automatically
- For dumber allocator, each time through the loop may have a different sized allocation and they probably don't do
realloc
efficiently, so they end up doing multiple allocations anyway!
In general I have a preference to reduce the scope of variables as much as possible.
fn appendWrite(self: *Self, m: []const u8) !usize { | ||
try self.appendSlice(m); | ||
return m.len; | ||
} | ||
|
||
pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { | ||
return .{ .context = self }; | ||
} |
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.
It's nice addition!
But maybe add a new structure with all necessary functions?
Something like std.io.MemoryStream.
775d89a
to
5ef2d0f
Compare
…ying on zero termination
5ef2d0f
to
14fb7d2
Compare
This is merged now, with additionally the following (breaking) changes:
|
Followup to #4405