Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension causes high cpu load #114

Open
genesisbertiz opened this issue Jan 31, 2023 · 7 comments
Open

Extension causes high cpu load #114

genesisbertiz opened this issue Jan 31, 2023 · 7 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@genesisbertiz
Copy link

  • Issue Type: Performance
  • Extension Name: op-vscode
  • Extension Version: 1.0.4
  • OS Version: Windows_NT x64 10.0.22621
  • VS Code version: 1.74.3

⚠️ Make sure to attach this file from your home-directory:
⚠️file:///c%3A/Users/GENESI~1.BER/AppData/Local/Temp/1Password.op-vscode-unresponsive.cpuprofile.txt

Find more details here: https://github.com/microsoft/vscode/wiki/Explain-extension-causes-high-cpu-load

@jodyheavener
Copy link
Member

Hey @genesisbertiz! Thanks for reporting this. I haven't done too much debugging when it comes to CPU load and the extension so I appreciate you bringing this to our attention. I've added this to our team's radar.

@jodyheavener jodyheavener added the bug Something isn't working label May 26, 2023
@alexanderniebuhr
Copy link

I have the same issue on macOs. The extension is unusable for me.

@SaidAlSouti
Copy link

Same issue on MacOS, when having 15 MB csv file

@jodyheavener
Copy link
Member

Hey all, we're looking into this issue. I haven't personally come across warnings about high CPU load on my end, so if someone is able to upload the 1Password.op-vscode-unresponsive.cpuprofile.txt file they are provided, I can examine and use it to debug. Thank you!

@jodyheavener jodyheavener added the help wanted Extra attention is needed label May 23, 2024
@alias-lightheart
Copy link

alias-lightheart commented Jun 12, 2024

@jodyheavener If I was interested in contributing to this repository could I make many performance fixes? I have been programming javascript for about 5 or so years now and think I could help. I enjoy making visual studio code extensions and since I am paying for 1password I figured I might as well help make a lot of improvements.

(Edit 1): Just let me know if you are down for me to create a pull request and contribute to a lot of fixes that I find. Mention me and I will respond within probably a couple business hours.

(Edit 2): Never mind, I found out there are the contributing guidelines. I apologize lol

@khallmark
Copy link

I regularly experience this issue. Here's the cpuprofile.

1Password.op-vscode-unresponsive.cpuprofile

@khallmark
Copy link

I'll open a PR later once I've tested the fix. Need to find the spot it goes in the source. I was running into issues tonight and so I ran it through the Cursor chat. "Sebastien" (my AI assistants name) found this:

// Cache compiled regex patterns outside function for single initialization
const compiledPatterns = Vs.map(l => ({
    pattern: new RegExp(l.pattern),
    original: l
}));

// Optimized be function
be = (r, t = !1) => {
    // Early return if input is empty or too long
    if (!r || r.length > 10000) return;
    
    let i = Ax.exec(r);
    if (!i) return;
    
    let s = i[0], 
        a = i.index;
        
    // Skip if conditions aren't met
    if (!t && r !== s || t && !ni(r, s)) return;

    // Use cached patterns
    for (let {pattern, original} of compiledPatterns) {
        if (pattern.test(s)) {
            let n = {...original};
            if (!n.item) n.item = Ys(r);
            return {value: s, index: a, suggestion: n};
        }
    }
    
    return;
}

Full output from the LLM:

Your Grace, I shall analyze the code and recommend a fix.

/analyze

  1. Problem Identification:
  • The issue occurs in the be() function which is called during parsing
  • The function is causing the extension host to become unresponsive
  • This happens during CodeLens provision, suggesting a performance bottleneck
  • Looking at the code, it's performing regex pattern matching that may be inefficient
  1. Code Analysis:
// Current problematic code
be = (r, t = !1) => {
    let i = Ax.exec(r);
    if (!i) return;
    let n, s = i[0], a = i.index;
    if (!(!t && r !== s || t && !ni(r, s))) {
        for (let l of Vs) 
            if (new RegExp(l.pattern).test(s)) {
                n = l;
                n.item || (n.item = Ys(r));
                break;
            }
        return {value: s, index: a, suggestion: n}
    }
}
  1. Issues Found:
  • Creating new RegExp objects in a loop
  • Multiple regex executions on the same string
  • No caching of compiled patterns
  • Potential catastrophic backtracking in regex patterns

/recommend

Here's my recommended fix:

// Cache compiled regex patterns
const compiledPatterns = Vs.map(l => ({
    pattern: new RegExp(l.pattern),
    original: l
}));

be = (r, t = !1) => {
    // Early return if input is empty or too long
    if (!r || r.length > 10000) return;
    
    let i = Ax.exec(r);
    if (!i) return;
    
    let s = i[0], 
        a = i.index;
        
    // Skip if conditions aren't met
    if (!t && r !== s || t && !ni(r, s)) return;

    // Use cached patterns
    for (let {pattern, original} of compiledPatterns) {
        if (pattern.test(s)) {
            let n = {...original};
            if (!n.item) n.item = Ys(r);
            return {value: s, index: a, suggestion: n};
        }
    }
    
    return;
}

Key improvements:

  1. Pattern Caching: Pre-compile all regex patterns once instead of creating new RegExp objects in the loop
  2. Early Returns: Add early return conditions to avoid unnecessary processing
  3. Memory Efficiency: Create suggestion object only when needed
  4. Input Validation: Add basic input validation to prevent processing of empty or extremely long strings
  5. Immutability: Clone the original pattern object to avoid mutation issues

Would you like me to proceed with implementing these changes?

— Your Humble Servant, Sebastien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants