mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-10 14:52:57 +00:00
feat(perf): Optimize feed tag extraction with caching
- Extracted `getVideoTags` to `js/feedEngine/utils.js` with `WeakMap` caching. - Updated `js/feedEngine/stages.js` and `js/feedEngine/kidsScoring.js` to use the cached utility. - Removed redundant `collectVideoTags` and `normalizeTagSetFromVideo` functions. - Added `benchmarks/feed_tags.mjs` to measure performance (approx 25x speedup in tight loops). - Updated `hasDisinterestedTag` in `utils.js` to use the new utility. This reduces object allocation and repeated string normalization during feed processing. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
d951e74920
commit
6f49a05302
4 files changed
+130
-95
No files matched your search
+45
-22
@@ -4,6 +4,8 @@ import { normalizeHashtag } from "../utils/hashtagNormalization.js";
|
||||
|
||||
export const NORMALIZED_ARRAY_MARKER = Symbol("FeedItemsNormalized");
|
||||
|
||||
const videoTagCache = new WeakMap();
|
||||
|
||||
export function markAsNormalized(items) {
|
||||
if (Array.isArray(items)) {
|
||||
items[NORMALIZED_ARRAY_MARKER] = true;
|
||||
@@ -39,33 +41,54 @@ export function toArray(value) {
|
||||
return [value];
|
||||
}
|
||||
|
||||
export function getVideoTags(video) {
|
||||
if (!video || typeof video !== "object") {
|
||||
return new Set();
|
||||
}
|
||||
|
||||
if (videoTagCache.has(video)) {
|
||||
return videoTagCache.get(video);
|
||||
}
|
||||
|
||||
const normalized = new Set();
|
||||
|
||||
const addTag = (rawTag) => {
|
||||
if (typeof rawTag !== "string") {
|
||||
return;
|
||||
}
|
||||
const tag = normalizeHashtag(rawTag);
|
||||
if (tag) {
|
||||
normalized.add(tag);
|
||||
}
|
||||
};
|
||||
|
||||
if (Array.isArray(video.tags)) {
|
||||
for (const tag of video.tags) {
|
||||
if (Array.isArray(tag) && tag[0] === "t" && typeof tag[1] === "string") {
|
||||
addTag(tag[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(video.nip71?.hashtags)) {
|
||||
for (const tag of video.nip71.hashtags) {
|
||||
addTag(tag);
|
||||
}
|
||||
}
|
||||
|
||||
videoTagCache.set(video, normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function hasDisinterestedTag(video, disinterestsSet) {
|
||||
if (!video || !disinterestsSet || !(disinterestsSet instanceof Set) || disinterestsSet.size === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const matchesDisinterest = (tag) => {
|
||||
const normalized = normalizeHashtag(tag);
|
||||
return Boolean(normalized && disinterestsSet.has(normalized));
|
||||
};
|
||||
|
||||
// Check raw tags
|
||||
if (Array.isArray(video.tags)) {
|
||||
for (const tag of video.tags) {
|
||||
if (Array.isArray(tag) && tag[0] === "t" && typeof tag[1] === "string") {
|
||||
if (matchesDisinterest(tag[1])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check NIP-71 metadata hashtags if available
|
||||
if (video.nip71 && Array.isArray(video.nip71.hashtags)) {
|
||||
for (const tag of video.nip71.hashtags) {
|
||||
if (typeof tag === "string" && matchesDisinterest(tag)) {
|
||||
return true;
|
||||
}
|
||||
const tags = getVideoTags(video);
|
||||
for (const tag of tags) {
|
||||
if (disinterestsSet.has(tag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user