1

Does anyone have experience using VecDeque in an Anchor project?

I have the following structure:

#[account]
pub struct Market {
    pub bids: VecDeque<Order>,
    pub asks: VecDeque<Order>,
    pub quote_token: Pubkey,
    pub base_token: Pubkey,
}

It compiles just fine but running anchor test results in the following:

IdlError: Type not found: {"name":"bids","type":{"defined":"VecDeque<Order>"}}
    at Function.fieldLayout (/home/user/Downloads/CLOB/node_modules/@coral-xyz/anchor/src/coder/borsh/idl.ts:100:19)
    at /home/user/Downloads/CLOB/node_modules/@coral-xyz/anchor/src/coder/borsh/idl.ts:128:28
    at Array.map (<anonymous>)

From this, I infer that anchor-ts can't work with the VecDeque structure. Is there another double-ended queue structure that Anchor supports? If not, is my best best to write the tests in Rust?

1 Answer 1

5

Not sure if it is supported, but you won't see much (really any) performance gain over Vec. Your on-chain memory usage is typically limited to a few hundred items at best and push/pop from the head for Vec vs VecDeque will be about the same when the lists are that small. You're not going to see any appreciable difference in performance until the number of items is in the tens of thousands.

VecDeque keeps buffer space in the middle, e.g. [tail data n-3, n-2, n-1][buffer][head data 1, 2, 3], which would make it hard to serialize without wasting the middle buffer. If we serialized it without the buffer, then it would be full and need to realloc after being deserialized (if you try to add any items), so at that point it's equivalent to a realloc with VecDeque::from(some_vec); anyways.

So if you really need VecDeque, just store a Vec on-chain on realloc into a VecDeque when loading it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.