Compare commits

...

3 Commits

Author SHA1 Message Date
Meghan Denny
9d77519c9a Merge branch 'main' into nektro-patch-16185 2025-06-20 23:07:18 -08:00
Meghan Denny
b3267ef086 Merge branch 'main' into nektro-patch-16185 2025-05-31 13:58:20 -08:00
Meghan Denny
69ed4d40c0 zig: shrink in toOwnedSlice 2025-05-31 05:57:00 -07:00
8 changed files with 24 additions and 51 deletions

View File

@@ -1083,7 +1083,8 @@ pub const PipeReader = struct {
return this.state.done;
}
// we do not use .toOwnedSlice() because we don't want to reallocate memory.
const out = this.reader._buffer;
var out = this.reader._buffer;
out.shrinkAndFree(out.items.len);
this.reader._buffer.items = &.{};
this.reader._buffer.capacity = 0;
@@ -1942,7 +1943,7 @@ pub fn spawnMaybeSync(
if (try args.getTruthy(globalThis, "argv0")) |argv0_| {
const argv0_str = try argv0_.getZigString(globalThis);
if (argv0_str.len > 0) {
argv0 = try argv0_str.toOwnedSliceZ(allocator);
argv0 = try argv0_str.toOwnedSlice(allocator);
}
}
@@ -1950,7 +1951,7 @@ pub fn spawnMaybeSync(
if (try args.getTruthy(globalThis, "cwd")) |cwd_| {
const cwd_str = try cwd_.getZigString(globalThis);
if (cwd_str.len > 0) {
cwd = try cwd_str.toOwnedSliceZ(allocator);
cwd = try cwd_str.toOwnedSlice(allocator);
}
}
}

View File

@@ -551,7 +551,7 @@ pub const FFI = struct {
}
const str = try val.getZigString(globalThis);
if (str.isEmpty()) continue;
items.append(str.toOwnedSliceZ(bun.default_allocator) catch bun.outOfMemory()) catch bun.outOfMemory();
items.append(str.toOwnedSlice(bun.default_allocator) catch bun.outOfMemory()) catch bun.outOfMemory();
}
return .{ .items = items.items };
@@ -565,7 +565,7 @@ pub const FFI = struct {
const str = try value.getZigString(globalThis);
if (str.isEmpty()) return .{};
var items = std.ArrayList([:0]const u8).init(bun.default_allocator);
items.append(str.toOwnedSliceZ(bun.default_allocator) catch bun.outOfMemory()) catch bun.outOfMemory();
items.append(str.toOwnedSlice(bun.default_allocator) catch bun.outOfMemory()) catch bun.outOfMemory();
return .{ .items = items.items };
}
@@ -651,7 +651,7 @@ pub const FFI = struct {
const str = try flags_value.getZigString(globalThis);
if (!str.isEmpty()) {
compile_c.flags = str.toOwnedSliceZ(allocator) catch bun.outOfMemory();
compile_c.flags = str.toOwnedSlice(allocator) catch bun.outOfMemory();
}
}
}
@@ -672,7 +672,7 @@ pub const FFI = struct {
if (iter.value.isString()) {
const value = try iter.value.getZigString(globalThis);
if (value.len > 0) {
owned_value = value.toOwnedSliceZ(allocator) catch bun.outOfMemory();
owned_value = value.toOwnedSlice(allocator) catch bun.outOfMemory();
}
}
}
@@ -706,12 +706,12 @@ pub const FFI = struct {
if (!value.isString()) {
return globalThis.throwInvalidArgumentTypeValue("source", "array of strings", value);
}
try compile_c.source.files.append(bun.default_allocator, try (try value.getZigString(globalThis)).toOwnedSliceZ(bun.default_allocator));
try compile_c.source.files.append(bun.default_allocator, try (try value.getZigString(globalThis)).toOwnedSlice(bun.default_allocator));
}
} else if (!source_value.isString()) {
return globalThis.throwInvalidArgumentTypeValue("source", "string", source_value);
} else {
const source_path = try (try source_value.getZigString(globalThis)).toOwnedSliceZ(bun.default_allocator);
const source_path = try (try source_value.getZigString(globalThis)).toOwnedSlice(bun.default_allocator);
compile_c.source.file = source_path;
}
}

