Closed Bug 850855 Opened 12 years ago Closed 12 years ago

Clean up context's get*

Categories

(L20n :: JS Library, defect, P1)

x86
macOS
defect

Tracking

(Not tracked)

RESOLVED FIXED
1.0 beta

People

(Reporter: zbraniecki, Assigned: stas)

References

Details

(Keywords: APIchange)

Attachments

(1 file, 2 obsolete files)

We currently have a mess inside get* functions. Both code wise and API wise. We need to clean this up. Arguments, return values, code flow, fallbacks, error reporting.
Assignee: nobody → gandalf
Priority: -- → P1
Target Milestone: --- → 1.0 beta
Flags: blocking-parkcity+
Keywords: APIchange
Depends on: 847012
Assignee: gandalf → stas
Attached patch Patch (obsolete) — Splinter Review
The patch implements a recursive getFromLocale instead of a _get with a while loop. It also defaults to getEntity. When doing ctx.get, the code uses getEntity but just returns the string value. There shouldn't be any perf implications: most of the times, if you care about the attrs, you'll use ctx.getEntity anyways. If you're using ctx.get, it's probably with an entity which doesn't have any public attributes.
Attachment #737538 - Flags: review?(gandalf)
Attached patch Patch (rebased) (obsolete) — Splinter Review
I rebased the patch on top of the current master.
Attachment #737538 - Attachment is obsolete: true
Attachment #737538 - Flags: review?(gandalf)
Attachment #737562 - Flags: review?(gandalf)
Comment on attachment 737562 [details] [diff] [review] Patch (rebased) >diff --git a/lib/compiler.js b/lib/compiler.js >index 85e55e7..5c889a6 100644 >--- a/lib/compiler.js >+++ b/lib/compiler.js >@@ -235,6 +235,7 @@ > this.local = node.local || false; > this.index = []; > this.attributes = {}; >+ this.publicAttributes = []; > var i; > for (i = 0; i < node.index.length; i++) { > this.index.push(Expression(node.index[i], this)); >@@ -242,6 +243,9 @@ > for (i = 0; i < node.attrs.length; i++) { > var attr = node.attrs[i]; > this.attributes[attr.key.name] = new Attribute(attr, this); >+ if (!attr.local) { >+ this.publicAttributes.push(attr.key.name); >+ } > } > this.value = Expression(node.value, this, this.index); > } >@@ -284,8 +288,8 @@ > value: this.getString(ctxdata), > attributes: {} > }; >- for (var name in this.attributes) { >- entity.attributes[name] = this.attributes[name].getString(ctxdata); >+ for (var i = 0, attr; attr = this.publicAttributes[i]; i++) { >+ entity.attributes[attr] = this.attributes[attr].getString(ctxdata); > } Why is publicAttributes better than checking for .local here? >diff --git a/lib/context.js b/lib/context.js >index 23d40e7..376d625 100644 >--- a/lib/context.js >+++ b/lib/context.js >@@ -204,13 +204,46 @@ > }, > }; > var _listeners = []; >- var _pending = []; >+ var _pending = { >+ get: [], >+ getEntity: [], >+ }; Can we not use _pending here and require context to be ready before any get is fired? I think it would be suitable for 1.0, extendable for Next and would simplify the code.
(In reply to Zbigniew Braniecki [:gandalf] from comment #3) > Comment on attachment 737562 [details] [diff] [review] > Patch (rebased) > > >diff --git a/lib/compiler.js b/lib/compiler.js > >index 85e55e7..5c889a6 100644 > >--- a/lib/compiler.js > >+++ b/lib/compiler.js > >@@ -235,6 +235,7 @@ > > this.local = node.local || false; > > this.index = []; > > this.attributes = {}; > >+ this.publicAttributes = []; > > var i; > > for (i = 0; i < node.index.length; i++) { > > this.index.push(Expression(node.index[i], this)); > >@@ -242,6 +243,9 @@ > > for (i = 0; i < node.attrs.length; i++) { > > var attr = node.attrs[i]; > > this.attributes[attr.key.name] = new Attribute(attr, this); > >+ if (!attr.local) { > >+ this.publicAttributes.push(attr.key.name); > >+ } > > } > > this.value = Expression(node.value, this, this.index); > > } > >@@ -284,8 +288,8 @@ > > value: this.getString(ctxdata), > > attributes: {} > > }; > >- for (var name in this.attributes) { > >- entity.attributes[name] = this.attributes[name].getString(ctxdata); > >+ for (var i = 0, attr; attr = this.publicAttributes[i]; i++) { > >+ entity.attributes[attr] = this.attributes[attr].getString(ctxdata); > > } > > Why is publicAttributes better than checking for .local here? publicAttributes is filled in during the initialization of the Entity, so only once. Checking for .local here means checking every time we call Entity.getEntity (I'll rename this to Entity.get). From my tests, on an attribute-sparse file, there's negligible performance difference. For attribute-heavy files, publicAttributes approach can be up to 15% faster, > >diff --git a/lib/context.js b/lib/context.js > >index 23d40e7..376d625 100644 > >--- a/lib/context.js > >+++ b/lib/context.js > >@@ -204,13 +204,46 @@ > > }, > > }; > > var _listeners = []; > >- var _pending = []; > >+ var _pending = { > >+ get: [], > >+ getEntity: [], > >+ }; > > Can we not use _pending here and require context to be ready before any get > is fired? > > I think it would be suitable for 1.0, extendable for Next and would simplify > the code. OK, I'll change it.
This patch also fixes bug 862101.
Attachment #737562 - Attachment is obsolete: true
Attachment #737562 - Flags: review?(gandalf)
Attachment #737859 - Flags: review?(gandalf)
Attachment #737859 - Flags: review?(gandalf) → review+
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Is it possible that this regressed string compilation? It seems like the strings are always returned as simple ones now.
Flags: needinfo?(stas)
false alarm, sorry.
Flags: needinfo?(stas)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: