Skip to content

Commit c83f84c

Browse files
authored
Merge pull request #2613 from strongloop/fix/globalize-perf-2x
Fix performance of globalization
2 parents 3719ac4 + 3b88753 commit c83f84c

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

lib/persisted-model.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ var debug = require('debug')('loopback:persisted-model');
1616
var PassThrough = require('stream').PassThrough;
1717
var utils = require('./utils');
1818

19+
// workaround for low performance of strong-globalize
20+
// see https://github.com/strongloop/strong-globalize/issues/66
21+
var stringCache = Object.create(null);
22+
g.s = function(str) {
23+
assert.equal(1, arguments.length, 'g.s() does not support parameters');
24+
if (str in stringCache)
25+
return stringCache[str];
26+
var result = g.t(str);
27+
stringCache[str] = result;
28+
return result;
29+
};
30+
1931
module.exports = function(registry) {
2032
var Model = registry.getModel('Model');
2133

@@ -560,7 +572,7 @@ module.exports = function(registry) {
560572
}
561573

562574
setRemoting(PersistedModel, 'create', {
563-
description: g.f('Create a new instance of the model and persist it into the data source.'),
575+
description: g.s('Create a new instance of the model and persist it into the data source.'),
564576
accessType: 'WRITE',
565577
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
566578
returns: {arg: 'data', type: typeName, root: true},
@@ -569,7 +581,7 @@ module.exports = function(registry) {
569581

570582
setRemoting(PersistedModel, 'upsert', {
571583
aliases: ['updateOrCreate'],
572-
description: g.f('Update an existing model instance or insert a new one ' +
584+
description: g.s('Update an existing model instance or insert a new one ' +
573585
'into the data source.'),
574586
accessType: 'WRITE',
575587
accepts: {arg: 'data', type: 'object', description: 'Model instance data', http: {source: 'body'}},
@@ -578,7 +590,7 @@ module.exports = function(registry) {
578590
});
579591

580592
setRemoting(PersistedModel, 'exists', {
581-
description: g.f('Check whether a model instance exists in the data source.'),
593+
description: g.s('Check whether a model instance exists in the data source.'),
582594
accessType: 'READ',
583595
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true},
584596
returns: {arg: 'exists', type: 'boolean'},
@@ -609,29 +621,29 @@ module.exports = function(registry) {
609621
});
610622

611623
setRemoting(PersistedModel, 'findById', {
612-
description: g.f('Find a model instance by {{id}} from the data source.'),
624+
description: g.s('Find a model instance by {{id}} from the data source.'),
613625
accessType: 'READ',
614626
accepts: [
615627
{ arg: 'id', type: 'any', description: 'Model id', required: true,
616628
http: {source: 'path'}},
617629
{ arg: 'filter', type: 'object',
618-
description: g.f('Filter defining fields and include') },
630+
description: g.s('Filter defining fields and include') },
619631
],
620632
returns: {arg: 'data', type: typeName, root: true},
621633
http: {verb: 'get', path: '/:id'},
622634
rest: {after: convertNullToNotFoundError}
623635
});
624636

625637
setRemoting(PersistedModel, 'find', {
626-
description: g.f('Find all instances of the model matched by filter from the data source.'),
638+
description: g.s('Find all instances of the model matched by filter from the data source.'),
627639
accessType: 'READ',
628640
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit'},
629641
returns: {arg: 'data', type: [typeName], root: true},
630642
http: {verb: 'get', path: '/'}
631643
});
632644

633645
setRemoting(PersistedModel, 'findOne', {
634-
description: g.f('Find first instance of the model matched by filter from the data source.'),
646+
description: g.s('Find first instance of the model matched by filter from the data source.'),
635647
accessType: 'READ',
636648
accepts: {arg: 'filter', type: 'object', description: 'Filter defining fields, where, include, order, offset, and limit'},
637649
returns: {arg: 'data', type: typeName, root: true},
@@ -640,7 +652,7 @@ module.exports = function(registry) {
640652
});
641653

642654
setRemoting(PersistedModel, 'destroyAll', {
643-
description: g.f('Delete all matching records.'),
655+
description: g.s('Delete all matching records.'),
644656
accessType: 'WRITE',
645657
accepts: {arg: 'where', type: 'object', description: 'filter.where object'},
646658
returns: {
@@ -655,17 +667,17 @@ module.exports = function(registry) {
655667

656668
setRemoting(PersistedModel, 'updateAll', {
657669
aliases: ['update'],
658-
description: g.f('Update instances of the model matched by {{where}} from the data source.'),
670+
description: g.s('Update instances of the model matched by {{where}} from the data source.'),
659671
accessType: 'WRITE',
660672
accepts: [
661673
{arg: 'where', type: 'object', http: { source: 'query'},
662-
description: g.f('Criteria to match model instances')},
674+
description: g.s('Criteria to match model instances')},
663675
{arg: 'data', type: 'object', http: {source: 'body'},
664-
description: g.f('An object of model property name/value pairs')},
676+
description: g.s('An object of model property name/value pairs')},
665677
],
666678
returns: {
667679
arg: 'count',
668-
description: g.f('The number of instances updated'),
680+
description: g.s('The number of instances updated'),
669681
type: 'object',
670682
root: true
671683
},
@@ -674,7 +686,7 @@ module.exports = function(registry) {
674686

675687
setRemoting(PersistedModel, 'deleteById', {
676688
aliases: ['destroyById', 'removeById'],
677-
description: g.f('Delete a model instance by {{id}} from the data source.'),
689+
description: g.s('Delete a model instance by {{id}} from the data source.'),
678690
accessType: 'WRITE',
679691
accepts: {arg: 'id', type: 'any', description: 'Model id', required: true,
680692
http: {source: 'path'}},
@@ -683,15 +695,15 @@ module.exports = function(registry) {
683695
});
684696

685697
setRemoting(PersistedModel, 'count', {
686-
description: g.f('Count instances of the model matched by where from the data source.'),
698+
description: g.s('Count instances of the model matched by where from the data source.'),
687699
accessType: 'READ',
688700
accepts: {arg: 'where', type: 'object', description: 'Criteria to match model instances'},
689701
returns: {arg: 'count', type: 'number'},
690702
http: {verb: 'get', path: '/count'}
691703
});
692704

693705
setRemoting(PersistedModel.prototype, 'updateAttributes', {
694-
description: g.f('Update attributes for a model instance and persist it into ' +
706+
description: g.s('Update attributes for a model instance and persist it into ' +
695707
'the data source.'),
696708
accessType: 'WRITE',
697709
accepts: {arg: 'data', type: 'object', http: {source: 'body'}, description: 'An object of model property name/value pairs'},
@@ -701,7 +713,7 @@ module.exports = function(registry) {
701713

702714
if (options.trackChanges || options.enableRemoteReplication) {
703715
setRemoting(PersistedModel, 'diff', {
704-
description: g.f('Get a set of deltas and conflicts since the given checkpoint.'),
716+
description: g.s('Get a set of deltas and conflicts since the given checkpoint.'),
705717
accessType: 'READ',
706718
accepts: [
707719
{arg: 'since', type: 'number', description: 'Find deltas since this checkpoint'},
@@ -713,7 +725,7 @@ module.exports = function(registry) {
713725
});
714726

715727
setRemoting(PersistedModel, 'changes', {
716-
description: g.f('Get the changes to a model since a given checkpoint.' +
728+
description: g.s('Get the changes to a model since a given checkpoint.' +
717729
'Provide a filter object to reduce the number of results returned.'),
718730
accessType: 'READ',
719731
accepts: [
@@ -725,7 +737,7 @@ module.exports = function(registry) {
725737
});
726738

727739
setRemoting(PersistedModel, 'checkpoint', {
728-
description: g.f('Create a checkpoint.'),
740+
description: g.s('Create a checkpoint.'),
729741
// The replication algorithm needs to create a source checkpoint,
730742
// even though it is otherwise not making any source changes.
731743
// We need to allow this method for users that don't have full
@@ -736,14 +748,14 @@ module.exports = function(registry) {
736748
});
737749

738750
setRemoting(PersistedModel, 'currentCheckpoint', {
739-
description: g.f('Get the current checkpoint.'),
751+
description: g.s('Get the current checkpoint.'),
740752
accessType: 'READ',
741753
returns: {arg: 'checkpoint', type: 'object', root: true},
742754
http: {verb: 'get', path: '/checkpoint'}
743755
});
744756

745757
setRemoting(PersistedModel, 'createUpdates', {
746-
description: g.f('Create an update list from a delta list.'),
758+
description: g.s('Create an update list from a delta list.'),
747759
// This operation is read-only, it does not change any local data.
748760
// It is called by the replication algorithm to compile a list
749761
// of changes to apply on the target.
@@ -754,14 +766,14 @@ module.exports = function(registry) {
754766
});
755767

756768
setRemoting(PersistedModel, 'bulkUpdate', {
757-
description: g.f('Run multiple updates at once. Note: this is not atomic.'),
769+
description: g.s('Run multiple updates at once. Note: this is not atomic.'),
758770
accessType: 'WRITE',
759771
accepts: {arg: 'updates', type: 'array'},
760772
http: {verb: 'post', path: '/bulk-update'}
761773
});
762774

763775
setRemoting(PersistedModel, 'findLastChange', {
764-
description: g.f('Get the most recent change record for this instance.'),
776+
description: g.s('Get the most recent change record for this instance.'),
765777
accessType: 'READ',
766778
accepts: {
767779
arg: 'id', type: 'any', required: true, http: { source: 'path' },
@@ -773,7 +785,7 @@ module.exports = function(registry) {
773785

774786
setRemoting(PersistedModel, 'updateLastChange', {
775787
description: [
776-
g.f('Update the properties of the most recent change record ' +
788+
g.s('Update the properties of the most recent change record ' +
777789
'kept for this instance.'),
778790
],
779791
accessType: 'WRITE',
@@ -784,7 +796,7 @@ module.exports = function(registry) {
784796
},
785797
{
786798
arg: 'data', type: 'object', http: {source: 'body'},
787-
description: g.f('An object of Change property name/value pairs'),
799+
description: g.s('An object of Change property name/value pairs'),
788800
},
789801
],
790802
returns: { arg: 'result', type: this.Change.modelName, root: true },
@@ -810,7 +822,7 @@ module.exports = function(registry) {
810822
}
811823

812824
setRemoting(PersistedModel, 'createChangeStream', {
813-
description: g.f('Create a change stream.'),
825+
description: g.s('Create a change stream.'),
814826
accessType: 'READ',
815827
http: [
816828
{verb: 'post', path: '/change-stream'},

lib/registry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ Registry.prototype.configureModel = function(ModelCtor, config) {
239239
// configuration, so that the datasource picks up updated relations
240240
if (config.dataSource) {
241241
assert(config.dataSource instanceof DataSource,
242-
g.f('Cannot configure %s: {{config.dataSource}} must be an instance ' +
243-
'of {{DataSource}}', ModelCtor.modelName));
242+
'Cannot configure ' + ModelCtor.modelName +
243+
': config.dataSource must be an instance of DataSource');
244244
ModelCtor.attachTo(config.dataSource);
245245
debug('Attached model `%s` to dataSource `%s`',
246246
modelName, config.dataSource.name);

0 commit comments

Comments
 (0)