Libove Blog

Personal Blog about anything - mostly programming, cooking and random thoughts


#

#zig comptime is great!

For implementing a B+Tree I have a bunch of constants which are all calculated based on the page size. At the end I can calculate a (reasonable) max depth of the tree and use this in my code to allocate slices which will always be big enough to hold the entire path to leaf node.

pub const MAX_DEPTH = max_depth_block: {
    // max file size on ext4 is 16 TiB
    const max_file_size = 16 * 1024 * 1024 * 1024 * 1024;

    // this will never be reached as the file also contains the tree
    const max_points = max_file_size / POINT_SIZE;
    const max_leaf_page = max_points / LEAF_CAPACITY;

    // log rounds down -> +1
    const max_depth = std.math.log(comptime_int, MAX_LINKS, max_leaf_page) + 1;
    break :max_depth_block max_depth;
};

#

Wrote my first data structure using comptime to replace an ArrayList where the maximum size is known at compile time. As I use it as a stack, it only implements append and pop as that is all I need.

#zig #dev

pub fn ConstStack(comptime T: type, size: comptime_int) type {
    return struct {
        items: [size]T = undefined,
        len: usize = 0,
        const Self = @This();

        pub fn append(self: *Self, item: T) !void {
            if (self.len == size) {
                return error.OutOfMemory;
            }
            self.items[self.len] = item;
            self.len += 1;
        }

        pub fn pop(self: *Self) ?T {
            if (self.len == 0) return null;
            self.len -= 1;
            return self.items[self.len];
        }
    };
}

#

If we aren't supposed to write our own database, why is it so much fun to do?


#

I'm currently watching the CMU Database Group Lectures to learn more about #database systems and design. And I just noticed how refreshing it is to learn this way and I somewhat miss being at a university, working into the depth of a topic. Work feels shallow in comparison.

#dev #thoughts


#

Cooked a large batch of #vegan Bolognese.

four jars with vegan Bolognese


#

#gamedev progress on hotel game:

  • did some further refactoring to rendering function
  • added room highlighting when building new rooms and selecting bed rooms for new guests

Not sure if I should be proud or embarrassed about this #rust code :D

// mark building tile
if let Some(mut r) = match game.mouse_action {
    MouseAction::BuildGrill => Some(Room::new_grill()),
    MouseAction::BuildStairs => Some(Room::new_stairs()),
    MouseAction::BuildBedroom => Some(Room::new_bed_room()),
    MouseAction::BuildCorridor => Some(Room::new_corridor()),
    MouseAction::BuildRestaurant => Some(Room::new_restaurant()),
    MouseAction::BuildWineCellar => Some(Room::new_wine_cellar()),
    _ => None,
} {
    // rendering code
}

Screen Recording of my hotel game. It shows the highlighting of available bed rooms and placing new rooms


#

Insights from the weekend:

  • Claude Sonnet 3.7 is the first #GenAI I tried out that actually produced usable code. The code is not pretty but it was the first time I was impressed by an AI system being able to produce non-trivial code and being able to adjust it to feedback.
  • I should stop using #python for one off scripts to convert data. The #rust ecosystem has a similar maturity. Building such scripts takes roughly the same time in both languages, but the rust system will finish the job order of magnitudes faster.
  • When your rust program feels slow you probably forget --release.