Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup/Speedup Montage.defineProperty() #1859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 47 additions & 44 deletions core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ Object.defineProperty(Montage.prototype, _serializableAttributeProperties, {
var ObjectAttributeProperties = new Map();
function getAttributeProperties(proto, attributeName, privateAttributeName) {
var attributePropertyName = privateAttributeName || (
attributeName === SERIALIZABLE ?
_serializableAttributeProperties :
attributeName === SERIALIZABLE ?
_serializableAttributeProperties :
(UNDERSCORE + attributeName + ATTRIBUTE_PROPERTIES));

if(proto !== Object.prototype) {
Expand Down Expand Up @@ -428,48 +428,42 @@ valuePropertyDescriptor.value = function Montage_defineProperty(obj, prop, descr
throw new TypeError("Cannot use distinct attribute on non-value property '" + prop + "'");
}


// reset defaults appropriately for framework.
if (PROTO in descriptor) {
descriptor.__proto__ = (isValueDescriptor ? (typeof descriptor.value === FUNCTION ? _defaultFunctionValueProperty : _defaultObjectValueProperty) : _defaultAccessorProperty);
} else {
var defaults;
if (isValueDescriptor) {
if (typeof descriptor.value === FUNCTION) {
defaults = _defaultFunctionValueProperty;
} else {
defaults = _defaultObjectValueProperty;
}
} else {
defaults = _defaultAccessorProperty;
}
for (var key in defaults) {
if (hasProperty.call(defaults, key)) {
if (!(key in descriptor)) {
descriptor[key] = defaults[key];
}
}
}
//Enumerable:
if(descriptor.enumerable === undefined) {
//If value and it's a function, emumerable should be false.
descriptor.enumerable = ((isValueDescriptor && typeof descriptor.value === FUNCTION) || prop.charCodeAt(0) === UNDERSCORE_UNICODE)
? false
: true;
}

if (!hasProperty.call(descriptor, ENUMERABLE) && prop.charCodeAt(0) === UNDERSCORE_UNICODE) {
descriptor.enumerable = false;
//writable
if(isValueDescriptor && descriptor.writable === undefined) {
descriptor.writable = true;
}

//configurable
if(descriptor.configurable === undefined)
descriptor.configurable = true;

//serializable
if (!hasProperty.call(descriptor, SERIALIZABLE)) {
if (! descriptor.enumerable) {
descriptor.serializable = false;
} else if (descriptor.get && !descriptor.set) {
descriptor.serializable = false;
} else if (descriptor.writable === false) {
descriptor.serializable = false;
}

descriptor.serializable =
((! descriptor.enumerable) ||
(descriptor.get && !descriptor.set) ||
(descriptor.writable === false))
? false
: (isValueDescriptor
? (typeof descriptor.value === FUNCTION
? false
: "reference")
: true);
}

if (SERIALIZABLE in descriptor) {
// if (SERIALIZABLE in descriptor) {
// get the _serializableAttributeProperties property or creates it through the entire chain if missing.
getAttributeProperties(obj, SERIALIZABLE)[prop] = descriptor.serializable;
}
// }

// clear the cache for super() for property we're about to redefine.
// But If we're defining a property as part of a Type/Class construction, we most likely don't need to worry about
Expand All @@ -478,7 +472,16 @@ valuePropertyDescriptor.value = function Montage_defineProperty(obj, prop, descr
__clearSuperDepencies(obj,prop, descriptor);
}

return Object.defineProperty(obj, prop, descriptor);
//Optimization to bypass Object.defineProperty() when possible
if(isValueDescriptor && !(prop in obj) && descriptor.writable && descriptor.enumerable && descriptor.configurable ) {
obj[prop] = descriptor.value;
return obj;
}
else {
return Object.defineProperty(obj, prop, descriptor);
}


};
Object.defineProperty(Montage, "defineProperty", valuePropertyDescriptor);

Expand Down Expand Up @@ -552,8 +555,8 @@ function __findSuperMethodImplementation( method, classFn, isFunctionSuper, meth
if(!methodPropertyName) {
//If methodPropertyNameArg is passed as an argument, we know what to look for,
//But it may not be there...
propertyNames = methodPropertyNameArg ?
(_propertyNames[0] = methodPropertyNameArg) && _propertyNames :
propertyNames = methodPropertyNameArg ?
(_propertyNames[0] = methodPropertyNameArg) && _propertyNames :
Object.getOwnPropertyNames(context);

//As we start, we don't really know which property name points to method, we're going to find out:
Expand Down Expand Up @@ -679,29 +682,29 @@ function __super(callerFn, methodPropertyName, isValue, isGetter, isSetter) {
*/
function _super() {
// Figure out which function called us.
var callerFn = ( _super && _super.caller ) ? _super.caller : arguments.callee.caller,
var callerFn = ( _super && _super.caller ) ? _super.caller : arguments.callee.caller,
superFn = __super.call(this,callerFn);
return superFn ? superFn.apply(this, arguments) : undefined;
}

function _superForValue(methodName) {
var callerFn = ( _superForValue && _superForValue.caller ) ? _superForValue.caller : arguments.callee.caller,
var callerFn = ( _superForValue && _superForValue.caller ) ? _superForValue.caller : arguments.callee.caller,
superFn = __super.call(this, callerFn, methodName, true, false, false);

//We may need to cache that at some point if it gets called too often
return superFn ? superFn.bind(this) : Function.noop;
}

function _superForGet(methodName) {
var callerFn = ( _superForGet && _superForGet.caller ) ? _superForGet.caller : arguments.callee.caller,
var callerFn = ( _superForGet && _superForGet.caller ) ? _superForGet.caller : arguments.callee.caller,
superFn = __super.call(this, callerFn, methodName, false, true, false);

//We may need to cache that at some point if it gets called too often
return superFn ? superFn.bind(this) : Function.noop;
}

function _superForSet(methodName) {
var callerFn = ( _superForSet && _superForSet.caller ) ? _superForSet.caller : arguments.callee.caller,
var callerFn = ( _superForSet && _superForSet.caller ) ? _superForSet.caller : arguments.callee.caller,
superFn = __super.call(this,callerFn,methodName,false,false,true);

//We may need to cache that at some point if it gets called too often
Expand Down Expand Up @@ -832,7 +835,7 @@ Montage.defineProperty(Montage, "getPropertyAttributes", {value: function (anObj
for (var name in attributes) {
if (hasProperty.call(attributes, name)) {
attributeValues[name] = attributes[name];
// should return the inherited defined attribute values
// should return the inherited defined attribute values
} else {
attributeValues[name] = attributes[name];
}
Expand Down Expand Up @@ -983,7 +986,7 @@ Montage.defineProperty(Montage.prototype, "callDelegateMethod", {

var delegateFunctionName = this.identifier + name.toCapitalized();
if (
typeof this.identifier === "string" &&
typeof this.identifier === "string" &&
typeof delegate[delegateFunctionName] === FUNCTION
) {
delegateFunction = delegate[delegateFunctionName];
Expand Down
14 changes: 7 additions & 7 deletions ui/flow.reel/flow-bezier-spline.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ var FlowBezierSpline = exports.FlowBezierSpline = Montage.specialize( {
cubicRealRoots: {
enumerable: false,
value: function (a, b, c, d) {
var epsilon = 1e-10, math = Math;
var math = Math;

if ((a < -epsilon) || (a > epsilon)) {
if ((a !== 0)) {
var dv = 1 / a,
A = b * dv,
B = c * dv,
Q = (3 * B - A * A) * (1 / 9),
R = (4.5 * A * B - 13.5 * d * dv - A * A * A) * (1 / 27),
D = Q * Q * Q + R * R;

if (D > epsilon) {
if (D > 0) {
var sqD = math.sqrt(D);

return [this.cubeRoot(R + sqD) + this.cubeRoot(R - sqD) + A * (-1 / 3)];
} else {
if (D > -epsilon) {
if ((R < -epsilon) || (R > epsilon)) {
if (D > 0) {
if ((R !== 0)) {
var S = this.cubeRoot(R),
r1 = S * 2 + A * (-1 / 3),
r2 = A * (-1 / 3) - S;
Expand All @@ -364,7 +364,7 @@ var FlowBezierSpline = exports.FlowBezierSpline = Montage.specialize( {
}
}
} else {
if ((b < -epsilon) || (b > epsilon)) {
if ((b !== 0)) {
var sq = c * c - 4 * b * d;

if (sq >= 0) {
Expand All @@ -374,7 +374,7 @@ var FlowBezierSpline = exports.FlowBezierSpline = Montage.specialize( {
return [];
}
} else {
if ((c < -epsilon) || (c > epsilon)) {
if ((c !== 0)) {
return [-d / c];
} else {
return [];
Expand Down