Mode: Use ArrayListUnmanaged to save memory

This commit is contained in:
Hugo Machet 2022-02-07 14:30:15 +01:00 committed by Isaac Freund
parent ca47b8a54e
commit 995ae99be5
4 changed files with 17 additions and 26 deletions

View File

@ -55,7 +55,7 @@ border_color_urgent: [4]f32 = [_]f32{ 0.86274510, 0.19607843, 0.18431373, 1.0 },
mode_to_id: std.StringHashMap(usize), mode_to_id: std.StringHashMap(usize),
/// All user-defined keymap modes, indexed by mode id /// All user-defined keymap modes, indexed by mode id
modes: std.ArrayList(Mode), modes: std.ArrayListUnmanaged(Mode),
/// Sets of app_ids and titles which will be started floating /// Sets of app_ids and titles which will be started floating
float_filter_app_ids: std.StringHashMapUnmanaged(void) = .{}, float_filter_app_ids: std.StringHashMapUnmanaged(void) = .{},
@ -88,23 +88,22 @@ repeat_delay: u31 = 600,
pub fn init() !Self { pub fn init() !Self {
var self = Self{ var self = Self{
.mode_to_id = std.StringHashMap(usize).init(util.gpa), .mode_to_id = std.StringHashMap(usize).init(util.gpa),
.modes = std.ArrayList(Mode).init(util.gpa), .modes = try std.ArrayListUnmanaged(Mode).initCapacity(util.gpa, 2),
}; };
errdefer self.deinit(); errdefer self.deinit();
// Start with two empty modes, "normal" and "locked" // Start with two empty modes, "normal" and "locked"
try self.modes.ensureTotalCapacity(2);
{ {
// Normal mode, id 0 // Normal mode, id 0
const owned_slice = try util.gpa.dupe(u8, "normal"); const owned_slice = try util.gpa.dupe(u8, "normal");
try self.mode_to_id.putNoClobber(owned_slice, 0); try self.mode_to_id.putNoClobber(owned_slice, 0);
self.modes.appendAssumeCapacity(Mode.init()); self.modes.appendAssumeCapacity(.{});
} }
{ {
// Locked mode, id 1 // Locked mode, id 1
const owned_slice = try util.gpa.dupe(u8, "locked"); const owned_slice = try util.gpa.dupe(u8, "locked");
try self.mode_to_id.putNoClobber(owned_slice, 1); try self.mode_to_id.putNoClobber(owned_slice, 1);
self.modes.appendAssumeCapacity(Mode.init()); self.modes.appendAssumeCapacity(.{});
} }
return self; return self;
@ -117,8 +116,8 @@ pub fn deinit(self: *Self) void {
self.mode_to_id.deinit(); self.mode_to_id.deinit();
} }
for (self.modes.items) |mode| mode.deinit(); for (self.modes.items) |*mode| mode.deinit();
self.modes.deinit(); self.modes.deinit(util.gpa);
{ {
var it = self.float_filter_app_ids.keyIterator(); var it = self.float_filter_app_ids.keyIterator();

View File

@ -22,19 +22,11 @@ const util = @import("util.zig");
const Mapping = @import("Mapping.zig"); const Mapping = @import("Mapping.zig");
const PointerMapping = @import("PointerMapping.zig"); const PointerMapping = @import("PointerMapping.zig");
// TODO: use unmanaged array lists here to save memory mappings: std.ArrayListUnmanaged(Mapping) = .{},
mappings: std.ArrayList(Mapping), pointer_mappings: std.ArrayListUnmanaged(PointerMapping) = .{},
pointer_mappings: std.ArrayList(PointerMapping),
pub fn init() Self { pub fn deinit(self: *Self) void {
return .{
.mappings = std.ArrayList(Mapping).init(util.gpa),
.pointer_mappings = std.ArrayList(PointerMapping).init(util.gpa),
};
}
pub fn deinit(self: Self) void {
for (self.mappings.items) |m| m.deinit(); for (self.mappings.items) |m| m.deinit();
self.mappings.deinit(); self.mappings.deinit(util.gpa);
self.pointer_mappings.deinit(); self.pointer_mappings.deinit(util.gpa);
} }

View File

@ -39,9 +39,9 @@ pub fn declareMode(
if (config.mode_to_id.get(new_mode_name) != null) return; if (config.mode_to_id.get(new_mode_name) != null) return;
try config.modes.ensureUnusedCapacity(1); try config.modes.ensureUnusedCapacity(util.gpa, 1);
const owned_name = try util.gpa.dupe(u8, new_mode_name); const owned_name = try util.gpa.dupe(u8, new_mode_name);
errdefer util.gpa.free(owned_name); errdefer util.gpa.free(owned_name);
try config.mode_to_id.putNoClobber(owned_name, config.modes.items.len); try config.mode_to_id.putNoClobber(owned_name, config.modes.items.len);
config.modes.appendAssumeCapacity(Mode.init()); config.modes.appendAssumeCapacity(.{});
} }

View File

@ -73,7 +73,7 @@ pub fn map(
// possible crash if the Mapping ArrayList is reallocated, stop any // possible crash if the Mapping ArrayList is reallocated, stop any
// currently repeating mappings. // currently repeating mappings.
seat.clearRepeatingMapping(); seat.clearRepeatingMapping();
try mode_mappings.append(new); try mode_mappings.append(util.gpa, new);
} }
} }
@ -117,7 +117,7 @@ pub fn mapPointer(
if (pointerMappingExists(mode_pointer_mappings, modifiers, event_code)) |current| { if (pointerMappingExists(mode_pointer_mappings, modifiers, event_code)) |current| {
mode_pointer_mappings.items[current] = new; mode_pointer_mappings.items[current] = new;
} else { } else {
try mode_pointer_mappings.append(new); try mode_pointer_mappings.append(util.gpa, new);
} }
} }
@ -135,7 +135,7 @@ fn modeNameToId(allocator: std.mem.Allocator, mode_name: []const u8, out: *?[]co
/// Returns the index of the Mapping with matching modifiers, keysym and release, if any. /// Returns the index of the Mapping with matching modifiers, keysym and release, if any.
fn mappingExists( fn mappingExists(
mappings: *std.ArrayList(Mapping), mappings: *std.ArrayListUnmanaged(Mapping),
modifiers: wlr.Keyboard.ModifierMask, modifiers: wlr.Keyboard.ModifierMask,
keysym: xkb.Keysym, keysym: xkb.Keysym,
release: bool, release: bool,
@ -151,7 +151,7 @@ fn mappingExists(
/// Returns the index of the PointerMapping with matching modifiers and event code, if any. /// Returns the index of the PointerMapping with matching modifiers and event code, if any.
fn pointerMappingExists( fn pointerMappingExists(
pointer_mappings: *std.ArrayList(PointerMapping), pointer_mappings: *std.ArrayListUnmanaged(PointerMapping),
modifiers: wlr.Keyboard.ModifierMask, modifiers: wlr.Keyboard.ModifierMask,
event_code: u32, event_code: u32,
) ?usize { ) ?usize {