Skip to content

Commit

Permalink
chore: add a few timers for improved profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydw committed Jun 28, 2021
1 parent 22430db commit 1a52faa
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 50 deletions.
2 changes: 2 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class Cache {
pod: Pod;
collections!: Record<string, Collection>;
docs!: Record<string, Document>;
fileExists!: Record<string, Boolean>;
interpolations!: Record<string, Function>;
locales!: Record<string, Locale>;
routeMap!: Record<string, Route>;
Expand All @@ -31,6 +32,7 @@ export class Cache {
clearAll() {
this.collections = {};
this.docs = {};
this.fileExists = {};
this.interpolations = {};
this.locales = {};
this.routeMap = {};
Expand Down
27 changes: 17 additions & 10 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,24 @@ export class Document {

await this.resolveFields(defaultContext);

// When `$view: self` is used, use the document's body as the template.
if (this.view === Document.SelfReferencedView) {
const templateEngine = this.pod.engines.getEngineByFilename(this.podPath);
return templateEngine.renderFromString(
this.body as string,
defaultContext
);
}
const timer = this.pod.profiler.timer('document.render', 'Document render');
try {
// When `$view: self` is used, use the document's body as the template.
if (this.view === Document.SelfReferencedView) {
const templateEngine = this.pod.engines.getEngineByFilename(
this.podPath
);
return templateEngine.renderFromString(
this.body as string,
defaultContext
);
}

const templateEngine = this.pod.engines.getEngineByFilename(this.view);
return templateEngine.render(this.view, defaultContext);
const templateEngine = this.pod.engines.getEngineByFilename(this.view);
return templateEngine.render(this.view, defaultContext);
} finally {
timer.stop();
}
}

/**
Expand Down
73 changes: 41 additions & 32 deletions src/locale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Document} from './document';
import {Pod} from './pod';
import {Timer} from './profile';
import {TranslationString} from './string';

const RTL_REGEX = /^(ar|fa|he|ur)(\W|$)/;
Expand Down Expand Up @@ -90,45 +91,53 @@ export class Locale {
}

getTranslation(value: Translatable, location?: Document) {
if (!value) {
return value;
}
const timer = this.pod.profiler.timer(
'locale.getTranslation',
'Locale get translation'
);
try {
if (!value) {
return value;
}

const string = this.toTranslationString(value);
const string = this.toTranslationString(value);

// Return the preferred string without checking translations, if in the
// default locale.
if (string.prefer && this === this.pod.defaultLocale) {
return string.prefer;
}
// Return the preferred string without checking translations, if in the
// default locale.
if (string.prefer && this === this.pod.defaultLocale) {
return string.prefer;
}

if (!this.pod.fileExists(this.podPath) || !this.translations) {
this.recordMissingString(string, location);
return string.value;
}
if (!this.pod.fileExists(this.podPath) || !this.translations) {
this.recordMissingString(string, location);
return string.value;
}

// Check for the translation of the preferred value and return it. This
// permits specification of a "preferred" string, for instances where some
// locales may have translations and others may not. Usually, this is useful
// to support updating source languages without waiting for new translations
// to arrive.
if (string.prefer) {
const preferredValue = this.translations[string.prefer];
if (preferredValue) {
return preferredValue;
// Check for the translation of the preferred value and return it. This
// permits specification of a "preferred" string, for instances where some
// locales may have translations and others may not. Usually, this is useful
// to support updating source languages without waiting for new translations
// to arrive.
if (string.prefer) {
const preferredValue = this.translations[string.prefer];
if (preferredValue) {
return preferredValue;
}
this.recordMissingString(string, location);
}
this.recordMissingString(string, location);
}

// Collect the string because the preferred translation is missing.
const foundValue = this.translations[string.value];
if (foundValue) {
return foundValue;
}
// Collect the string because the preferred translation is missing.
const foundValue = this.translations[string.value];
if (foundValue) {
return foundValue;
}

// No translation was found at all, fall back to the source string.
this.recordMissingString(string, location);
return string.value;
// No translation was found at all, fall back to the source string.
this.recordMissingString(string, location);
return string.value;
} finally {
timer.stop();
}
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/plugins/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,18 @@ export class YamlPlugin implements PluginComponent {
// value can be: /content/partials/base.yaml
// value can be: /content/partials/base.yaml?foo
// value can be: /content/partials/base.yaml?foo.bar.baz
const parts = value.split('?');
const podPath = parts[0];
const result = this.pod.readYaml(podPath);
if (parts.length > 1) {
return utils.queryObject(parts[1], result);
} else {
return result;
const timer = this.pod.profiler.timer('yaml.query', 'Yaml query');
try {
const parts = value.split('?');
const podPath = parts[0];
const result = this.pod.readYaml(podPath);
if (parts.length > 1) {
return utils.queryObject(parts[1], result);
} else {
return result;
}
} finally {
timer.stop();
}
},
represent: string => {
Expand Down
8 changes: 7 additions & 1 deletion src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,18 @@ export class Pod {
* @param podPath The podPath to the file.
*/
fileExists(podPath: string) {
if (this.cache.fileExists[podPath] !== undefined) {
return this.cache.fileExists[podPath];
}
const timer = this.profiler.timer('file.exists', 'File exists');
try {
return existsSync(this.getAbsoluteFilePath(podPath));
this.cache.fileExists[podPath] = existsSync(
this.getAbsoluteFilePath(podPath)
);
} finally {
timer.stop();
}
return this.cache.fileExists[podPath];
}

/**
Expand Down

0 comments on commit 1a52faa

Please sign in to comment.