ElementsProject / libwally-core

Useful primitives for wallets
Other
284 stars 136 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 1 year ago

benma commented 1 year 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 1 year 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 1 year ago

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

jgriffiths commented 1 year ago

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