add function diffArraysSimple for more efficient change detection

This commit is contained in:
Hazelnoot 2025-05-25 12:34:09 -04:00
parent 8a2ed3bc86
commit 35dfde838b
3 changed files with 89 additions and 13 deletions

View file

@ -14,7 +14,7 @@ import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import type { GlobalEvents } from '@/core/GlobalEventService.js';
import { Serialized } from '@/types.js';
import { diffArrays } from '@/misc/diff-arrays.js';
import { diffArrays, diffArraysSimple } from '@/misc/diff-arrays.js';
@Injectable()
export class FederatedInstanceService implements OnApplicationShutdown {
@ -99,11 +99,11 @@ export class FederatedInstanceService implements OnApplicationShutdown {
private syncCache(before: Serialized<MiMeta | undefined>, after: Serialized<MiMeta>): void {
const changed =
hasDiff(before?.blockedHosts, after.blockedHosts) ||
hasDiff(before?.silencedHosts, after.silencedHosts) ||
hasDiff(before?.mediaSilencedHosts, after.mediaSilencedHosts) ||
hasDiff(before?.federationHosts, after.federationHosts) ||
hasDiff(before?.bubbleInstances, after.bubbleInstances);
diffArraysSimple(before?.blockedHosts, after.blockedHosts) ||
diffArraysSimple(before?.silencedHosts, after.silencedHosts) ||
diffArraysSimple(before?.mediaSilencedHosts, after.mediaSilencedHosts) ||
diffArraysSimple(before?.federationHosts, after.federationHosts) ||
diffArraysSimple(before?.bubbleInstances, after.bubbleInstances);
if (changed) {
// We have to clear the whole thing, otherwise subdomains won't be synced.
@ -134,9 +134,3 @@ export class FederatedInstanceService implements OnApplicationShutdown {
this.dispose();
}
}
function hasDiff(before: string[] | null | undefined, after: string[] | null | undefined): boolean {
const { added, removed } = diffArrays(before, after);
return added.length > 0 || removed.length > 0;
}

View file

@ -56,3 +56,47 @@ export function diffArrays<T>(dataBefore: T[] | null | undefined, dataAfter: T[]
// data NEITHER before nor after => no change
return { added: [], removed: [] };
}
/**
* Checks for any difference between two snapshots of data.
* Null, undefined, and empty arrays are supported, and duplicate values are ignored.
* The inputs are treated as un-ordered, so a re-ordering of the same data will NOT be considered a change.
* @param dataBefore Array containing data before the change
* @param dataAfter Array containing data after the change
*/
export function diffArraysSimple<T>(dataBefore: T[] | null | undefined, dataAfter: T[] | null | undefined): boolean {
const before = dataBefore ? new Set(dataBefore) : null;
const after = dataAfter ? new Set(dataAfter) : null;
if (before?.size && after?.size) {
// different size => changed
if (before.size !== after.size) return true;
// removed => changed
for (const host of before) {
// delete operation removes duplicates to speed up the "after" loop
if (!after.delete(host)) {
return true;
}
}
// added => changed
for (const host of after) {
if (!before.has(host)) {
return true;
}
}
// identical values => no change
return false;
}
// before and NOT after => change
if (before?.size) return true;
// after and NOT before => change
if (after?.size) return true;
// NEITHER before nor after => no change
return false;
}

View file

@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { diffArrays } from '@/misc/diff-arrays.js';
import { diffArrays, diffArraysSimple } from '@/misc/diff-arrays.js';
describe(diffArrays, () => {
it('should return empty result when both inputs are null', () => {
@ -51,3 +51,41 @@ describe(diffArrays, () => {
expect(result.removed).toEqual(['b', 'd']);
});
});
describe(diffArraysSimple, () => {
it('should return false when both inputs are null', () => {
const result = diffArraysSimple(null, null);
expect(result).toBe(false);
});
it('should return false when both inputs are empty', () => {
const result = diffArraysSimple([], []);
expect(result).toBe(false);
});
it('should return true when before is populated and after is empty', () => {
const result = diffArraysSimple([1, 2, 3], []);
expect(result).toBe(true);
});
it('should return true when before is empty and after is populated', () => {
const result = diffArraysSimple([], [1, 2, 3]);
expect(result).toBe(true);
});
it('should return true when values have changed', () => {
const result = diffArraysSimple(
['a', 'a', 'b', 'c'],
['a', 'b', 'c', 'd'],
);
expect(result).toBe(true);
});
it('should return false when values have not changed', () => {
const result = diffArraysSimple(
['a', 'a', 'b', 'c'],
['a', 'b', 'c', 'c'],
);
expect(result).toBe(false);
});
});