View File

@@ -256,7 +256,7 @@ pub const ZigString = extern struct {
return bun.webcore.encoding.byteLengthU8(this.slice().ptr, this.slice().len, .utf8);
}
pub fn toOwnedSlice(this: ZigString, allocator: std.mem.Allocator) OOM![]u8 {
pub fn toOwnedSlice(this: ZigString, allocator: std.mem.Allocator) OOM![:0]u8 {
if (this.isUTF8())
return try allocator.dupeZ(u8, this.slice());
@@ -266,29 +266,11 @@ pub const ZigString = extern struct {
else
try strings.allocateLatin1IntoUTF8WithList(list, 0, []const u8, this.slice());
if (list.capacity > list.items.len) {
list.items.ptr[list.items.len] = 0;
}
if (list.capacity > 0 and list.items.len == 0) {
list.deinit();
return &.{};
}
return list.items;
}
pub fn toOwnedSliceZ(this: ZigString, allocator: std.mem.Allocator) OOM![:0]u8 {
if (this.isUTF8())
return allocator.dupeZ(u8, this.slice());
var list = std.ArrayList(u8).init(allocator);
list = if (this.is16Bit())
try strings.toUTF8ListWithType(list, []const u16, this.utf16SliceAligned())
else
try strings.allocateLatin1IntoUTF8WithList(list, 0, []const u8, this.slice());
return list.toOwnedSliceSentinel(0);
const len = list.items.len;
try list.resize(len + 1);
list.shrinkAndFree(len + 1);
list.items.ptr[len] = 0;
return list.items[0..len :0];
}
pub fn trunc(this: ZigString, len: usize) ZigString {

View File

@@ -4409,17 +4409,11 @@ pub const Internal = struct {
}
pub fn toOwnedSlice(this: *@This()) []u8 {
const bytes = this.bytes.items;
const capacity = this.bytes.capacity;
if (bytes.len == 0 and capacity > 0) {
this.bytes.clearAndFree();
return &.{};
}
var bytes = this.bytes;
bytes.shrinkAndFree(bytes.items.len);
this.bytes.items = &.{};
this.bytes.capacity = 0;
return bytes;
return bytes.items;
}
pub fn clearAndFree(this: *@This()) void {
@@ -4518,10 +4512,6 @@ pub const Inline = extern struct {
return this.bytes[0..this.len];
}
pub fn toOwnedSlice(this: *@This()) []u8 {
return this.slice();
}
pub fn clearAndFree(_: *@This()) void {}
};

View File

@@ -2219,7 +2219,7 @@ pub fn Bun__fetch_(
hostname = null;
allocator.free(host);
}
hostname = _hostname.toOwnedSliceZ(allocator) catch bun.outOfMemory();
hostname = _hostname.toOwnedSlice(allocator) catch bun.outOfMemory();
}
if (url.isS3()) {
if (headers_.fastGet(bun.webcore.FetchHeaders.HTTPHeaderName.Range)) |_range| {
@@ -2227,7 +2227,7 @@ pub fn Bun__fetch_(
range = null;
allocator.free(range_);
}
range = _range.toOwnedSliceZ(allocator) catch bun.outOfMemory();
range = _range.toOwnedSlice(allocator) catch bun.outOfMemory();
}
}

View File

@@ -7571,7 +7571,6 @@ pub const DeclaredSymbol = struct {
pub fn toOwnedSlice(this: *List) List {
const new = this.*;
this.* = .{};
return new;
}

View File

@@ -1257,7 +1257,8 @@ pub const PipeReader = struct {
return this.state.done;
}
// we do not use .toOwnedSlice() because we don't want to reallocate memory.
const out = this.reader._buffer;
var out = this.reader._buffer;
out.shrinkAndFree(out.items.len);
this.reader._buffer.items = &.{};
this.reader._buffer.capacity = 0;

View File

@@ -316,7 +316,7 @@ pub const String = extern struct {
}
pub fn toOwnedSliceZ(this: String, allocator: std.mem.Allocator) OOM![:0]u8 {
return this.toZigString().toOwnedSliceZ(allocator);
return this.toZigString().toOwnedSlice(allocator);
}
/// Create a bun.String from a slice. This is never a copy.