Skip to content

Commit

Permalink
Don't allocate the inner cache unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 9, 2020
1 parent dd40435 commit eaa0df6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/react-fs/src/ReactFilesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ const Rejected = 2;
type PendingRecord = {|
status: 0,
value: Wakeable,
cache: Array<mixed>,
cache: null,
|};

type ResolvedRecord<T> = {|
status: 1,
value: T,
cache: Array<mixed>,
cache: null | Array<mixed>,
|};

type RejectedRecord = {|
status: 2,
value: mixed,
cache: Array<mixed>,
cache: null,
|};

type Record<T> = PendingRecord | ResolvedRecord<T> | RejectedRecord;
Expand All @@ -41,7 +41,7 @@ function createRecordFromThenable<T>(thenable: Thenable<T>): Record<T> {
const record: Record<T> = {
status: Pending,
value: thenable,
cache: [],
cache: null,
};
thenable.then(
value => {
Expand All @@ -62,9 +62,9 @@ function createRecordFromThenable<T>(thenable: Thenable<T>): Record<T> {
return record;
}

function readRecordValue<T>(record: Record<T>): T {
function readRecord<T>(record: Record<T>): ResolvedRecord<T> {
if (record.status === Resolved) {
return record.value;
return record;
} else {
throw record.value;
}
Expand Down Expand Up @@ -114,7 +114,8 @@ export function readFile(
record = createRecordFromThenable(thenable);
map.set(path, record);
}
const buffer: Buffer = readRecordValue(record);
const resolvedRecord = readRecord(record);
const buffer: Buffer = resolvedRecord.value;
if (!options) {
return buffer;
}
Expand All @@ -136,7 +137,7 @@ export function readFile(
if (typeof encoding !== 'string') {
return buffer;
}
const textCache = record.cache;
const textCache = resolvedRecord.cache || (resolvedRecord.cache = []);
for (let i = 0; i < textCache.length; i += 2) {
if (textCache[i] === encoding) {
return (textCache[i + 1]: any);
Expand Down

0 comments on commit eaa0df6

Please sign in to comment.