Skip to content

Commit 37f853d

Browse files
committed
remove suggestion pool
1 parent 97113bc commit 37f853d

2 files changed

Lines changed: 40 additions & 92 deletions

File tree

src/suggestion/manager.zig

Lines changed: 33 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,44 @@ pub var autocomplete_suggestions: std.ArrayList([]const u8) = undefined;
3535
pub var autocomplete_ui_manager: suggestion_ui.AutocompleteUI = undefined;
3636

3737
/// Last word processed for suggestions
38-
pub var last_word_processed: []const u8 = "";
39-
40-
pub const SuggestionPool = struct {
41-
// Pre-allocated buffers for suggestions
42-
buffers: [config.TEXT.MAX_SUGGESTIONS][config.TEXT.MAX_SUGGESTION_LEN]u8,
43-
// Track which buffers are in use
44-
in_use: [config.TEXT.MAX_SUGGESTIONS]bool,
45-
46-
pub fn init() SuggestionPool {
47-
return SuggestionPool{
48-
.buffers = undefined,
49-
.in_use = [_]bool{false} ** config.TEXT.MAX_SUGGESTIONS,
50-
};
51-
}
38+
pub var last_word: [256]u8 = undefined;
39+
pub var last_word_len: usize = 0;
5240

53-
pub fn allocSuggestion(self: *SuggestionPool, allocator: std.mem.Allocator, text: []const u8) ![]u8 {
54-
// First try to find an available buffer
55-
for (self.in_use, 0..) |in_use, i| {
56-
if (!in_use and text.len < self.buffers[i].len) {
57-
self.in_use[i] = true;
58-
@memcpy(self.buffers[i][0..text.len], text);
59-
return self.buffers[i][0..text.len];
60-
}
61-
}
41+
// Replace the clearSuggestions function with this:
42+
pub fn clearSuggestions() void {
43+
// Destroy the entire list and create a new one
44+
autocomplete_suggestions.deinit();
45+
autocomplete_suggestions = std.ArrayList([]const u8).init(gpa_allocator);
46+
}
6247

63-
// Fall back to regular allocation if pool is full
64-
return allocator.dupe(u8, text);
65-
}
48+
// Update getAutocompleteSuggestions
49+
pub fn getAutocompleteSuggestions() !void {
50+
// Get current word
51+
const current_word = autocomplete_engine.current_word;
6652

67-
pub fn freeSuggestion(self: *SuggestionPool, allocator: std.mem.Allocator, text: []const u8) void {
68-
// Check if this is one of our pooled buffers
69-
for (0..self.buffers.len) |i| {
70-
if (@intFromPtr(text.ptr) == @intFromPtr(&self.buffers[i][0])) {
71-
self.in_use[i] = false;
72-
return;
73-
}
74-
}
53+
// Skip processing if the word hasn't changed
54+
if (current_word.len > 0 and current_word.len == last_word_len and
55+
std.mem.eql(u8, current_word, last_word[0..last_word_len]) and
56+
autocomplete_suggestions.items.len > 0)
57+
{
58+
return error.NoSuggestionsNeeded;
59+
}
7560

76-
// Not from our pool, so free normally
77-
allocator.free(text);
61+
// Store current word for next comparison
62+
if (current_word.len > 0 and current_word.len < last_word.len) {
63+
@memcpy(last_word[0..current_word.len], current_word);
64+
last_word_len = current_word.len;
65+
} else {
66+
// Too long or empty
67+
last_word_len = 0;
7868
}
79-
};
69+
70+
// Clear suggestions by recreating the list
71+
clearSuggestions();
72+
73+
// Get new suggestions
74+
try autocomplete_engine.getSuggestions(&autocomplete_suggestions);
75+
}
8076

8177
/// Initialize suggestion handling components
8278
pub fn init(allocator: std.mem.Allocator, module_instance: anytype) !void {
@@ -112,40 +108,6 @@ pub fn setCurrentWord(word: []const u8) void {
112108
autocomplete_engine.setCurrentWord(word);
113109
}
114110

115-
/// Get autocompletion suggestions
116-
pub fn getAutocompleteSuggestions() !void {
117-
// Skip processing if the word hasn't changed
118-
if (std.mem.eql(u8, autocomplete_engine.current_word, last_word_processed) and
119-
autocomplete_suggestions.items.len > 0)
120-
{
121-
return error.NoSuggestionsNeeded;
122-
}
123-
124-
// Store the current word as the last processed
125-
const current_word = autocomplete_engine.current_word;
126-
if (last_word_processed.len > 0) {
127-
gpa_allocator.free(last_word_processed);
128-
}
129-
if (!std.mem.eql(u8, last_word_processed, current_word)) {
130-
const owned_word = try gpa_allocator.dupe(u8, current_word);
131-
if (last_word_processed.len > 0) {
132-
gpa_allocator.free(last_word_processed);
133-
}
134-
last_word_processed = owned_word;
135-
} else {
136-
debug.debugPrint("Reusing existing word - avoided allocation\n", .{});
137-
}
138-
139-
// Clear existing suggestions
140-
for (autocomplete_suggestions.items) |item| {
141-
gpa_allocator.free(item);
142-
}
143-
autocomplete_suggestions.clearRetainingCapacity();
144-
145-
// Get new suggestions
146-
try autocomplete_engine.getSuggestions(&autocomplete_suggestions);
147-
}
148-
149111
/// Check if a word is spelled correctly
150112
pub fn isWordCorrect(word: []const u8) bool {
151113
return spell_checker.isCorrect(word);
@@ -600,20 +562,9 @@ pub fn addWordToVocabulary(word: []const u8) !void {
600562
/// Deinitialize components
601563
pub fn deinit() void {
602564
// Free resources
603-
for (suggestions.items) |item| {
604-
gpa_allocator.free(item);
605-
}
606565
suggestions.deinit();
607-
608-
for (autocomplete_suggestions.items) |item| {
609-
gpa_allocator.free(item);
610-
}
611566
autocomplete_suggestions.deinit();
612567

613-
if (last_word_processed.len > 0) {
614-
gpa_allocator.free(last_word_processed);
615-
}
616-
617568
spell_checker.deinit();
618569
autocomplete_engine.deinit();
619570
autocomplete_ui_manager.deinit();

src/text/autocomplete.zig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const config = sysinput.core.config;
77

88
/// Maximum number of suggestions to generate
99
const MAX_SUGGESTIONS = config.TEXT.MAX_SUGGESTIONS;
10-
var suggestion_pool = sysinput.suggestion.manager.SuggestionPool.init();
1110

1211
/// Autocompletion engine structure
1312
pub const AutocompleteEngine = struct {
@@ -74,12 +73,6 @@ pub const AutocompleteEngine = struct {
7473

7574
/// Get suggestions based on the current partial word
7675
pub fn getSuggestions(self: *AutocompleteEngine, results: *std.ArrayList([]const u8)) !void {
77-
// Clear existing suggestions
78-
for (results.items) |item| {
79-
suggestion_pool.freeSuggestion(self.allocator, item);
80-
}
81-
results.clearRetainingCapacity();
82-
8376
// Don't provide suggestions for very short partial words
8477
if (self.current_word.len < 1) return;
8578

@@ -102,8 +95,10 @@ pub const AutocompleteEngine = struct {
10295
if (std.mem.startsWith(u8, word, buf[0..self.current_word.len]) and
10396
!std.mem.eql(u8, word, buf[0..self.current_word.len]))
10497
{
105-
// Create a copy of the word
98+
// Create a copy of the word that WE OWN
10699
const owned_suggestion = try self.allocator.dupe(u8, word);
100+
errdefer self.allocator.free(owned_suggestion);
101+
107102
try results.append(owned_suggestion);
108103

109104
user_words_added += 1;
@@ -119,8 +114,10 @@ pub const AutocompleteEngine = struct {
119114
if (std.mem.startsWith(u8, dict_word.*, buf[0..self.current_word.len]) and
120115
!std.mem.eql(u8, dict_word.*, buf[0..self.current_word.len]))
121116
{
122-
// Create a copy of the word
123-
const owned_suggestion = try suggestion_pool.allocSuggestion(self.allocator, dict_word.*);
117+
// Create a copy of the word that WE OWN
118+
const owned_suggestion = try self.allocator.dupe(u8, dict_word.*);
119+
errdefer self.allocator.free(owned_suggestion);
120+
124121
try results.append(owned_suggestion);
125122

126123
if (results.items.len >= MAX_SUGGESTIONS) break;

0 commit comments

Comments
 (0)