RyotaUshio / obsidian-pdf-plus

The most Obsidian-native PDF annotation, viewing & editing tool ever. Comes with optional Vim keybindings.
https://ryotaushio.github.io/obsidian-pdf-plus/
MIT License
470 stars 11 forks source link

Performance degradation when a PDF has many backlink highlights #148

Closed RyotaUshio closed 2 months ago

RyotaUshio commented 2 months ago

Discussed in https://github.com/RyotaUshio/obsidian-pdf-plus/discussions/128

Originally posted by @uoFishbox March 25, 2024 First, I would like to express my sincere gratitude for this fantastic plugin. While I appreciate its functionality, I have encountered a performance issue. When I open a note and a PDF side by side on my mid-range PC and start typing in the note, I experience significant lag, especially when the PDF has 6 or more highlights. Here are my current settings (version: 0.39.2) ```json { "displayTextFormats": [ { "name": "Obsidian default", "template": "{{file.basename}}, page {{page}}" }, { "name": "Title & page", "template": "{{file.basename}}, p.{{pageLabel}}" }, { "name": "Page only", "template": "p.{{pageLabel}}" }, { "name": "Text", "template": "{{text}}" } ], "defaultDisplayTextFormatIndex": 0, "syncDisplayTextFormat": true, "syncDefaultDisplayTextFormat": false, "copyCommands": [ { "name": "Link only", "template": "{{linkWithDisplay}}" }, { "name": "Embed", "template": "!{{link}}" }, { "name": "Create new note", "template": "{{app.vault.create(text + \".md\", linkWithDisplay).then((file) => app.workspace.getLeaf(true).openFile(file)), \"\"}}" }, { "name": "list-pageonly", "template": "{{colorName}}:: *{{text.replaceAll(\"]\", \"\").replaceAll(\"[\",\"\")}}* {{linkWithDisplay.split(\"|\")[0] + \"|\" + linkWithDisplay.split(\"|\")[1].split(\", \")[1];}}" } ], "useAnotherCopyTemplateWhenNoSelection": true, "copyTemplateWhenNoSelection": "{{linkToPageWithDisplay}}", "trimSelectionEmbed": true, "embedMargin": 50, "noSidebarInEmbed": true, "noSpreadModeInEmbed": true, "embedUnscrollable": true, "singleTabForSinglePDF": true, "highlightExistingTab": false, "existingTabHighlightOpacity": 0.5, "existingTabHighlightDuration": 0.75, "paneTypeForFirstPDFLeaf": "", "openLinkNextToExistingPDFTab": true, "openPDFWithDefaultApp": false, "openPDFWithDefaultAppAndObsidian": true, "focusObsidianAfterOpenPDFWithDefaultApp": true, "syncWithDefaultApp": false, "dontActivateAfterOpenPDF": true, "dontActivateAfterOpenMD": true, "highlightDuration": 0.75, "noTextHighlightsInEmbed": false, "noAnnotationHighlightsInEmbed": true, "persistentTextHighlightsInEmbed": true, "persistentAnnotationHighlightsInEmbed": false, "highlightBacklinks": true, "selectionBacklinkVisualizeStyle": "highlight", "dblclickEmbedToOpenLink": true, "highlightBacklinksPane": true, "highlightOnHoverBacklinkPane": true, "backlinkHoverColor": "yellow", "colors": { "yellow": "#ffd000", "red": "#ea5252", "blue": "#086ddd", "green": "#4dcb5c" }, "defaultColor": "yellow", "defaultColorPaletteItemIndex": 1, "syncColorPaletteItem": false, "syncDefaultColorPaletteItem": false, "colorPaletteInToolbar": true, "noColorButtonInColorPalette": false, "colorPaletteInEmbedToolbar": false, "showStatusInToolbar": true, "highlightColorSpecifiedOnly": false, "doubleClickHighlightToOpenBacklink": true, "hoverHighlightAction": "preview", "paneTypeForFirstMDLeaf": "right", "singleMDLeafInSidebar": true, "alwaysUseSidebar": true, "ignoreExistingMarkdownTabIn": [], "defaultColorPaletteActionIndex": 3, "syncColorPaletteAction": true, "syncDefaultColorPaletteAction": true, "proxyMDProperty": "PDF", "hoverPDFLinkToOpen": false, "ignoreHeightParamInPopoverPreview": true, "filterBacklinksByPageDefault": true, "showBacklinkToPage": true, "enableHoverPDFInternalLink": true, "recordPDFInternalLinkHistory": true, "alwaysRecordHistory": true, "renderMarkdownInStickyNote": true, "enablePDFEdit": false, "author": "", "writeHighlightToFileOpacity": 0.2, "defaultWriteFileToggle": false, "syncWriteFileToggle": true, "syncDefaultWriteFileToggle": false, "enableAnnotationDeletion": true, "warnEveryAnnotationDelete": false, "warnBacklinkedAnnotationDelete": true, "enableAnnotationContentEdit": true, "enableEditEncryptedPDF": false, "pdfLinkColor": "#04a802", "pdfLinkBorder": false, "replaceContextMenu": false, "executeBuiltinCommandForOutline": true, "executeBuiltinCommandForZoom": true, "executeFontSizeAdjusterCommand": true, "closeSidebarWithShowCommandIfExist": true, "autoHidePDFSidebar": true, "outlineDrag": true, "outlineContextMenu": true, "outlineLinkDisplayTextFormat": "{{file.basename}}, {{text}}", "outlineLinkCopyFormat": "{{linkWithDisplay}}", "recordHistoryOnOutlineClick": true, "popoverPreviewOnOutlineHover": true, "thumbnailDrag": true, "thumbnailContextMenu": true, "thumbnailLinkDisplayTextFormat": "{{file.basename}}, page {{pageLabel}}", "thumbnailLinkCopyFormat": "{{linkWithDisplay}}", "recordHistoryOnThumbnailClick": true, "popoverPreviewOnThumbnailHover": true, "annotationPopupDrag": true, "useCallout": false, "calloutType": "PDF", "calloutIcon": "highlighter", "highlightBacklinksInEmbed": false, "highlightBacklinksInHoverPopover": false, "highlightBacklinksInCanvas": false, "clickPDFInternalLinkWithModifierKey": true, "clickOutlineItemWithModifierKey": true, "clickThumbnailWithModifierKey": true, "focusEditorAfterAutoPaste": true, "respectCursorPositionWhenAutoPaste": true, "autoCopy": true, "autoFocus": true, "autoPaste": false, "autoFocusTarget": "last-active-and-open-then-last-paste", "autoPasteTarget": "last-active-and-open-then-last-paste", "openAutoFocusTargetIfNotOpened": true, "howToOpenAutoFocusTargetIfNotOpened": "hover-editor", "closeHoverEditorWhenLostFocus": false, "closeSidebarWhenLostFocus": true, "openAutoFocusTargetInEditingView": true, "executeCommandWhenTargetNotIdentified": true, "commandToExecuteWhenTargetNotIdentified": "obsidian-hover-editor:open-new-popover", "autoPasteTargetDialogTimeoutSec": 20, "autoCopyToggleRibbonIcon": true, "autoCopyIconName": "highlighter", "autoFocusToggleRibbonIcon": true, "autoFocusIconName": "zap", "autoPasteToggleRibbonIcon": true, "autoPasteIconName": "clipboard-paste", "viewSyncFollowPageNumber": false, "viewSyncPageDebounceInterval": 1, "openAfterExtractPages": true, "howToOpenExtractedPDF": "tab", "warnEveryPageDelete": false, "warnBacklinkedPageDelete": true, "extractPageInPlace": false, "askExtractPageInPlace": true, "pageLabelUpdateWhenInsertPage": "keep", "pageLabelUpdateWhenDeletePage": "keep", "pageLabelUpdateWhenExtractPage": "keep", "askPageLabelUpdateWhenInsertPage": true, "askPageLabelUpdateWhenDeletePage": true, "askPageLabelUpdateWhenExtractPage": true, "copyOutlineAsListFormat": "{{linkWithDisplay}}", "copyOutlineAsListDisplayTextFormat": "{{text}}", "copyOutlineAsHeadingsFormat": "{{text}}\n\n{{linkWithDisplay}}", "copyOutlineAsHeadingsDisplayTextFormat": "p.{{pageLabel}}", "copyOutlineAsHeadingsMinLevel": 2, "newFileNameFormat": "", "newFileTemplatePath": "Templates/pdfplus.md", "newPDFLocation": "folder", "newPDFFolderPath": "PDF-notes", "rectEmbedStaticImage": false, "rectImageFormat": "file", "rectImageExtension": "webp", "zoomToFitRect": false, "rectEmbedResolution": 50, "includeColorWhenCopyingRectLink": false, "backlinkIconSize": 50, "showBacklinkIconForSelection": false, "showBacklinkIconForAnnotation": false, "showBacklinkIconForOffset": true, "showBacklinkIconForRect": false, "showBoundingRectForBacklinkedAnnot": false, "hideReplyAnnotation": true, "showCopyLinkToSearchInContextMenu": true, "searchLinkHighlightAll": "true", "searchLinkCaseSensitive": "true", "searchLinkMatchDiacritics": "default", "searchLinkEntireWord": "false", "dontFitWidthWhenOpenPDFLink": true, "preserveCurrentLeftOffsetWhenOpenPDFLink": false, "defaultZoomValue": "page-width", "scrollModeOnLoad": 0, "spreadModeOnLoad": 0, "hoverableDropdownMenuInToolbar": true, "zoomLevelInputBoxInToolbar": true, "popoverPreviewOnExternalLinkHover": true, "actionOnCitationHover": "google-scholar-popover", "anystylePath": "" } ``` It might be due to other plugins or my environment, but I would appreciate any advice you could provide.
RyotaUshio commented 2 months ago

Originally posted by @daknili in https://github.com/RyotaUshio/obsidian-pdf-plus/discussions/128#discussioncomment-9014699

First, thank you for this awesome plugin. Unfortunately I have the same problem as uoFishbox, but in my case the separate windows solution brings no relief. The issue in my case seems to be related to the file size of the pdf. I pretty much use the plugin to comment on entire books (> 10 MB) but the plugin gets quickly unusable. Disabling the plugin and then enabling it again somewhat helps. I'am on a Mac and performance degradation seems to be connected with high CPU usage of the Obsidian Helper (Renderer).

RyotaUshio commented 2 months ago

What is happening is something like this:

  1. The user types something in the editor
  2. After ~2 sec, the change is saved to the disk, and the events vault.on('modify', ...) & metadataCache.on('changed', ...) are fired
  3. The metadataCache.on('changed', ...) event triggers the update of the PDFBacklinkIndex: https://github.com/RyotaUshio/obsidian-pdf-plus/blob/45ab81f3b8548c1989d8e7499bcb152301596038/src/lib/pdf-backlink-index.ts#L25-L28
  4. This in turn calls the visualize() method of PDFViewerBacklinkVisualizer: https://github.com/RyotaUshio/obsidian-pdf-plus/blob/45ab81f3b8548c1989d8e7499bcb152301596038/src/backlink-visualizer.ts#L237-L239 https://github.com/RyotaUshio/obsidian-pdf-plus/blob/45ab81f3b8548c1989d8e7499bcb152301596038/src/backlink-visualizer.ts#L253

