Skip to content

Commit

Permalink
Merge branch 'main' into dylan/fix-flaky-install-test
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Jan 9, 2025
2 parents 9ae235c + a733421 commit 34170fb
Show file tree
Hide file tree
Showing 28 changed files with 347 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! A nullable allocator the same size as `std.mem.Allocator`.
const std = @import("std");
const NullableAllocator = @This();
const bun = @import("root").bun;

const NullableAllocator = @This();

ptr: *anyopaque = undefined,
// Utilize the null pointer optimization on the vtable instead of
// the regular ptr because some allocator implementations might tag their
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@ pub const LinuxMemFdAllocator = struct {
}

pub fn deref(this: *LinuxMemFdAllocator) void {
if (this.ref_count.fetchSub(1, .monotonic) == 1) {
_ = bun.sys.close(this.fd);
this.destroy();
switch (this.ref_count.fetchSub(1, .monotonic)) {
1 => {
_ = bun.sys.close(this.fd);
this.destroy();
},
0 => {
// TODO: @branchHint(.cold) after Zig 0.14 upgrade
if (comptime bun.Environment.isDebug) {
std.debug.panic("LinuxMemFdAllocator ref_count underflow", .{});
}
},
else => {},
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const bun = @import("root").bun;
const log = bun.Output.scoped(.mimalloc, true);
const assert = bun.assert;
const Allocator = mem.Allocator;
const mimalloc = @import("./allocators/mimalloc.zig");
const FeatureFlags = @import("./feature_flags.zig");
const Environment = @import("./env.zig");
const mimalloc = @import("./mimalloc.zig");
const FeatureFlags = @import("../feature_flags.zig");
const Environment = @import("../env.zig");

fn mimalloc_free(
_: *anyopaque,
Expand Down
6 changes: 3 additions & 3 deletions src/mimalloc_arena.zig → src/allocators/mimalloc_arena.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const mem = @import("std").mem;
const builtin = @import("std").builtin;
const std = @import("std");

const mimalloc = @import("./allocators/mimalloc.zig");
const Environment = @import("./env.zig");
const FeatureFlags = @import("./feature_flags.zig");
const mimalloc = @import("./mimalloc.zig");
const Environment = @import("../env.zig");
const FeatureFlags = @import("../feature_flags.zig");
const Allocator = mem.Allocator;
const assert = bun.assert;
const bun = @import("root").bun;
Expand Down
2 changes: 1 addition & 1 deletion src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4485,5 +4485,5 @@ const JSModuleLoader = JSC.JSModuleLoader;
const EventLoopHandle = JSC.EventLoopHandle;
const JSInternalPromise = JSC.JSInternalPromise;

const ThreadlocalArena = @import("../mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const Chunk = bun.bundle_v2.Chunk;
2 changes: 1 addition & 1 deletion src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const JSAst = bun.JSAst;
const JSParser = bun.js_parser;
const JSPrinter = bun.js_printer;
const ScanPassResult = JSParser.ScanPassResult;
const Mimalloc = @import("../../mimalloc_arena.zig");
const Mimalloc = @import("../../allocators/mimalloc_arena.zig");
const Runtime = @import("../../runtime.zig").Runtime;
const JSLexer = bun.js_lexer;
const Expr = JSAst.Expr;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const JSAst = bun.JSAst;
const JSParser = bun.js_parser;
const JSPrinter = bun.js_printer;
const ScanPassResult = JSParser.ScanPassResult;
const Mimalloc = @import("../../mimalloc_arena.zig");
const Mimalloc = @import("../../allocators/mimalloc_arena.zig");
const Runtime = @import("../../runtime.zig").Runtime;
const JSLexer = bun.js_lexer;
const Expr = JSAst.Expr;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const Fallback = Runtime.Fallback;
const MimeType = HTTP.MimeType;
const Blob = JSC.WebCore.Blob;
const BoringSSL = bun.BoringSSL;
const Arena = @import("../../mimalloc_arena.zig").Arena;
const Arena = @import("../../allocators/mimalloc_arena.zig").Arena;
const SendfileContext = struct {
fd: bun.FileDescriptor,
socket_fd: bun.FileDescriptor = bun.invalid_fd,
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const ErrorableString = bun.JSC.ErrorableString;
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const C = bun.C;

const Exception = bun.JSC.Exception;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/module_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const C = bun.C;

const Allocator = std.mem.Allocator;
Expand Down
16 changes: 8 additions & 8 deletions src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ pub const use_mimalloc = true;
pub const default_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").c_allocator;
@import("./allocators/memory_allocator.zig").c_allocator;

/// Zeroing memory allocator
pub const z_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").z_allocator;
@import("./allocators/memory_allocator.zig").z_allocator;

pub const huge_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").huge_allocator;
@import("./allocators/memory_allocator.zig").huge_allocator;

pub const auto_allocator: std.mem.Allocator = if (!use_mimalloc)
std.heap.c_allocator
else
@import("./memory_allocator.zig").auto_allocator;
@import("./allocators/memory_allocator.zig").auto_allocator;

pub const callmod_inline: std.builtin.CallModifier = if (builtin.mode == .Debug) .auto else .always_inline;
pub const callconv_inline: std.builtin.CallingConvention = if (builtin.mode == .Debug) .Unspecified else .Inline;
Expand Down Expand Up @@ -556,7 +556,7 @@ pub const StringBuilder = @import("./string_builder.zig");

pub const LinearFifo = @import("./linear_fifo.zig").LinearFifo;
pub const linux = struct {
pub const memfd_allocator = @import("./linux_memfd_allocator.zig").LinuxMemFdAllocator;
pub const memfd_allocator = @import("./allocators/linux_memfd_allocator.zig").LinuxMemFdAllocator;
};

/// hash a string
Expand Down Expand Up @@ -887,7 +887,7 @@ pub fn openDirAbsoluteNotForDeletingOrRenaming(path_: []const u8) !std.fs.Dir {
return fd.asDir();
}

pub const MimallocArena = @import("./mimalloc_arena.zig").Arena;
pub const MimallocArena = @import("./allocators/mimalloc_arena.zig").Arena;
pub fn getRuntimeFeatureFlag(comptime flag: [:0]const u8) bool {
return struct {
const state = enum(u8) { idk, disabled, enabled };
Expand Down Expand Up @@ -1607,7 +1607,7 @@ pub const fast_debug_build_mode = fast_debug_build_cmd != .None and

pub const MultiArrayList = @import("./multi_array_list.zig").MultiArrayList;
pub const StringJoiner = @import("./StringJoiner.zig");
pub const NullableAllocator = @import("./NullableAllocator.zig");
pub const NullableAllocator = @import("./allocators/NullableAllocator.zig");

pub const renamer = @import("./renamer.zig");
// TODO: Rename to SourceMap as this is a struct.
Expand Down Expand Up @@ -2044,7 +2044,7 @@ pub fn HiveRef(comptime T: type, comptime capacity: u16) type {
};
}

pub const MaxHeapAllocator = @import("./max_heap_allocator.zig").MaxHeapAllocator;
pub const MaxHeapAllocator = @import("./allocators/max_heap_allocator.zig").MaxHeapAllocator;

pub const tracy = @import("./tracy.zig");
pub const trace = tracy.trace;
Expand Down
2 changes: 1 addition & 1 deletion src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DotEnv = @import("env_loader.zig");
const which = @import("which.zig").which;
const JSC = bun.JSC;
const AsyncHTTP = bun.http.AsyncHTTP;
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;

const OpaqueWrap = JSC.OpaqueWrap;
const VirtualMachine = JSC.VirtualMachine;
Expand Down
2 changes: 1 addition & 1 deletion src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const Ref = @import("../ast/base.zig").Ref;
const Define = @import("../defines.zig").Define;
const DebugOptions = @import("../cli.zig").Command.DebugOptions;
const ThreadPoolLib = @import("../thread_pool.zig");
const ThreadlocalArena = @import("../mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("../allocators/mimalloc_arena.zig").Arena;
const BabyList = @import("../baby_list.zig").BabyList;
const Fs = @import("../fs.zig");
const schema = @import("../api/schema.zig");
Expand Down
2 changes: 1 addition & 1 deletion src/css/css_internals.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const bun = @import("root").bun;
const std = @import("std");
const builtin = @import("builtin");
const Arena = @import("../mimalloc_arena.zig").Arena;
const Arena = @import("../allocators/mimalloc_arena.zig").Arena;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const JSC = bun.JSC;
Expand Down
2 changes: 1 addition & 1 deletion src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ThreadPool = bun.ThreadPool;
const ObjectPool = @import("./pool.zig").ObjectPool;
const posix = std.posix;
const SOCK = posix.SOCK;
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;
const ZlibPool = @import("./http/zlib.zig");
const BoringSSL = bun.BoringSSL;
const Progress = bun.Progress;
Expand Down
2 changes: 1 addition & 1 deletion src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const clap = bun.clap;
const ExtractTarball = @import("./extract_tarball.zig");
pub const Npm = @import("./npm.zig");
const Bitset = bun.bit_set.DynamicBitSetUnmanaged;
const z_allocator = @import("../memory_allocator.zig").z_allocator;
const z_allocator = @import("../allocators/memory_allocator.zig").z_allocator;
const Syscall = bun.sys;
const RunCommand = @import("../cli/run_command.zig").RunCommand;
const PackageManagerCommand = @import("../cli/package_manager_command.zig").PackageManagerCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/install/lockfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const clap = bun.clap;
const ExtractTarball = @import("./extract_tarball.zig");
const Npm = @import("./npm.zig");
const Bitset = bun.bit_set.DynamicBitSetUnmanaged;
const z_allocator = @import("../memory_allocator.zig").z_allocator;
const z_allocator = @import("../allocators/memory_allocator.zig").z_allocator;
const Lockfile = @This();

const IdentityContext = @import("../identity_context.zig").IdentityContext;
Expand Down
20 changes: 19 additions & 1 deletion src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,26 @@ declare function $toPropertyKey(x: any): PropertyKey;
* `$toObject(this, "Class.prototype.method requires that |this| not be null or undefined");`
*/
declare function $toObject(object: any, errorMessage?: string): object;
/**
* ## References
* - [WebKit - `emit_intrinsic_newArrayWithSize`](https://github.com/oven-sh/WebKit/blob/e1a802a2287edfe7f4046a9dd8307c8b59f5d816/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp#L2317)
*/
declare function $newArrayWithSize<T>(size: number): T[];
declare function $newArrayWithSpecies(): TODO;
/**
* Optimized path for creating a new array storing objects with the same homogenous Structure
* as {@link array}.
*
* @param size the initial size of the new array
* @param array the array whose shape we want to copy
*
* @returns a new array
*
* ## References
* - [WebKit - `emit_intrinsic_newArrayWithSpecies`](https://github.com/oven-sh/WebKit/blob/e1a802a2287edfe7f4046a9dd8307c8b59f5d816/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp#L2328)
* - [WebKit - #4909](https://github.com/WebKit/WebKit/pull/4909)
* - [WebKit Bugzilla - Related Issue/Ticket](https://bugs.webkit.org/show_bug.cgi?id=245797)
*/
declare function $newArrayWithSpecies<T>(size: number, array: T[]): T[];
declare function $newPromise(): TODO;
declare function $createPromise(): TODO;
declare const $iterationKindKey: TODO;
Expand Down
4 changes: 2 additions & 2 deletions src/js/builtins/StreamInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export function validateAndNormalizeQueuingStrategy(size, highWaterMark) {

$linkTimeConstant;
export function createFIFO() {
const Denqueue = require("internal/fifo");
return new Denqueue();
const Dequeue = require("internal/fifo");
return new Dequeue();
}

export function newQueue() {
Expand Down
1 change: 1 addition & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,4 @@ export const bindgen = $zig("bindgen_test.zig", "getBindgenTestFunctions") as {
};

export const noOpForTesting = $cpp("NoOpForTesting.cpp", "createNoOpForTesting");
export const Dequeue = require("internal/fifo");
39 changes: 18 additions & 21 deletions src/js/internal/fifo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var slice = Array.prototype.slice;
class Denqueue {
class Dequeue<T> {
_head: number;
_tail: number;
_capacityMask: number;
_list: (T | undefined)[];

constructor() {
this._head = 0;
this._tail = 0;
Expand All @@ -8,26 +13,21 @@ class Denqueue {
this._list = $newArrayWithSize(4);
}

_head;
_tail;
_capacityMask;
_list;

size() {
size(): number {
if (this._head === this._tail) return 0;
if (this._head < this._tail) return this._tail - this._head;
else return this._capacityMask + 1 - (this._head - this._tail);
}

isEmpty() {
isEmpty(): boolean {
return this.size() == 0;
}

isNotEmpty() {
isNotEmpty(): boolean {
return this.size() > 0;
}

shift() {
shift(): T | undefined {
var { _head: head, _tail, _list, _capacityMask } = this;
if (head === _tail) return undefined;
var item = _list[head];
Expand All @@ -37,24 +37,21 @@ class Denqueue {
return item;
}

peek() {
peek(): T | undefined {
if (this._head === this._tail) return undefined;
return this._list[this._head];
}

push(item) {
push(item: T): void {
var tail = this._tail;
$putByValDirect(this._list, tail, item);
this._tail = (tail + 1) & this._capacityMask;
if (this._tail === this._head) {
this._growArray();
}
// if (this._capacity && this.size() > this._capacity) {
// this.shift();
// }
}

toArray(fullCopy) {
toArray(fullCopy: boolean): T[] {
var list = this._list;
var len = $toLength(list.length);

Expand All @@ -66,19 +63,19 @@ class Denqueue {
var j = 0;
for (var i = _head; i < len; i++) $putByValDirect(array, j++, list[i]);
for (var i = 0; i < _tail; i++) $putByValDirect(array, j++, list[i]);
return array;
return array as T[];
} else {
return slice.$call(list, this._head, this._tail);
}
}

clear() {
clear(): void {
this._head = 0;
this._tail = 0;
this._list.fill(undefined);
}

_growArray() {
private _growArray(): void {
if (this._head) {
// copy existing data, head to end, then beginning to tail.
this._list = this.toArray(true);
Expand All @@ -92,10 +89,10 @@ class Denqueue {
this._capacityMask = (this._capacityMask << 1) | 1;
}

_shrinkArray() {
private _shrinkArray(): void {
this._list.length >>>= 1;
this._capacityMask >>>= 1;
}
}

export default Denqueue;
export default Dequeue;
2 changes: 1 addition & 1 deletion src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ComptimeStringMap = bun.ComptimeStringMap;
const JSPrinter = @import("./js_printer.zig");
const js_lexer = @import("./js_lexer.zig");
const TypeScript = @import("./js_parser.zig").TypeScript;
const ThreadlocalArena = @import("./mimalloc_arena.zig").Arena;
const ThreadlocalArena = @import("./allocators/mimalloc_arena.zig").Arena;
const MimeType = bun.http.MimeType;
const OOM = bun.OOM;
const Loader = bun.options.Loader;
Expand Down
2 changes: 1 addition & 1 deletion src/main_wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export fn init(heapsize: u32) void {
buffer_writer = writer.ctx;
}
}
const Arena = @import("./mimalloc_arena.zig").Arena;
const Arena = @import("./allocators/mimalloc_arena.zig").Arena;

var log: Logger.Log = undefined;

Expand Down
Loading

0 comments on commit 34170fb

Please sign in to comment.