From 6bd8c22ef722a87368e8a9d9cb6aa22c244ba61e Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 14 Aug 2023 01:12:30 -0400 Subject: [PATCH] Adds a `maybeStopWatch` function to avoid overhead --- src/annotations/gutterBlameAnnotationProvider.ts | 8 +++---- src/annotations/gutterChangesAnnotationProvider.ts | 12 +++++----- .../gutterHeatmapBlameAnnotationProvider.ts | 8 +++---- src/env/node/git/locator.ts | 10 ++++----- src/plus/github/github.ts | 6 ++--- src/plus/gitlab/gitlab.ts | 19 +++++----------- src/system/logger.ts | 2 +- src/system/stopwatch.ts | 26 +++++++++------------- 8 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/annotations/gutterBlameAnnotationProvider.ts b/src/annotations/gutterBlameAnnotationProvider.ts index 297f217..9190179 100644 --- a/src/annotations/gutterBlameAnnotationProvider.ts +++ b/src/annotations/gutterBlameAnnotationProvider.ts @@ -13,7 +13,7 @@ import { configuration } from '../system/configuration'; import { log } from '../system/decorators/log'; import { first } from '../system/iterable'; import { getLogScope } from '../system/logger.scope'; -import { Stopwatch } from '../system/stopwatch'; +import { maybeStopWatch } from '../system/stopwatch'; import type { TokenOptions } from '../system/string'; import { getTokensFromTemplate, getWidth } from '../system/string'; import type { GitDocumentState } from '../trackers/gitDocumentTracker'; @@ -49,7 +49,7 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase { const blame = await this.getBlame(); if (blame == null) return false; - const sw = new Stopwatch(scope); + const sw = maybeStopWatch(scope); const cfg = configuration.get('blame'); @@ -162,14 +162,14 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase { decorationsMap.set(l.sha, gutter); } - sw.restart({ suffix: ' to compute gutter blame annotations' }); + sw?.restart({ suffix: ' to compute gutter blame annotations' }); if (decorationOptions.length) { this.setDecorations([ { decorationType: Decorations.gutterBlameAnnotation, rangesOrOptions: decorationOptions }, ]); - sw.stop({ suffix: ' to apply all gutter blame annotations' }); + sw?.stop({ suffix: ' to apply all gutter blame annotations' }); } this.registerHoverProviders(configuration.get('hovers.annotations')); diff --git a/src/annotations/gutterChangesAnnotationProvider.ts b/src/annotations/gutterChangesAnnotationProvider.ts index 485f3f0..89c9163 100644 --- a/src/annotations/gutterChangesAnnotationProvider.ts +++ b/src/annotations/gutterChangesAnnotationProvider.ts @@ -10,12 +10,12 @@ import { Hover, languages, Position, Range, Selection, TextEditorRevealType } fr import { FileAnnotationType } from '../config'; import type { Container } from '../container'; import type { GitCommit } from '../git/models/commit'; -import type { GitDiff } from '../git/models/diff'; +import type { GitDiffFile } from '../git/models/diff'; import { localChangesMessage } from '../hovers/hovers'; import { configuration } from '../system/configuration'; import { log } from '../system/decorators/log'; import { getLogScope } from '../system/logger.scope'; -import { Stopwatch } from '../system/stopwatch'; +import { maybeStopWatch } from '../system/stopwatch'; import type { GitDocumentState, TrackedDocument } from '../trackers/gitDocumentTracker'; import type { AnnotationContext } from './annotationProvider'; import { AnnotationProviderBase } from './annotationProvider'; @@ -29,7 +29,7 @@ export interface ChangesAnnotationContext extends AnnotationContext { } export class GutterChangesAnnotationProvider extends AnnotationProviderBase { - private state: { commit: GitCommit | undefined; diffs: GitDiff[] } | undefined; + private state: { commit: GitCommit | undefined; diffs: GitDiffFile[] } | undefined; private hoverProviderDisposable: Disposable | undefined; constructor( @@ -156,7 +156,7 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase(d?: T): d is T => Boolean(d)); if (!diffs?.length) return false; - const sw = new Stopwatch(scope); + const sw = maybeStopWatch(scope); const decorationsMap = new Map< string, @@ -259,12 +259,12 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase { - const sw = new Stopwatch(`findSpecificGit(${path})`, { logLevel: LogLevel.Debug }); + const sw = maybeStopWatch(`findSpecificGit(${path})`, { logLevel: LogLevel.Debug }); let version; try { version = await run(path, ['--version'], 'utf8'); } catch (ex) { - sw.stop({ message: ` ${GlyphChars.Dot} Unable to find git: ${ex}` }); + sw?.stop({ message: ` ${GlyphChars.Dot} Unable to find git: ${ex}` }); if (/bad config/i.test(ex.message)) throw new InvalidGitConfigError(ex); throw ex; @@ -48,7 +48,7 @@ async function findSpecificGit(path: string): Promise { try { version = await run(foundPath, ['--version'], 'utf8'); } catch (ex) { - sw.stop({ message: ` ${GlyphChars.Dot} Unable to find git: ${ex}` }); + sw?.stop({ message: ` ${GlyphChars.Dot} Unable to find git: ${ex}` }); if (/bad config/i.test(ex.message)) throw new InvalidGitConfigError(ex); throw ex; @@ -62,7 +62,7 @@ async function findSpecificGit(path: string): Promise { .replace(/^git version /, '') .trim(); - sw.stop({ message: ` ${GlyphChars.Dot} Found ${parsed} in ${path}; ${version}` }); + sw?.stop({ message: ` ${GlyphChars.Dot} Found ${parsed} in ${path}; ${version}` }); return { path: path, diff --git a/src/plus/github/github.ts b/src/plus/github/github.ts index 6571833..0e9edc0 100644 --- a/src/plus/github/github.ts +++ b/src/plus/github/github.ts @@ -38,7 +38,7 @@ import { Logger } from '../../system/logger'; import { LogLevel } from '../../system/logger.constants'; import type { LogScope } from '../../system/logger.scope'; import { getLogScope } from '../../system/logger.scope'; -import { Stopwatch } from '../../system/stopwatch'; +import { maybeStopWatch } from '../../system/stopwatch'; import { base64 } from '../../system/string'; import type { Version } from '../../system/version'; import { fromString, satisfies } from '../../system/version'; @@ -2233,7 +2233,7 @@ export class GitHubApi implements Disposable { if (Logger.logLevel === LogLevel.Debug || Logger.isDebugging) { octokit.hook.wrap('request', async (request, options) => { - const stopwatch = new Stopwatch(`[GITHUB] ${options.method} ${options.url}`, { log: false }); + const sw = maybeStopWatch(`[GITHUB] ${options.method} ${options.url}`, { log: false }); try { return await request(options); } finally { @@ -2244,7 +2244,7 @@ export class GitHubApi implements Disposable { message = ` ${match?.[1].trim() ?? options.query}`; } } catch {} - stopwatch.stop({ message: message }); + sw?.stop({ message: message }); } }); } diff --git a/src/plus/gitlab/gitlab.ts b/src/plus/gitlab/gitlab.ts index 8af2e1b..a529fa6 100644 --- a/src/plus/gitlab/gitlab.ts +++ b/src/plus/gitlab/gitlab.ts @@ -28,10 +28,9 @@ import { import { configuration } from '../../system/configuration'; import { debug } from '../../system/decorators/log'; import { Logger } from '../../system/logger'; -import { LogLevel } from '../../system/logger.constants'; import type { LogScope } from '../../system/logger.scope'; import { getLogScope, setLogScopeExit } from '../../system/logger.scope'; -import { Stopwatch } from '../../system/stopwatch'; +import { maybeStopWatch } from '../../system/stopwatch'; import { equalsIgnoreCase } from '../../system/string'; import type { GitLabCommit, @@ -752,11 +751,7 @@ $search: String! ): Promise { let rsp: Response; try { - const stopwatch = - Logger.logLevel === LogLevel.Debug || Logger.isDebugging - ? new Stopwatch(`[GITLAB] POST ${baseUrl}`, { log: false }) - : undefined; - + const sw = maybeStopWatch(`[GITLAB] POST ${baseUrl}`, { log: false }); const agent = this.getProxyAgent(provider); try { @@ -781,7 +776,7 @@ $search: String! const match = /(^[^({\n]+)/.exec(query); const message = ` ${match?.[1].trim() ?? query}`; - stopwatch?.stop({ message: message }); + sw?.stop({ message: message }); } } catch (ex) { if (ex instanceof ProviderFetchError) { @@ -806,11 +801,7 @@ $search: String! let rsp: Response; try { - const stopwatch = - Logger.logLevel === LogLevel.Debug || Logger.isDebugging - ? new Stopwatch(`[GITLAB] ${options?.method ?? 'GET'} ${url}`, { log: false }) - : undefined; - + const sw = maybeStopWatch(`[GITLAB] ${options?.method ?? 'GET'} ${url}`, { log: false }); const agent = this.getProxyAgent(provider); try { @@ -829,7 +820,7 @@ $search: String! throw new ProviderFetchError('GitLab', rsp); } finally { - stopwatch?.stop(); + sw?.stop(); } } catch (ex) { if (ex instanceof ProviderFetchError) { diff --git a/src/system/logger.ts b/src/system/logger.ts index 3737293..93a51f2 100644 --- a/src/system/logger.ts +++ b/src/system/logger.ts @@ -238,7 +238,7 @@ export interface LogProvider { } export const defaultLogProvider: LogProvider = { - enabled: (logLevel: LogLevel) => Logger.enabled(logLevel), + enabled: (logLevel: LogLevel) => Logger.enabled(logLevel) || Logger.isDebugging, log: (logLevel: LogLevel, scope: LogScope | undefined, message: string, ...params: any[]) => { switch (logLevel) { case LogLevel.Error: diff --git a/src/system/stopwatch.ts b/src/system/stopwatch.ts index fa92dd9..1de0069 100644 --- a/src/system/stopwatch.ts +++ b/src/system/stopwatch.ts @@ -24,7 +24,7 @@ export class Stopwatch { } constructor( - public readonly scope: string | LogScope | undefined, + private readonly scope: string | LogScope | undefined, options?: StopwatchOptions, ...params: any[] ) { @@ -117,20 +117,14 @@ export class Stopwatch { `${prefix ? `${prefix} ` : ''}[${ms}ms]${options?.suffix ?? ''}`, ); } +} - private static readonly watches = new Map(); - - static start(key: string, options?: StopwatchOptions, ...params: any[]): void { - Stopwatch.watches.get(key)?.log(); - Stopwatch.watches.set(key, new Stopwatch(key, options, ...params)); - } - - static log(key: string, options?: StopwatchLogOptions): void { - Stopwatch.watches.get(key)?.log(options); - } - - static stop(key: string, options?: StopwatchLogOptions): void { - Stopwatch.watches.get(key)?.stop(options); - Stopwatch.watches.delete(key); - } +export function maybeStopWatch( + scope: string | LogScope | undefined, + options?: StopwatchOptions, + ...params: any[] +): Stopwatch | undefined { + return (options?.provider ?? defaultLogProvider).enabled(options?.logLevel ?? LogLevel.Info) + ? new Stopwatch(scope, options, ...params) + : undefined; }