Skip to content

Refactor array slicing code to reversed arrays with pop - #157

Merged
JonoPrest merged 3 commits into
mainfrom
jp/fix-array-slice
Aug 29, 2024
Merged

Refactor array slicing code to reversed arrays with pop#157
JonoPrest merged 3 commits into
mainfrom
jp/fix-array-slice

Conversation

@JonoPrest

@JonoPrest JonoPrest commented Aug 29, 2024

Copy link
Copy Markdown
Collaborator
  • Reverses all the arrays that are event queue data structures so events are ordered from latest->earliest
  • Shallow copy of values happens before createBatchInternal is called
  • Individually popped items have a callback for mutating/popping the value off of it's queue
  • After the earliest item is evaluated across these queues the pop function is called.

"prom-client": "15.0.0",
"react": "18.2.0",
"rescript": "11.1.0",
"rescript": "11.1.3",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a release for Fuel indexer tomorrow to prevent it from breaking

Comment on lines +154 to +159
/**
Currently a bug in rescript if you ignore the return value of spliceInPlace
https://github.com/rescript-lang/rescript-compiler/issues/6991
*/
@send
external spliceInPlace: (array<'a>, ~pos: int, ~remove: int) => array<'a> = "splice"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a bug in the compiler that isn't solved by upgrade: rescript-lang/rescript#6991

@DZakh DZakh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks but looks really good!

Comment thread codegenerator/cli/templates/static/codegen/src/eventFetching/ChainManager.res Outdated
latestFetchedBlock,
firstEventBlockNumber,
fetchedEventQueue: Array.concat(self.fetchedEventQueue, newFetchedEvents),
fetchedEventQueue: Array.concat(newFetchedEvents->Array.reverse, self.fetchedEventQueue),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Letting you know that Array.reverse mutates the original array. I don't know whether it's a problem here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this is Belt.Array.reverse since it's opened at the top, is that the same?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the source code. Belt is fine, it's immutable

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirming I've tested and it does not mutate 👍🏼

() => {...self, fetchedEventQueue: self.fetchedEventQueue->Array.sliceToEnd(1)},
Item(head),
)
let getEarliestEventInRegisterWithUpdatedQueue = (self: t) =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same as getEarliestEventInRegister

a->EventUtils.getEventComparatorFromQueueItem <= b->EventUtils.getEventComparatorFromQueueItem
}, unprocessedBatch, state.chainManager.arbitraryEventQueue)
a->EventUtils.getEventComparatorFromQueueItem > b->EventUtils.getEventComparatorFromQueueItem
}, unprocessedBatch->Array.reverse, state.chainManager.arbitraryEventQueue)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here. Array.reverse mutates the array

Comment on lines +195 to +198
->Option.map(((item, index)) => {
FetchState.item,
popItemOffQueue: () => queue->Utils.Array.spliceInPlace(~pos=index, ~remove=1)->ignore,
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also creates an object here, so theoretically, the solution with ArraySlice should be even faster. The only challenge would be to pop items somewhere in the middle, as you do here. It'd require additional changes in the queue datastructure, by having a separate queue by chain, so I agree that your current solution is better 👍

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about this. But this case is only hit during unorderd multichain mode after a dynamic contract registration. So we definitely shouldn't be optimizing for this case off the bat.

JonoPrest and others added 2 commits August 29, 2024 18:59
Co-authored-by: Dmitry Zakharov <dzakh.dev@gmail.com>
@JonoPrest
JonoPrest merged commit 2aa1d13 into main Aug 29, 2024
@JonoPrest
JonoPrest deleted the jp/fix-array-slice branch August 29, 2024 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants