Code Review: nanoid

Hi,

This is my first public work done with Mojo:
GitHub - cirello-py/nanoid

I would appreciate if I could get some code review. I must say I struggled a bit with the lack of examples in the documentation, and lack of access to the source code to validate some of my concerns (especially, around the random package, as I wanted to know what source it uses).

I look forward for feedback.

Consider using String Literal for the alphabet as it is not a dynamic value.
Initialising a string without a String constructor assumes StringLiteral

Also, did you try compiling to .mojopkg?
I usually stub out init.mojo within the subdirs, but does it still work without it? Cause I see you have a global init

Also as far as I understand you initialise an array buffer to 0 but then do a randint fill pass, rendering 0-setting redundant?
Or was the compiler playing nicer?
Consider compiling to mlir representation to observe the internal flow

*llvm IR, sorry

Also, did you try compiling to .mojopkg?
I usually stub out init .mojo within the subdirs, but does it still work without it? Cause I see you have a global init

I think I got this one wrong! I’m going to fix it.

1 Like

StringLiteral is a dependent type now so I would avoid using it in this way now. With the recent work on SSO for String there’s also little reason to do so as String will intelligently build a reference to the static data where the default alphabet lives.

See this ctor for String modular/mojo/stdlib/stdlib/collections/string/string.mojo at main · modular/modular · GitHub

randint just repeatedly calls to random_ui64/random_si64. So you can avoid that allocation entirely and just call to random_ui64 directly within you loop

hi @bgreni – I iterated according to your suggestion. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.