-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Copy on Write (CoW) and use in event queue #436
Open
JonoPrest
wants to merge
3
commits into
main
Choose a base branch
from
jp/add-cow-for-events-queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−58
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
type t<'a> = { | ||
mutable data: 'a, | ||
mutable isImmutable: bool, | ||
copy: 'a => 'a, | ||
} | ||
|
||
let make = (data: 'a, ~copy): t<'a> => {data, isImmutable: true, copy} | ||
let getDataRef = (self: t<'a>): 'a => self.data | ||
let getData = (self: t<'a>): 'a => | ||
if self.isImmutable { | ||
self.data | ||
} else { | ||
self.data->self.copy | ||
} | ||
|
||
let copy = ({isImmutable, data, copy} as self: t<'a>): t<'a> => | ||
if isImmutable { | ||
data->make(~copy) | ||
} else { | ||
self.isImmutable = true | ||
data->make(~copy) | ||
} | ||
|
||
let mutate = (self: t<'a>, fn) => | ||
if self.isImmutable { | ||
self.isImmutable = false | ||
self.data = self.data->self.copy | ||
fn(self.data) | ||
} else { | ||
fn(self.data) | ||
} | ||
|
||
module Array = { | ||
type t<'a> = t<array<'a>> | ||
module InternalFunctions = { | ||
let copy = arr => arr->Array.copy | ||
let push = (arr, item) => arr->Js.Array2.push(item)->ignore | ||
let pop = arr => arr->Js.Array2.pop | ||
} | ||
let make = (data: array<'a>): t<'a> => make(data, ~copy=InternalFunctions.copy) | ||
let push = (self: t<'a>, item) => self->mutate(arr => arr->InternalFunctions.push(item)) | ||
let pop = (self: t<'a>) => self->mutate(InternalFunctions.pop) | ||
let length = (self: t<'a>) => self->getDataRef->Array.length | ||
let last = (self: t<'a>) => self->getDataRef->Utils.Array.last | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ type partition = { | |
//of a rollback. | ||
dynamicContracts: array<TablesStatic.DynamicContractRegistry.t>, | ||
//Events ordered from latest to earliest | ||
fetchedEventQueue: array<Internal.eventItem>, | ||
fetchedEventQueue: Cow.Array.t<Internal.eventItem>, | ||
} | ||
|
||
type t = { | ||
|
@@ -62,7 +62,7 @@ type t = { | |
|
||
let shallowCopyPartition = (p: partition) => { | ||
...p, | ||
fetchedEventQueue: p.fetchedEventQueue->Array.copy, | ||
fetchedEventQueue: p.fetchedEventQueue->Cow.copy, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the improvement happens. It won't copy the array until a mutation occurs |
||
} | ||
|
||
let copy = (fetchState: t) => { | ||
|
@@ -155,7 +155,7 @@ let mergeIntoPartition = (p: partition, ~target: partition, ~maxAddrInPartition) | |
status: { | ||
fetchingStateId: None, | ||
}, | ||
fetchedEventQueue: [], | ||
fetchedEventQueue: []->Cow.Array.make, | ||
selection: target.selection, | ||
contractAddressMapping: restContractAddressMapping, | ||
dynamicContracts: restDcs, | ||
|
@@ -178,7 +178,10 @@ let mergeIntoPartition = (p: partition, ~target: partition, ~maxAddrInPartition) | |
selection: target.selection, | ||
contractAddressMapping: mergedContractAddressMapping, | ||
dynamicContracts: mergedDynamicContracts, | ||
fetchedEventQueue: mergeSortedEventList(p.fetchedEventQueue, target.fetchedEventQueue), | ||
fetchedEventQueue: mergeSortedEventList( | ||
p.fetchedEventQueue->Cow.getDataRef, | ||
target.fetchedEventQueue->Cow.getDataRef, | ||
)->Cow.Array.make, | ||
latestFetchedBlock, | ||
}, | ||
rest, | ||
|
@@ -205,7 +208,10 @@ let addItemsToPartition = ( | |
fetchingStateId: None, | ||
}, | ||
latestFetchedBlock, | ||
fetchedEventQueue: Array.concat(reversedNewItems, p.fetchedEventQueue), | ||
fetchedEventQueue: Array.concat( | ||
reversedNewItems, | ||
p.fetchedEventQueue->Cow.getDataRef, | ||
)->Cow.Array.make, | ||
} | ||
} | ||
|
||
|
@@ -255,7 +261,7 @@ let updateInternal = ( | |
for idx in 0 to partitions->Array.length - 1 { | ||
let p = partitions->Js.Array2.unsafe_get(idx) | ||
|
||
let partitionQueueSize = p.fetchedEventQueue->Array.length | ||
let partitionQueueSize = p.fetchedEventQueue->Cow.Array.length | ||
|
||
queueSize := queueSize.contents + partitionQueueSize | ||
|
||
|
@@ -330,7 +336,7 @@ let makeDcPartition = ( | |
selection, | ||
contractAddressMapping, | ||
dynamicContracts, | ||
fetchedEventQueue: [], | ||
fetchedEventQueue: Cow.Array.make([]), | ||
} | ||
} | ||
|
||
|
@@ -627,7 +633,7 @@ let getNextQuery = ( | |
if ( | ||
p->checkIsFetchingPartition->not && | ||
p.latestFetchedBlock.blockNumber < currentBlockHeight && | ||
(checkQueueSize ? p.fetchedEventQueue->Array.length < maxPartitionQueueSize : true) && ( | ||
(checkQueueSize ? p.fetchedEventQueue->Cow.Array.length < maxPartitionQueueSize : true) && ( | ||
isWithinSyncRange | ||
? true | ||
: !checkIsWithinSyncRange(~latestFetchedBlock=p.latestFetchedBlock, ~currentBlockHeight) | ||
|
@@ -749,9 +755,9 @@ Returns queue item WITHOUT the updated fetch state. Used for checking values | |
not updating state | ||
*/ | ||
let getEarliestEventInPartition = (p: partition) => { | ||
switch p.fetchedEventQueue->Utils.Array.last { | ||
switch p.fetchedEventQueue->Cow.Array.last { | ||
| Some(head) => | ||
Item({item: head, popItemOffQueue: () => p.fetchedEventQueue->Js.Array2.pop->ignore}) | ||
Item({item: head, popItemOffQueue: () => p.fetchedEventQueue->Cow.Array.pop->ignore}) | ||
| None => makeNoItem(p) | ||
} | ||
} | ||
|
@@ -819,7 +825,7 @@ let make = ( | |
}, | ||
contractAddressMapping: ContractAddressingMap.make(), | ||
dynamicContracts: [], | ||
fetchedEventQueue: [], | ||
fetchedEventQueue: Cow.Array.make([]), | ||
}) | ||
} | ||
|
||
|
@@ -841,7 +847,7 @@ let make = ( | |
selection: normalSelection, | ||
contractAddressMapping: ContractAddressingMap.make(), | ||
dynamicContracts: [], | ||
fetchedEventQueue: [], | ||
fetchedEventQueue: Cow.Array.make([]), | ||
} | ||
} | ||
|
||
|
@@ -980,12 +986,15 @@ let checkContainsRegisteredContractAddress = ( | |
let getLatestFullyFetchedBlock = ({latestFullyFetchedBlock}: t) => latestFullyFetchedBlock | ||
|
||
let pruneQueueFromFirstChangeEvent = ( | ||
queue: array<Internal.eventItem>, | ||
queue: Cow.Array.t<Internal.eventItem>, | ||
~firstChangeEvent: blockNumberAndLogIndex, | ||
) => { | ||
queue->Array.keep(item => | ||
queue | ||
->Cow.getDataRef | ||
->Array.keep(item => | ||
(item.blockNumber, item.logIndex) < (firstChangeEvent.blockNumber, firstChangeEvent.logIndex) | ||
) | ||
->Cow.Array.make | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
open RescriptMocha | ||
|
||
describe("Copy on write", () => { | ||
it("Should not copy without mutation", () => { | ||
let arr = [1, 2, 3] | ||
let cowA = arr->Cow.Array.make | ||
let cowB = cowA->Cow.copy | ||
|
||
Assert.ok( | ||
cowA->Cow.getDataRef === cowB->Cow.getDataRef && cowA->Cow.getDataRef === arr, | ||
~message="Should be a reference to the same array", | ||
) | ||
|
||
cowB->Cow.Array.push(4) | ||
|
||
let cowBRefA = cowB->Cow.getDataRef | ||
|
||
Assert.ok( | ||
cowA->Cow.getDataRef === arr && arr !== cowB->Cow.getDataRef, | ||
~message="Should be a new array after mutation", | ||
) | ||
|
||
cowB->Cow.Array.push(5) | ||
Assert.ok( | ||
cowB->Cow.getDataRef === cowBRefA, | ||
~message="Should continue to be the same ref after mutation", | ||
) | ||
|
||
let cowC = cowB->Cow.copy | ||
|
||
Assert.ok( | ||
cowB->Cow.getDataRef === cowC->Cow.getDataRef, | ||
~message="Should be the same ref after copy", | ||
) | ||
|
||
cowB->Cow.Array.push(6) | ||
Assert.ok( | ||
cowB->Cow.getDataRef !== cowC->Cow.getDataRef, | ||
~message="Should not be the same ref after mutation", | ||
) | ||
Assert.ok( | ||
cowC->Cow.getDataRef === cowBRefA, | ||
~message="See still has the original ref when it was copied", | ||
) | ||
|
||
Assert.deepEqual(cowA->Cow.getData, arr) | ||
Assert.deepEqual(cowB->Cow.getData, [1, 2, 3, 4, 5, 6]) | ||
Assert.deepEqual(cowC->Cow.getData, [1, 2, 3, 4, 5]) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about CoW when reading a bit about the swift compiler and thought it would be useful here