Skip to content

Commit

Permalink
Optimizes gravatar lookup, since this is perf sensative code
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 6, 2018
1 parent 52720ad commit 079b6f4
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/git/models/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Git } from '../git';
import { GitUri } from '../gitUri';

const gravatarCache: Map<string, Uri> = new Map();
const missingGravatarHash = '00000000000000000000000000000000';

export function clearGravatarCache() {
gravatarCache.clear();
}
Expand Down Expand Up @@ -178,16 +180,16 @@ export abstract class GitCommit {
}

getGravatarUri(fallback: GravatarDefaultStyle, size: number = 16): Uri {
const key = this.email ? `${this.email.trim().toLowerCase()}:${size}` : '';
const hash =
this.email != null && this.email.length !== 0
? Strings.md5(this.email.trim().toLowerCase(), 'hex')
: missingGravatarHash;

const key = `${hash}:${size}`;
let gravatar = gravatarCache.get(key);
if (gravatar !== undefined) return gravatar;

gravatar = Uri.parse(
`https://www.gravatar.com/avatar/${
this.email ? Strings.md5(this.email.trim().toLowerCase(), 'hex') : '00000000000000000000000000000000'
}.jpg?s=${size}&d=${fallback}`
);
gravatar = Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`);
gravatarCache.set(key, gravatar);

return gravatar;
Expand Down

0 comments on commit 079b6f4

Please sign in to comment.