Merge pull request #3870 from soulsands/perf-count

perf: improve descendant count performance
This commit is contained in:
zadam 2023-04-28 21:53:41 +02:00 committed by GitHub
commit 4f1fafdf75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,9 +3,12 @@
const becca = require("../../becca/becca"); const becca = require("../../becca/becca");
const { JSDOM } = require("jsdom"); const { JSDOM } = require("jsdom");
const NotFoundError = require("../../errors/not_found_error"); const NotFoundError = require("../../errors/not_found_error");
function buildDescendantCountMap(noteIdsToCount) {
if (!Array.isArray(noteIdsToCount)) {
throw new Error('noteIdsToCount: type error');
}
function buildDescendantCountMap() { const noteIdToCountMap = Object.create(null);
const noteIdToCountMap = {};
function getCount(noteId) { function getCount(noteId) {
if (!(noteId in noteIdToCountMap)) { if (!(noteId in noteIdToCountMap)) {
@ -24,12 +27,12 @@ function buildDescendantCountMap() {
return noteIdToCountMap[noteId]; return noteIdToCountMap[noteId];
} }
noteIdsToCount.forEach((noteId) => {
getCount('root'); getCount(noteId);
});
return noteIdToCountMap; return noteIdToCountMap;
} }
/** /**
* @param {BNote} note * @param {BNote} note
* @param {int} depth * @param {int} depth
@ -119,7 +122,9 @@ function getLinkMap(req) {
noteIds.add(noteId); noteIds.add(noteId);
} }
const notes = Array.from(noteIds).map(noteId => { const noteIdsArray = Array.from(noteIds)
const notes = noteIdsArray.map(noteId => {
const note = becca.getNote(noteId); const note = becca.getNote(noteId);
return [ return [
@ -155,7 +160,7 @@ function getLinkMap(req) {
return { return {
notes: notes, notes: notes,
noteIdToDescendantCountMap: buildDescendantCountMap(), noteIdToDescendantCountMap: buildDescendantCountMap(noteIdsArray),
links: links links: links
}; };
} }
@ -209,7 +214,7 @@ function getTreeMap(req) {
}); });
} }
const noteIdToDescendantCountMap = buildDescendantCountMap(); const noteIdToDescendantCountMap = buildDescendantCountMap(Array.from(noteIds));
updateDescendantCountMapForSearch(noteIdToDescendantCountMap, subtree.relationships); updateDescendantCountMapForSearch(noteIdToDescendantCountMap, subtree.relationships);