So it is likely that either (or both) of PDFBacklinkIndex.update() or PDFViewerBacklinkVisualizer.visualize() is consuming too much computational resources.

daknili commented 2 months ago

Thank you for your attention. Yes, the problem disappears if Highlight backlinks in PDF viewer is disabled.

RyotaUshio commented 2 months ago

After some investigation, it looks like this guy is the most performance-heavy.

https://github.com/RyotaUshio/obsidian-pdf-plus/blob/45ab81f3b8548c1989d8e7499bcb152301596038/src/lib/highlights/geometry.ts#L69

A possible solution is to cache the results of this function so that we can reuse them.

RyotaUshio commented 2 months ago

I've just beta-released 0.39.14-beta0 (BRAT only). @uoFishbox @daknili Can you test it and give me some feedback if you have time?

uoFishbox commented 2 months ago

Thanks for the new beta release.

Performance degradation when a PDF has many backlink highlights seems to have been almost completely resolved. I want to express my gratitude for this!

The phenomenon I posted previously, where the problem is greatly reduced when opened in another window, appears to be caused by the obsidian-livesync plugin.

It seems that about 60% of the performance degradation I was experiencing was caused by this plugin separately. (It is possible that my vault is too large. I will verify this on my end.)

daknili commented 2 months ago

Hello, I'm not familiar with installing beta plugins. Therefore, I installed the BRAT plugin and in the settings under the Add Beta plugin list I entered this address

https://github.com/RyotaUshio/obsidian-pdf-plus/releases/tag/0.39.14-beta0

But then the following message pops up

image

Is there anything I can do?

RyotaUshio commented 2 months ago

@daknili Thanks for trying! Use this URL instead, then it will work:

https://github.com/RyotaUshio/obsidian-pdf-plus
daknili commented 2 months ago

Yes, everything is fine now. Finally I'am able to work properly again. Your plugin is tremendously useful. Thank you!

RyotaUshio commented 2 months ago

Thank you guys for your contribution, this fix has been publically released as 0.39.14!