Skip to content

Commit

Permalink
feat: build optimizations (#62)
Browse files Browse the repository at this point in the history
* remove: double buffering

* clarification: cache

* build: use all cores by default
  • Loading branch information
freref authored Feb 27, 2025
1 parent 95e06a4 commit fdb1352
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
5 changes: 5 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub fn build(b: *std.Build) void {
defer make_args.deinit();

make_args.append("make") catch unreachable;

// use as many cores as possible by default (like zig) I dont know how to check for j<N> arg
const cpu_count = std.Thread.getCpuCount() catch 1;
make_args.append(b.fmt("-j{d}", .{cpu_count})) catch unreachable;

make_args.append("-C") catch unreachable;
make_args.append("deps/mupdf") catch unreachable;

Expand Down
2 changes: 1 addition & 1 deletion src/Context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub const Context = struct {
},
.file_changed => {
try self.pdf_handler.reloadDocument();
// we could remove the current page from the cache here
self.reload_page = true;
},
}
Expand Down Expand Up @@ -251,7 +252,6 @@ pub const Context = struct {
}

pub fn drawCurrentPage(self: *Self, win: vaxis.Window) !void {
self.pdf_handler.commitReload();
if (self.current_page == null or self.reload_page) {
const winsize = try vaxis.Tty.getWinsize(self.tty.fd);
const pix_per_col = try std.math.divCeil(u16, win.screen.width_pix, win.screen.width);
Expand Down
19 changes: 4 additions & 15 deletions src/PdfHandler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub const ScrollDirection = enum { Up, Down, Left, Right };
allocator: std.mem.Allocator,
ctx: [*c]c.fz_context,
doc: [*c]c.fz_document,
temp_doc: ?[*c]c.fz_document,
total_pages: u16,
current_page_number: u16,
path: []const u8,
Expand Down Expand Up @@ -62,7 +61,6 @@ pub fn init(
.allocator = allocator,
.ctx = ctx,
.doc = doc,
.temp_doc = null,
.total_pages = total_pages,
.current_page_number = current_page_number,
.path = path,
Expand All @@ -76,36 +74,27 @@ pub fn init(
}

pub fn deinit(self: *Self) void {
if (self.temp_doc) |doc| c.fz_drop_document(self.ctx, doc);
c.fz_drop_document(self.ctx, self.doc);
c.fz_drop_context(self.ctx);
}

pub fn reloadDocument(self: *Self) !void {
if (self.temp_doc) |doc| {
if (self.doc) |doc| {
c.fz_drop_document(self.ctx, doc);
self.temp_doc = null;
self.doc = null;
}

self.temp_doc = c.fz_open_document(self.ctx, self.path.ptr) orelse {
self.doc = c.fz_open_document(self.ctx, self.path.ptr) orelse {
std.debug.print("Failed to reload document\n", .{});
return PdfError.FailedToOpenDocument;
};

self.total_pages = @as(u16, @intCast(c.fz_count_pages(self.ctx, self.temp_doc.?)));
self.total_pages = @as(u16, @intCast(c.fz_count_pages(self.ctx, self.doc.?)));
if (self.current_page_number >= self.total_pages) {
self.current_page_number = self.total_pages - 1;
}
}

pub fn commitReload(self: *Self) void {
if (self.temp_doc) |doc| {
c.fz_drop_document(self.ctx, self.doc);
self.doc = doc;
self.temp_doc = null;
}
}

pub fn renderPage(
self: *Self,
page_number: u16,
Expand Down

0 comments on commit fdb1352

Please sign in to comment.