Investigate - Slow transformRichTextV2 function (#16535)

Investigate CPU overloading ...

related to https://github.com/twentyhq/twenty/issues/15916
This commit is contained in:
Etienne
2025-12-12 18:39:01 +01:00
committed by GitHub
parent 44f0cfdd9e
commit d36d0d49d1
@@ -1,3 +1,5 @@
import { Logger } from '@nestjs/common';
import { isNonEmptyString } from '@sniptt/guards';
import {
type RichTextV2Metadata,
@@ -5,14 +7,32 @@ import {
} from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
const logger = new Logger('TransformRichTextV2');
const calculateInputSize = (input: unknown): number => {
if (typeof input === 'string') {
return Buffer.byteLength(input, 'utf8');
}
if (typeof input === 'object' && input !== null) {
return Buffer.byteLength(JSON.stringify(input), 'utf8');
}
return 0;
};
export const transformRichTextV2Value = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
richTextValue: any,
): Promise<RichTextV2Metadata> => {
const startTime = performance.now();
const inputSize = calculateInputSize(richTextValue);
const parsedValue = isNonEmptyString(richTextValue)
? richTextV2ValueSchema.parse(richTextValue)
: richTextValue;
const afterParsingTime = performance.now();
const { ServerBlockNoteEditor } = await import('@blocknote/server-util');
const serverBlockNoteEditor = ServerBlockNoteEditor.create();
@@ -31,6 +51,8 @@ export const transformRichTextV2Value = async (
convertedMarkdown = parsedValue.blocknote || null;
}
const afterMarkdownConversionTime = performance.now();
const convertedBlocknote = parsedValue.markdown
? JSON.stringify(
await serverBlockNoteEditor.tryParseMarkdownToBlocks(
@@ -39,6 +61,12 @@ export const transformRichTextV2Value = async (
)
: null;
const endTime = performance.now();
logger.debug(
`transformRichTextV2Value completed - total: ${endTime - startTime}ms, parsing: ${afterParsingTime - startTime}ms, markdown_conversion: ${afterMarkdownConversionTime - afterParsingTime}ms, blocknote_conversion: ${endTime - afterMarkdownConversionTime}ms, size: ${inputSize} bytes`,
);
return {
markdown: parsedValue.markdown || convertedMarkdown,
blocknote: parsedValue.blocknote || convertedBlocknote,