Skip to content

Commit

Permalink
it's so fast and stuff (feat. ms)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeffDev committed Jan 1, 2023
1 parent 7c614da commit 7179aaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub fn main() !void {
var timer = try std.time.Timer.start();
try v1.unpackV1DataFile();
const elapsed_time = timer.read();
std.log.info("elapsed time: {}.{}s", .{elapsed_time / std.time.ns_per_s, elapsed_time / std.time.ns_per_ms});
std.log.info("elapsed time: {}.{}s", .{ elapsed_time / std.time.ns_per_s, elapsed_time / std.time.ns_per_ms });
}
28 changes: 12 additions & 16 deletions src/v1.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const std = @import("std");

const Directories = struct {
const Directory = struct {
dir_offset: u64,
dir_name: []const u8,
};

var v_file_size: u64 = 0;
var virtual_file_offset: u64 = 0;

pub var file_handle: std.fs.File = undefined;
Expand All @@ -17,8 +16,6 @@ pub fn unpackV1DataFile() !void {
return;
};
file_handle = file;
const stat = try file.stat();
std.log.info("{}", .{stat.size});

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
Expand All @@ -45,7 +42,8 @@ pub fn unpackV1DataFile() !void {

// array length must be comptime known so lets just
// set this to 64 i guess
var directories: [64]Directories = std.mem.zeroes([64]Directories);
var directories: [64]Directory = std.mem.zeroes([64]Directory);
var v_file_size: u64 = 0;

var i: usize = 0;
while (i < dir_amount) : (i += 1) {
Expand Down Expand Up @@ -86,6 +84,8 @@ pub fn unpackV1DataFile() !void {
fileRead(&f_name_len, 1) catch |err| {
if (err == error.EndOfStream) {
std.log.info("unpacked all files", .{});
const leak = gpa.deinit();
std.log.info("allocator leak = {}", .{leak});
}
return;
};
Expand All @@ -110,7 +110,7 @@ pub fn unpackV1DataFile() !void {
v_file_size += @as(u32, file_buf) << 24;

const unpacked_file = try cur_dir.createFile(sliced_f_name, .{});
try writeFile(unpacked_file, allocator);
try writeFile(unpacked_file, v_file_size, allocator);
allocator.free(sliced_f_name);
}
}
Expand All @@ -123,14 +123,10 @@ fn fileRead(dest: *u8, bytes_to_read: usize) !void {
}
}

/// TODO: there's probably a wayyyy faster way of doing this
/// please speed this up
fn writeFile(file: std.fs.File, allocator: std.mem.Allocator) !void {
var i: usize = 0;
var fileData = try allocator.alloc(u8, v_file_size);
while (i < v_file_size) : (i += 1) {
fileData[i] = try file_handle.reader().readByte();
}
_ = try file.write(fileData);
allocator.free(fileData);
fn writeFile(file: std.fs.File, bytes_to_read: u64, allocator: std.mem.Allocator) !void {
//var i: usize = 0;
var file_data = try allocator.alloc(u8, bytes_to_read);
_ = try file_handle.reader().readAll(file_data);
_ = try file.write(file_data);
allocator.free(file_data);
}

0 comments on commit 7179aaf

Please sign in to comment.