Sfoglia il codice sorgente

Adds a `maybeStopWatch` function to avoid overhead

main
Eric Amodio 1 anno fa
parent
commit
6bd8c22ef7
8 ha cambiato i file con 38 aggiunte e 53 eliminazioni
  1. +4
    -4
      src/annotations/gutterBlameAnnotationProvider.ts
  2. +6
    -6
      src/annotations/gutterChangesAnnotationProvider.ts
  3. +4
    -4
      src/annotations/gutterHeatmapBlameAnnotationProvider.ts
  4. +5
    -5
      src/env/node/git/locator.ts
  5. +3
    -3
      src/plus/github/github.ts
  6. +5
    -14
      src/plus/gitlab/gitlab.ts
  7. +1
    -1
      src/system/logger.ts
  8. +10
    -16
      src/system/stopwatch.ts

+ 4
- 4
src/annotations/gutterBlameAnnotationProvider.ts Vedi File

@ -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'));

+ 6
- 6
src/annotations/gutterChangesAnnotationProvider.ts Vedi File

@ -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<ChangesAnnotationContext> {
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
).filter(<T>(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
}
}
sw.restart({ suffix: ' to compute recent changes annotations' });
sw?.restart({ suffix: ' to compute recent changes annotations' });
if (decorationsMap.size) {
this.setDecorations([...decorationsMap.values()]);
sw.stop({ suffix: ' to apply all recent changes annotations' });
sw?.stop({ suffix: ' to apply all recent changes annotations' });
if (selection != null && context?.selection !== false) {
this.editor.selection = selection;

+ 4
- 4
src/annotations/gutterHeatmapBlameAnnotationProvider.ts Vedi File

@ -5,7 +5,7 @@ import type { Container } from '../container';
import type { GitCommit } from '../git/models/commit';
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 } from '../trackers/gitDocumentTracker';
import type { TrackedDocument } from '../trackers/trackedDocument';
import type { AnnotationContext } from './annotationProvider';
@ -26,7 +26,7 @@ export class GutterHeatmapBlameAnnotationProvider extends BlameAnnotationProvide
const blame = await this.getBlame();
if (blame == null) return false;
const sw = new Stopwatch(scope);
const sw = maybeStopWatch(scope);
const decorationsMap = new Map<
string,
@ -50,12 +50,12 @@ export class GutterHeatmapBlameAnnotationProvider extends BlameAnnotationProvide
);
}
sw.restart({ suffix: ' to compute heatmap annotations' });
sw?.restart({ suffix: ' to compute heatmap annotations' });
if (decorationsMap.size) {
this.setDecorations([...decorationsMap.values()]);
sw.stop({ suffix: ' to apply all heatmap annotations' });
sw?.stop({ suffix: ' to apply all heatmap annotations' });
}
// this.registerHoverProviders(configuration.get('hovers.annotations'));

+ 5
- 5
src/env/node/git/locator.ts Vedi File

@ -3,7 +3,7 @@ import * as process from 'process';
import { GlyphChars } from '../../../constants';
import { LogLevel } from '../../../system/logger.constants';
import { any } from '../../../system/promise';
import { Stopwatch } from '../../../system/stopwatch';
import { maybeStopWatch } from '../../../system/stopwatch';
import { findExecutable, run } from './shell';
export class UnableToFindGitError extends Error {
@ -28,13 +28,13 @@ export interface GitLocation {
}
async function findSpecificGit(path: string): Promise<GitLocation> {
const sw = new Stopwatch(`findSpecificGit(${path})`, { logLevel: LogLevel.Debug });
const sw = maybeStopWatch(`findSpecificGit(${path})`, { logLevel: LogLevel.Debug });
let version;
try {
version = await run<string>(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<string>(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,

+ 3
- 3
src/plus/github/github.ts Vedi File

@ -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 });
}
});
}

+ 5
- 14
src/plus/gitlab/gitlab.ts Vedi File

@ -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<T | undefined> {
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) {

+ 1
- 1
src/system/logger.ts Vedi File

@ -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:

+ 10
- 16
src/system/stopwatch.ts Vedi File

@ -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<string, Stopwatch>();
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;
}

Caricamento…
Annulla
Salva