eliminate old text searching thread code

This commit is contained in:
Ryan Fleury
2024-03-29 11:28:45 -07:00
parent 8c5c0be040
commit eec124c53b
3 changed files with 0 additions and 431 deletions
-299
View File
@@ -7643,305 +7643,6 @@ df_push_search_string(Arena *arena)
return result;
}
////////////////////////////////
//~ rjf: Background Text Searching Thread
internal void
df_text_search_match_chunk_list_push(Arena *arena, DF_TextSearchMatchChunkList *list, U64 cap, DF_TextSearchMatch *match)
{
DF_TextSearchMatchChunkNode *node = list->last;
if(node == 0 || node->count >= node->cap)
{
node = push_array(arena, DF_TextSearchMatchChunkNode, 1);
node->cap = cap;
node->v = push_array_no_zero(arena, DF_TextSearchMatch, node->cap);
SLLQueuePush(list->first, list->last, node);
list->node_count += 1;
}
node->v[node->count] = *match;
node->count += 1;
list->total_count += 1;
}
internal DF_TextSearchMatchArray
df_text_search_match_array_from_chunk_list(Arena *arena, DF_TextSearchMatchChunkList *chunks)
{
DF_TextSearchMatchArray array = {0};
array.count = chunks->total_count;
array.v = push_array_no_zero(arena, DF_TextSearchMatch, array.count);
U64 idx = 0;
for(DF_TextSearchMatchChunkNode *node = chunks->first; node != 0; node = node->next)
{
MemoryCopy(array.v+idx, node->v, node->count * sizeof(DF_TextSearchMatch));
idx += node->count;
}
return array;
}
internal U64
df_text_search_little_hash_from_hash(U128 hash)
{
// TODO(rjf): [ ] @de2ctrl df_text_search_little_hash_from_hash
U64 little_hash = 0;
MemoryCopy(&little_hash, &hash, sizeof(little_hash));
return little_hash;
}
internal void
df_text_search_thread_entry_point(void *p)
{
#if 0
// TODO(rjf): [ ] @de2ctrl text searcher -- wound up in DE_Hash
//- rjf: types
typedef enum WorkKind
{
WorkKind_Search,
WorkKind_GarbageCollect,
WorkKind_COUNT
}
WorkKind;
typedef struct WorkNode WorkNode;
struct WorkNode
{
WorkNode *next;
WorkKind kind;
U128 hash;
String8 needle;
DF_TextSliceFlags flags;
TxtPt start_pt;
};
//- rjf: set up local debug engine map
Arena *local_map_arena = arena_alloc();
DE_ContentMap local_map = {0};
DE_PipelineHint hint = zero_struct;
//- rjf: loop over work
for(;;)
{
//- rjf: begin
Temp scratch = scratch_begin(0, 0);
DE_Session *session = de_session_begin();
//- rjf: wait for changes
os_mutex_take(df_gfx_state->tsrch_wakeup_mutex);
os_condition_variable_wait(df_gfx_state->tsrch_wakeup_cv, df_gfx_state->tsrch_wakeup_mutex, os_now_microseconds()+1000000);
os_mutex_drop(df_gfx_state->tsrch_wakeup_mutex);
//- rjf: gather all searches to complete
WorkNode *first_work_node = 0;
WorkNode *last_work_node = 0;
for(U64 slot_idx = 0; slot_idx < df_gfx_state->tsrch_slot_count; slot_idx += 1)
{
//- rjf: slot idx -> slot * stripe
DF_TextSearchCacheSlot *slot = &df_gfx_state->tsrch_slots[slot_idx];
U64 stripe_idx = slot_idx%df_gfx_state->tsrch_stripe_count;
OS_Handle stripe_rw_mutex = df_gfx_state->tsrch_stripe_rw_mutexes[stripe_idx];
//- rjf: gather nodes in this slot
os_rw_mutex_take_r(stripe_rw_mutex);
{
for(DF_TextSearchCacheNode *n = slot->first; n != 0; n = n->next)
{
B32 not_done = (n->good == 0);
B32 expired = (os_now_microseconds() >= n->last_time_touched_us + 10000000);
if(not_done || expired)
{
WorkNode *work = push_array(scratch.arena, WorkNode, 1);
work->kind = not_done ? WorkKind_Search : WorkKind_GarbageCollect;
work->hash = n->hash;
work->needle = push_str8_copy(scratch.arena, n->needle);
work->flags = n->flags;
work->start_pt = n->start_pt;
SLLQueuePush(first_work_node, last_work_node, work);
}
}
}
os_rw_mutex_drop_r(stripe_rw_mutex);
}
//- rjf: perform all searches
for(WorkNode *work_node = first_work_node; work_node != 0; work_node = work_node->next)
{
//- rjf: unpack work node
WorkKind kind = work_node->kind;
DE_Hash hash = work_node->hash;
String8 needle = work_node->needle;
DF_TextSliceFlags flags = work_node->flags;
TxtPt start_pt = work_node->start_pt;
//- rjf: work params -> slot/stripe info
U64 little_hash = df_text_search_little_hash_from_hash(hash);
U64 slot_idx = little_hash%df_gfx_state->tsrch_slot_count;
DF_TextSearchCacheSlot *slot = &df_gfx_state->tsrch_slots[slot_idx];
U64 stripe_idx = slot_idx%df_gfx_state->tsrch_stripe_count;
OS_Handle stripe_rw_mutex = df_gfx_state->tsrch_stripe_rw_mutexes[stripe_idx];
//- rjf: do work
switch(kind)
{
//- rjf: search
default:
case WorkKind_Search:
{
//- rjf: hash -> artifacts
DE_Key hash2data_key = de_key_hash(DE_KeyFunc_DataFromHash, &hash);
DE_Val *hash2data_val = de_shared_chained_lookup(local_map_arena, &local_map, de_shared, &hint, &hash2data_key);
DE_ContentBlock *hash2data_block = de_session_node_access_via_val(session, hash2data_val);
String8 data = hash2data_block->data;
DE_Key hash2txti_key = de_key_hash(DE_KeyFunc_TxtiFromHash, &hash);
DE_Val *hash2txti_val = de_shared_chained_lookup(local_map_arena, &local_map, de_shared, &hint, &hash2txti_key);
DE_ContentBlock *hash2txti_block = de_session_node_access_via_val(session, hash2txti_val);
DE_InfoTxt *txt = hash2txti_block->txt;
//- rjf: start pt -> search start offset
U64 start_off = 0;
if(1 <= start_pt.line && start_pt.line <= txt->line_count)
{
start_off = txt->line_ranges[start_pt.line-1].min;
if(1 <= start_pt.column && start_pt.column <= dim_1u64(txt->line_ranges[start_pt.line-1]))
{
start_off += (start_pt.column-1);
}
}
//- rjf: search for all needle occurrences
U8 *byte_first = data.str;
U8 *byte_opl = data.str+data.size;
U8 *byte_start = byte_first + start_off;
U64 num_bytes_traversed = 0;
for(U8 *byte = byte_start; num_bytes_traversed < data.size;)
{
String8 rest_of_data = str8(byte, byte_opl-byte);
String8 next_needle_size = str8_prefix(rest_of_data, needle.size);
B32 found_match = str8_match(next_needle_size, needle, StringMatchFlag_CaseInsensitive);
// rjf: record match
if(found_match)
{
U64 match_off = (U64)(byte-byte_first);
TxtPt match_pt = de_txt_pt_from_txti_off(txt, match_off);
DF_TextSearchMatch match = {match_pt};
os_rw_mutex_take_w(stripe_rw_mutex);
{
DF_TextSearchCacheNode *node = 0;
for(DF_TextSearchCacheNode *n = slot->first; n != 0; n = n->next)
{
if(MemoryMatchStruct(&hash, &n->hash) &&
str8_match(needle, n->needle, 0) &&
flags == n->flags)
{
node = n;
}
}
df_text_search_match_chunk_list_push(node->arena, &node->search_matches, 256, &match);
node->good = 1;
}
os_rw_mutex_drop_w(stripe_rw_mutex);
}
// rjf: increment
byte += 1;
num_bytes_traversed += 1;
if(byte >= byte_opl)
{
byte = byte_first;
}
}
}break;
//- rjf: garbage collect
case WorkKind_GarbageCollect:
{
os_rw_mutex_take_w(stripe_rw_mutex);
{
DF_TextSearchCacheNode *node = 0;
for(DF_TextSearchCacheNode *n = slot->first; n != 0; n = n->next)
{
if(MemoryMatchStruct(&hash, &n->hash) &&
str8_match(needle, n->needle, 0) &&
flags == n->flags)
{
node = n;
}
}
if(node != 0)
{
DLLRemove(slot->first, slot->last, node);
arena_release(node->arena);
}
}
os_rw_mutex_drop_w(stripe_rw_mutex);
}break;
}
}
//- rjf: end
de_session_end(session);
scratch_end(scratch);
}
#endif
}
internal int
df_text_search_match_array_qsort_compare(TxtPt *a, TxtPt *b)
{
int result = 0;
if(txt_pt_less_than(*a, *b))
{
result = -1;
}
else if(txt_pt_less_than(*b, *a))
{
result = +1;
}
return result;
}
internal void
df_text_search_match_array_sort_in_place(DF_TextSearchMatchArray *array)
{
qsort(array->v, array->count, sizeof(DF_TextSearchMatch), (int (*)(const void *, const void *))df_text_search_match_array_qsort_compare);
}
internal DF_TextSearchMatch
df_text_search_match_array_find_nearest__linear_scan(DF_TextSearchMatchArray *array, TxtPt pt, Side side)
{
ProfBeginFunction();
DF_TextSearchMatch result = {0};
if(array->count != 0)
{
S64 best_line_distance = max_S64;
S64 best_column_distance = max_S64;
B32 best_matches_side = 0;
for(U64 idx = 0; idx < array->count; idx += 1)
{
S64 line_distance = abs_s64(array->v[idx].pt.line - pt.line);
S64 column_distance = abs_s64(array->v[idx].pt.column - pt.column);
B32 matches_side = (side == Side_Max ? txt_pt_less_than(pt, array->v[idx].pt) :
side == Side_Min ? txt_pt_less_than(array->v[idx].pt, pt) :
1);
if(matches_side >= best_matches_side && line_distance == 0 && column_distance < best_column_distance)
{
best_matches_side = matches_side;
best_line_distance = 0;
best_column_distance = column_distance;
result = array->v[idx];
}
else if(matches_side >= best_matches_side && line_distance < best_line_distance)
{
best_matches_side = matches_side;
best_line_distance = line_distance;
result = array->v[idx];
}
}
}
ProfEnd();
return result;
}
////////////////////////////////
//~ rjf: Colors, Fonts, Config