Skip to content

Migrate execution to dynamic dispatch#360

Open
ivanlele wants to merge 1 commit intoBlockstreamResearch:masterfrom
ivanlele:refactor/remove-jet-supertraits
Open

Migrate execution to dynamic dispatch#360
ivanlele wants to merge 1 commit intoBlockstreamResearch:masterfrom
ivanlele:refactor/remove-jet-supertraits

Conversation

@ivanlele
Copy link
Copy Markdown
Contributor

This PR closes #349 by moving all non-object-safe Jet supertraits onto Box and migrating the project’s internal generic model to dynamic dispatch.

@ivanlele
Copy link
Copy Markdown
Contributor Author

@apoelstra The main voodoo lives in stc/bit_machine/mod.rs:

let typed_jet = jet
    .as_ref()
    .as_any()
    .downcast_ref::<JE::Jet>()
    .ok_or(ExecutionError::JetTypeMismatch)?;
jet_result = self.exec_jet(typed_jet, env);

Here we downcast the dyn Jet into a concrete type so it can be matched inside c_jet_ptr.

With the move to dynamic dispatch, we lose some benefits, a few places now require additional generic boilerplate (prime example is: RedeemNode::decode::<_, _, Core>). But, we also received some: working with plain Node types becomes simpler since they no longer carry generics.

In a follow up PR, I'm thinking to add runtime checks for dyn Jet within Context, to somehow compensate for the loss of compile time checks (runtime performance will take a hit unfortunatly)

@ivanlele ivanlele marked this pull request as ready for review April 28, 2026 09:43
@apoelstra
Copy link
Copy Markdown
Collaborator

apoelstra commented Apr 28, 2026

At a conference and have only done a cursory review so far. Overall this looks great! Amazing to get rid of so many generics. Frustrating to have extra generics on Redeem::decode but we expected this (and conceptually I think it's necessary to do it this way, or an equivalent way like taking a decode_jet closure).

@apoelstra The main voodoo lives in stc/bit_machine/mod.rs:

Neat. We can revisit this in a followup. Maybe we can remove the generic from BitMachine somehow by doing dangerous things with c jet ptrs? Unsure. Would likely require more changes on the Haskell side. Let's definitely not try to do it in this PR, which is already super big, so for now this solution is good.

In a follow up PR, I'm thinking to add runtime checks for dyn Jet within Context

Yeah, this is a good idea (but should be in a followup). We may want a JetType enum or something which can be returned from the DynJet trai. Unsure.

Comment thread src/node/commit.rs Outdated
struct UnfinalizeTypes<'a, 'brand> {
inference_context: &'a types::Context<'brand>,
phantom: PhantomData<J>,
phantom: PhantomData<()>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In f33074e:

Lol. You should just delete this phantomdata.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, and the next one too. Probably should've worn my glasses when I was going over it one last time :d

Comment thread src/node/construct.rs Outdated
Comment thread src/node/construct.rs
@ivanlele ivanlele force-pushed the refactor/remove-jet-supertraits branch from f33074e to 56deeb9 Compare April 28, 2026 21:51
@ivanlele ivanlele force-pushed the refactor/remove-jet-supertraits branch from 56deeb9 to 0fb7c51 Compare April 28, 2026 21:52
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.

Make Jet object-safe

2 participants