Skip to content

Commit

Permalink
avoid fill() overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Oct 14, 2022
1 parent d4469c1 commit 08f75b1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
10 changes: 4 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,12 +879,10 @@ function useMemoCache(size: number): Array<any> {

let data = memoCache.data[memoCache.index];
if (data === undefined) {
data = memoCache.data[memoCache.index] = new Array(size).fill(
MEMO_CACHE_UNSET_SENTINEL,
);
// Attach sentinel to the array, eventually we can make the sentinel a public
// export from React and avoid modifying the array
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
data = memoCache.data[memoCache.index] = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = MEMO_CACHE_UNSET_SENTINEL;
}
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand Down
10 changes: 4 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,12 +879,10 @@ function useMemoCache(size: number): Array<any> {

let data = memoCache.data[memoCache.index];
if (data === undefined) {
data = memoCache.data[memoCache.index] = new Array(size).fill(
MEMO_CACHE_UNSET_SENTINEL,
);
// Attach sentinel to the array, eventually we can make the sentinel a public
// export from React and avoid modifying the array
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
data = memoCache.data[memoCache.index] = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = MEMO_CACHE_UNSET_SENTINEL;
}
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand Down
3 changes: 1 addition & 2 deletions packages/react-reconciler/src/__tests__/useMemoCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ describe('useMemoCache()', () => {
const cache = useMemoCache(1);
expect(Array.isArray(cache)).toBe(true);
expect(cache.length).toBe(1);
expect(cache[0]).toBe(cache._);
expect(typeof cache._).toBe('symbol');
expect(cache[0]).toBe(Symbol.for('react.usememocache_sentinel'));
return 'Ok';
}
const root = ReactNoop.createRoot();
Expand Down
6 changes: 4 additions & 2 deletions packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,10 @@ const MEMO_CACHE_UNSET_SENTINEL: symbol = Symbol.for(
'react.usememocache_sentinel',
);
function useMemoCache(size: number): Array<any> {
const data = new Array(size).fill(MEMO_CACHE_UNSET_SENTINEL);
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
const data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = MEMO_CACHE_UNSET_SENTINEL;
}
return data;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/react-server/src/ReactFlightHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ export const Dispatcher: DispatcherType = {
return unsupportedRefresh;
},
useMemoCache(size: number): Array<any> {
const data = new Array(size).fill(MEMO_CACHE_UNSET_SENTINEL);
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
const data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = MEMO_CACHE_UNSET_SENTINEL;
}
return data;
},
use: enableUseHook ? use : (unsupportedHook: any),
Expand Down

0 comments on commit 08f75b1

Please sign in to comment.