MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / build_src_entry

Function build_src_entry

src/semantic/semantic.c:1101–1155  ·  view source on GitHub ↗

Build one src_entry for a token: dense float32 reference if in nomic vocab, * sparse inline representation otherwise. Collisions in the sparse hash are * merged and zeros filtered so the final representation is exactly the same * mathematical vector that the old dense path produced. */

Source from the content-addressed store, hash-verified

1099 * merged and zeros filtered so the final representation is exactly the same
1100 * mathematical vector that the old dense path produced. */
1101static void build_src_entry(const char *token, cbm_sem_src_entry_t *out) {
1102 memset(out, 0, sizeof(*out));
1103 if (!token) {
1104 out->is_sparse = SKIP_ONE;
1105 out->nnz = 0;
1106 return;
1107 }
1108 /* Dense path: direct int8 pointer into pretrained blob (zero-copy). */
1109 const char *idx_str = cbm_ht_get(g_pretrained_map, token);
1110 if (idx_str) {
1111 char *end = NULL;
1112 long idx = strtol(idx_str, &end, BASE_DECIMAL);
1113 if (end != idx_str && idx >= 0 && idx < PRETRAINED_TOKEN_COUNT) {
1114 out->is_sparse = 0;
1115 out->dense_int8 = pretrained_vec_at((int)idx);
1116 return;
1117 }
1118 }
1119 /* Sparse path: compute 8 hash positions with collision merging. */
1120 out->is_sparse = SKIP_ONE;
1121 uint16_t tmp_idx[CBM_SEM_SPARSE_NNZE];
1122 float tmp_val[CBM_SEM_SPARSE_NNZE];
1123 int count = 0;
1124 uint64_t seed = XXH3_64bits(token, strlen(token));
1125 for (int i = 0; i < CBM_SEM_SPARSE_NNZE; i++) {
1126 uint64_t h = XXH3_64bits_withSeed(&i, sizeof(i), seed + RI_SEED_BASE);
1127 int pos = (int)(h % CBM_SEM_DIM);
1128 float sign = (h & SKIP_ONE) ? CBM_SEM_UNIT_POS : -CBM_SEM_UNIT_POS;
1129 /* Merge collisions */
1130 int found = CBM_NOT_FOUND;
1131 for (int j = 0; j < count; j++) {
1132 if (tmp_idx[j] == (uint16_t)pos) {
1133 found = j;
1134 break;
1135 }
1136 }
1137 if (found >= 0) {
1138 tmp_val[found] += sign;
1139 } else {
1140 tmp_idx[count] = (uint16_t)pos;
1141 tmp_val[count] = sign;
1142 count++;
1143 }
1144 }
1145 /* Filter zeros */
1146 int nnz = 0;
1147 for (int j = 0; j < count; j++) {
1148 if (tmp_val[j] != 0.0F) {
1149 out->indices[nnz] = tmp_idx[j];
1150 out->values[nnz] = tmp_val[j];
1151 nnz++;
1152 }
1153 }
1154 out->nnz = (uint8_t)nnz;
1155}
1156
1157static void src_build_worker(int worker_id, void *ctx_ptr) {
1158 (void)worker_id;

Callers 1

src_build_workerFunction · 0.85

Calls 1

cbm_ht_getFunction · 0.85

Tested by

no test coverage detected