ElementsProject / libwally-core

Useful primitives for wallets
Other
280 stars 134 forks source link

why does bip39_get_word() use the heap -- can we have a function that returns a `const char*` instead? #427

Closed benma closed 9 months ago

benma commented 9 months ago

https://github.com/ElementsProject/libwally-core/blob/c102e7e8c7899822666e22d6cbcdaa8a1f444d38/src/bip39.c#L77

Why is the heap used here, even though the string could just be returned as is?

It would much more efficient and easier to use if we libwally exposed the wordlist_lookup_index() function instead, which returns the pointer to the existing string. Looping through all words right now for example requires malloc/free in each iteration.

jgriffiths commented 9 months ago

Hi @benma,

We currently allocate for the wrapped languages which assume ownership of output strings. I'm happy to add a non-wrapped call that doesn't allocate though (which would be available only through C/C++ and compatible languages). Does that work for your use case?

benma commented 9 months ago

@jgriffiths I see. It would be great if you could add such a function, thanks!

jgriffiths commented 9 months ago

Please try https://github.com/ElementsProject/libwally-core/pull/428