Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
solve typo and get rid off explicit skipWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 14, 2019
1 parent ea1b1c8 commit 0118e30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export interface InMemoryData {
refLock: OptimisticMap<Dict<number>>;
records: NodeMap<EntityField>;
links: NodeMap<Link>;
storage?: StorageAdapter;

This comment has been minimized.

Copy link
@kitten

kitten Dec 14, 2019

Member

Small but, if we type this as StorageAdapter | null we get rid of a performance cliff by initialising to null which avoids that the JS engine discards the previous shape and optimised code when we update this field

}

let currentData: null | InMemoryData = null;
let currentDependencies: null | Set<string> = null;
let currentOptimisticKey: null | number = null;

export const makeDict = (): any => Object.create(null);
export const storage: { current: StorageAdapter | null } = { current: null };
let persistanceBatch: SerializedEntries = makeDict();
let persistenceBatch: SerializedEntries = makeDict();

const makeNodeMap = <T>(): NodeMap<T> => ({
optimistic: makeDict(),
Expand Down Expand Up @@ -67,10 +67,10 @@ export const clearDataState = () => {
});
}

if (storage.current) {
if (data.storage) {
defer(() => {
(storage.current as StorageAdapter).write(persistanceBatch);
persistanceBatch = makeDict();
data.storage!.write(persistenceBatch);
persistenceBatch = makeDict();
});
}

Expand Down Expand Up @@ -278,17 +278,17 @@ export const gc = (data: InMemoryData) => {
delete data.refCount[entityKey];
data.records.base.delete(entityKey);
data.gcBatch.delete(entityKey);
if (storage.current) {
persistanceBatch[prefixKey('r', entityKey)] = undefined;
if (data.storage) {
persistenceBatch[prefixKey('r', entityKey)] = undefined;
}

// Delete all the entity's links, but also update the reference count
// for those links (which can lead to an unrolled recursive GC of the children)
const linkNode = data.links.base.get(entityKey);
if (linkNode !== undefined) {
data.links.base.delete(entityKey);
if (storage.current) {
persistanceBatch[prefixKey('l', entityKey)] = undefined;
if (data.storage) {
persistenceBatch[prefixKey('l', entityKey)] = undefined;
}
for (const key in linkNode) {
updateRCForLink(data.gcBatch, data.refCount, linkNode[key], -1);
Expand Down Expand Up @@ -332,14 +332,13 @@ export const readLink = (
export const writeRecord = (
entityKey: string,
fieldKey: string,
value: EntityField,
skipPersistance?: boolean
value: EntityField
) => {
updateDependencies(entityKey, fieldKey);
setNode(currentData!.records, entityKey, fieldKey, value);
if (storage.current && !skipPersistance && !currentOptimisticKey) {
if (currentData!.storage && !currentOptimisticKey) {
const key = prefixKey('r', joinKeys(entityKey, fieldKey));
persistanceBatch[key] = value;
persistenceBatch[key] = value;
}
};

Expand All @@ -351,8 +350,7 @@ export const hasField = (entityKey: string, fieldKey: string): boolean =>
export const writeLink = (
entityKey: string,
fieldKey: string,
link: Link | undefined,
skipPersistance?: boolean
link: Link | undefined
) => {
const data = currentData!;
// Retrieve the reference counting dict or the optimistic reference locking dict
Expand All @@ -369,9 +367,9 @@ export const writeLink = (
(data.refLock[currentOptimisticKey] = makeDict());
links = data.links.optimistic[currentOptimisticKey];
} else {
if (storage.current && !skipPersistance) {
if (data.storage) {
const key = prefixKey('l', joinKeys(entityKey, fieldKey));
persistanceBatch[key] = link;
persistenceBatch[key] = link;
}
refCount = data.refCount;
links = data.links.base;
Expand Down
6 changes: 3 additions & 3 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,20 @@ export class Store implements Cache {
}

hydrateData(data: object, adapter: StorageAdapter) {
InMemoryData.storage.current = adapter;
InMemoryData.initDataState(this.data, 0);
for (const key in data) {
const entityKey = key.slice(2, key.indexOf('.'));
const fieldKey = key.slice(key.indexOf('.') + 1);
switch (key.charCodeAt(0)) {
case 108:
InMemoryData.writeLink(entityKey, fieldKey, data[key], true);
InMemoryData.writeLink(entityKey, fieldKey, data[key]);
break;
case 114:
InMemoryData.writeRecord(entityKey, fieldKey, data[key], true);
InMemoryData.writeRecord(entityKey, fieldKey, data[key]);
break;
}
}
InMemoryData.clearDataState();
this.data.storage = adapter;
}
}

0 comments on commit 0118e30

Please sign in to comment.