Skip to content

Commit

Permalink
[Bug] getInst() function is getting mis-optimized for a worker with r…
Browse files Browse the repository at this point in the history
…ollup #302
  • Loading branch information
nev21 committed Sep 21, 2024
1 parent 1fedb77 commit 2424402
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/src/array/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
*/

import { ArrSlice, CALL } from "../internal/constants";
import { ArrSlice, CALL, NULL_VALUE } from "../internal/constants";

/**
* The arrSlice() method returns a shallow copy of a portion of an array into a new array object
Expand Down Expand Up @@ -52,5 +52,5 @@ import { ArrSlice, CALL } from "../internal/constants";
* ```
*/
export function arrSlice<T>(theArray: ArrayLike<T>, start?: number, end?: number): T[] {
return ((theArray && theArray["slice"]) || ArrSlice).apply(theArray, ArrSlice[CALL](arguments, 1));
return ((theArray ? theArray["slice"] : NULL_VALUE) || ArrSlice).apply(theArray, ArrSlice[CALL](arguments, 1));
}
6 changes: 5 additions & 1 deletion lib/src/funcs/readArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export function readArgs<T = any>(theArgs: ArrayLike<T> | Iterable<T>, start?: n
// IArgument is both ArrayLike and an iterable, so prefering to treat it as
// an array for performance
!_iterSymbol && (_iterSymbol = createCachedValue(hasSymbol() && getKnownSymbol(WellKnownSymbols.iterator)));
let iterFn = _iterSymbol.v && theArgs[_iterSymbol.v];
let iterFn: any;
if (_iterSymbol.v) {
iterFn = theArgs[_iterSymbol.v];
}

if (iterFn) {
let values: T[] = [];
let from = (start === UNDEF_VALUE || start < 0) ? 0 : start;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/helpers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export function isPrimitive(value: any): value is string | number | bigint | boo
export function isPrimitiveType(theType: string): boolean {
!_primitiveTypes && (_primitiveTypes = [ "string", "number", "boolean", UNDEFINED, "symbol", "bigint" ]);

return theType !== OBJECT && _primitiveTypes.indexOf(theType) !== -1;
return !!(theType !== OBJECT && _primitiveTypes.indexOf(theType) !== -1);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/src/helpers/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function dumpObj(object: any, format?: boolean | number): string {

try {
propertyValueDump = JSON.stringify(object, NULL_VALUE, format ? (((typeof format as unknown) === "number") ? format as number : 4) : UNDEF_VALUE);
propertyValueDump = (propertyValueDump && propertyValueDump.replace(/"(\w+)"\s*:\s{0,1}/g, "$1: ")) || asString(object);
propertyValueDump = (propertyValueDump ? propertyValueDump.replace(/"(\w+)"\s*:\s{0,1}/g, "$1: ") : NULL_VALUE) || asString(object);
} catch(e) {
// Unable to convert object (probably circular)
propertyValueDump = " - " + dumpObj(e, format);
Expand Down
23 changes: 18 additions & 5 deletions lib/src/helpers/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export function _getGlobalInstFn<T>(getFn: (...args: unknown[]) => T, theArgs?:
let cachedValue: ICachedValue<T>;
return function() {
!_globalLazyTestHooks && _initTestHooks();
(!cachedValue || _globalLazyTestHooks.lzy) && (cachedValue = createCachedValue(safe(getFn, theArgs).v));
if (!cachedValue || _globalLazyTestHooks.lzy) {
cachedValue = createCachedValue(safe(getFn, theArgs).v);
}

return cachedValue.v;
}
Expand Down Expand Up @@ -86,10 +88,11 @@ export function lazySafeGetInst<T>(name: string | number | symbol) : ILazyValue<
* @param useCached - [Optional] used for testing to bypass the cached lookup, when `true` this will
* cause the cached global to be reset.
*/
/*#__NO_SIDE_EFFECTS__*/
export function getGlobal(useCached?: boolean): Window {
!_globalLazyTestHooks && _initTestHooks();
(!_cachedGlobal || useCached === false || _globalLazyTestHooks.lzy) && (_cachedGlobal = createCachedValue(safe(_getGlobalValue).v || NULL_VALUE));
if (!_cachedGlobal || useCached === false || _globalLazyTestHooks.lzy) {
_cachedGlobal = createCachedValue(safe(_getGlobalValue).v || NULL_VALUE);
}

return _cachedGlobal.v;
}
Expand Down Expand Up @@ -117,15 +120,25 @@ export function getGlobal(useCached?: boolean): Window {
*/
/*#__NO_SIDE_EFFECTS__*/
export function getInst<T>(name: string | number | symbol, useCached?: boolean): T | null {
const gbl = (!_cachedGlobal || useCached === false) ? getGlobal(useCached) : _cachedGlobal.v;
let gbl: any;
if (!_cachedGlobal || useCached === false) {
gbl = getGlobal(useCached);
} else {
gbl = _cachedGlobal.v;
}

if (gbl && gbl[name]) {
return gbl[name] as T;
}

// Test workaround, for environments where <global>.window (when global == window) doesn't return the base window
if (name === WINDOW) {
// tslint:disable-next-line: no-angle-bracket-type-assertion
return <any>getWindow() as T;
try {
return window as T;
} catch (e) {
// Ignore
}
}

return NULL_VALUE;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/helpers/perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function hasPerformance(): boolean {
/*#__NO_SIDE_EFFECTS__*/
export function getPerformance(): Performance {
!_globalLazyTestHooks && _initTestHooks();
(!_perf || _globalLazyTestHooks.lzy) && (_perf = createCachedValue(safe(getInst<Performance>, ["performance"]).v));
if (!_perf || _globalLazyTestHooks.lzy) {
_perf = createCachedValue(safe(getInst<Performance>, ["performance"]).v);
}

return _perf.v;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/internal/unwrapFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { dumpObj } from "../helpers/diagnostics";
import { throwTypeError } from "../helpers/throw";
import { asString } from "../string/as_string";
import { ArrSlice, CALL } from "./constants";
import { ArrSlice, CALL, NULL_VALUE } from "./constants";

/**
* @internal
Expand Down Expand Up @@ -44,10 +44,10 @@ export const _unwrapFunction:<R, T>(funcName: keyof T, clsProto: T) => <T>(this:
*/
/*#__NO_SIDE_EFFECTS__*/
export function _unwrapFunctionWithPoly<T, P extends (...args: any) => any>(funcName: keyof T, clsProto?: T, polyFunc?: P) {
let clsFn = clsProto && clsProto[funcName];
let clsFn = clsProto ? clsProto[funcName] : NULL_VALUE;

return function(thisArg: any): ReturnType<P> {
let theFunc = (thisArg && thisArg[funcName]) || clsFn;
let theFunc = (thisArg ? thisArg[funcName] : NULL_VALUE) || clsFn;
if (theFunc || polyFunc) {
let theArgs = arguments;
return ((theFunc || polyFunc) as Function).apply(thisArg, theFunc ? ArrSlice[CALL](theArgs, 1) : theArgs);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/iterator/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export function createIterator<T>(ctx: CreateIteratorContext<T>): Iterator<T> {
}

function _next(): IteratorResult<T> {
isDone = isDone || (ctx.n ? ctx.n(arguments) : true);
if (!isDone) {
isDone = (ctx.n ? ctx.n(arguments) : true);
}

let result = {
done: isDone
Expand Down
3 changes: 1 addition & 2 deletions lib/src/iterator/forOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Licensed under the MIT license.
*/

import { fnCall } from "../funcs/funcs";
import { ICachedValue, createCachedValue } from "../helpers/cache";
import { CALL, NULL_VALUE, UNDEF_VALUE } from "../internal/constants";
import { getKnownSymbol } from "../symbol/symbol";
Expand Down Expand Up @@ -60,7 +59,7 @@ export function iterForOf<T>(iter: Iterator<T> | Iterable<T>, callbackfn: (value
if (iter) {
if (!isIterator(iter)) {
!_iterSymbol && (_iterSymbol = createCachedValue(getKnownSymbol(WellKnownSymbols.iterator)));
iter = iter[_iterSymbol.v] ? iter[_iterSymbol.v]() : null;
iter = iter[_iterSymbol.v] ? iter[_iterSymbol.v]() : NULL_VALUE;
}

if (isIterator(iter)) {
Expand Down
12 changes: 7 additions & 5 deletions lib/src/object/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ function _deepCopy<T>(visitMap: _RecursiveVisitMap[], value: T, ctx: _DeepCopyCo

const theType = typeof value;
let isPlain = false;
let isPrim = false;
if (value && theType === OBJECT) {
isPlain = isPlainObject(value);
} else {
isPrim = value === NULL_VALUE || isPrimitiveType(theType);
let isPrim = value === NULL_VALUE;
if (!isPrim) {
if (value && theType === OBJECT) {
isPlain = isPlainObject(value);
} else {
isPrim = isPrimitiveType(theType);
}
}

let details: IObjDeepCopyHandlerDetails = {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/object/is_plain_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function isPlainObject(value: any): value is object {
proto = proto[CONSTRUCTOR]
}

result = proto && typeof proto === FUNCTION && _fnToString[CALL](proto) === _objCtrFnString;
result = !!(proto && typeof proto === FUNCTION && _fnToString[CALL](proto) === _objCtrFnString);
}
} catch (ex) {
// Something went wrong, so it's not an object we are playing with
Expand Down
3 changes: 2 additions & 1 deletion lib/src/polyfills/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Licensed under the MIT license.
*/

import { UNDEF_VALUE } from "../internal/constants";
import { getKnownSymbol } from "../symbol/symbol";
import { WellKnownSymbols } from "../symbol/well_known";

Expand Down Expand Up @@ -55,7 +56,7 @@ import { WellKnownSymbols } from "../symbol/well_known";
*/
/*#__NO_SIDE_EFFECTS__*/
export function polyStrSymSplit(value: string, splitter: { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number): string[] {
let splitFn: (string: string, limit?: number) => string[] = splitter && splitter[getKnownSymbol(WellKnownSymbols.split)];
let splitFn: (string: string, limit?: number) => string[] = splitter ?(splitter as any)[getKnownSymbol(WellKnownSymbols.split)] : UNDEF_VALUE;

return splitFn ? splitFn(value, limit) : [ value ];
}
2 changes: 1 addition & 1 deletion lib/src/string/ends_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function polyStrEndsWith(value: string, searchString: string, length?: nu
}

let searchValue = isString(searchString) ? searchString : asString(searchString);
let end = !isUndefined(length) && length < value[LENGTH] ? length : value[LENGTH];
let end = (!isUndefined(length) && length < value[LENGTH]) ? length : value[LENGTH];

return strSubstring(value, end - searchValue[LENGTH], end) === searchValue;
}
4 changes: 2 additions & 2 deletions lib/src/string/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
*/

import { StrProto } from "../internal/constants";
import { NULL_VALUE, StrProto } from "../internal/constants";
import { _unwrapFunction, _unwrapFunctionWithPoly } from "../internal/unwrapFunction";
import { polyStrSymSplit } from "../polyfills/split";
import { hasSymbol } from "../symbol/symbol";
Expand Down Expand Up @@ -87,4 +87,4 @@ export const strSplit: (value: string, separator: string | RegExp, limit?: numbe
* console.log(strSymSplit(myString, splitByNumber)); // [ "a", "bc", "c5d", "e", "f" ]
* ```
*/
export const strSymSplit: (value: string, splitter: { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[] = (/*#__PURE__*/_unwrapFunctionWithPoly("split", StrProto, !hasSymbol() ? polyStrSymSplit : null));
export const strSymSplit: (value: string, splitter: { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number) => string[] = (/*#__PURE__*/_unwrapFunctionWithPoly("split", StrProto, !hasSymbol() ? polyStrSymSplit : NULL_VALUE));
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@microsoft/api-extractor": "^7.34.4",
"@nevware21/grunt-eslint-ts": "0.5.0",
"@nevware21/grunt-ts-plugin": "0.5.0",
"@nevware21/grunt-eslint-ts": "^0.5.1",
"@nevware21/grunt-ts-plugin": "^0.5.1",
"@nevware21/tripwire-chai": ">= 0.1.1 && < 2.0.0",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.0.0",
Expand Down

0 comments on commit 2424402

Please sign in to comment.