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 Jul 29, 2024
1 parent 1222a8a commit 6c7fada
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
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 as any)[_iterSymbol.v];
}

if (iterFn) {
let values: T[] = [];
let from = (start === UNDEF_VALUE || start < 0) ? 0 : start;
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 @@ -11,6 +11,7 @@ import { _getGlobalValue } from "../internal/global";
import { ILazyValue, _globalLazyTestHooks, _initTestHooks, getLazy } from "./lazy";
import { ICachedValue, createCachedValue } from "./cache";
import { safe } from "./safe";
import { safeGet } from "./safe_get";

const WINDOW = "window";

Expand All @@ -31,7 +32,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 +89,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 +121,24 @@ 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) {
}
}

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
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
2 changes: 1 addition & 1 deletion lib/src/object/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function _deepCopy<T>(visitMap: _RecursiveVisitMap[], value: T, ctx: _DeepCopyCo
if (value && theType === OBJECT) {
isPlain = isPlainObject(value);
} else {
isPrim = value === NULL_VALUE || isPrimitiveType(theType);
isPrim = (value === NULL_VALUE) || isPrimitiveType(theType);
}

let details: IObjDeepCopyHandlerDetails = {
Expand Down
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.0",
"@nevware21/grunt-ts-plugin": "^0.5.0",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
Expand Down

0 comments on commit 6c7fada

Please sign in to comment.