Repository navigation
MIT-licensed Rhai functions for messy dates, amounts and contact data #1220
Replies: 3 comments 5 replies
|
This looks wonderful. What is behind your decision to keep those as Rhai functions instead of coding them in Rust? For recurrent use I assume it would be more efficient in Rust... Have you found performance bottlenecks when using them? Also: I'd be interested to test them under Rhai Grain. Is there a test suite? Lastly: would you consider contributing the repo to the Rhai org where you'll be the primary maintainer? Thanks again! |
|
Thanks, glad you like them. Why Rhai and not Rust: we do both. For heavier work we write the modules in Rust and compile them to WASM, and Neblex runs them in a sandbox: https://github.com/neblexhq/neblex-wasm-modules (PDF, XML, email and fixed-width parsing, PII redaction, identifier validation and so on). The Rhai modules cover lighter, per-record jobs where people will want to change the rules: adding a country, a legal form or a nickname, or a date format. A user can read the script, edit it and use it in a step right away, with nothing to compile or upload. Performance: in real workflows these modules haven't been a bottleneck. The friction is in the API calls around them. But your question made us measure it. I ported five of the heavier data-quality functions to plain Rust line by line and ran both on the plain Rhai engine over 20,000 contact records, with identical output. Native Rust was 16-92x faster per function and about 35x faster for all five together. In absolute terms, Rhai ran all five in about 75 microseconds per record, while one API call in a flow usually takes tens to hundreds of milliseconds. Some of the gap is probably our style. The functions build strings one character at a time, so the scripts can be tuned. Test suite and Grain: each module has I also ran the suite under Grain. All four modules convert fully (0 residual fragments) and all 952 cases pass. On the benchmark Grain was 1.33x faster than the AST walker, below the 1.8-2x in the README, probably because the time goes into string built-ins, not control flow. Happy to share the harness and full numbers if useful. Moving it to the Rhai org: happy to, and I'll keep maintaining it. My one request is to keep the section on using the modules in Neblex and the link to our WASM modules, since that's how most of our users find and use them. Let me know how you'd like to do the transfer. Thanks again for Rhai. It's been a pleasure to build on. If you'd like to see the modules running in Neblex, I'm happy to set you up on our free edition. |
|
That's great. I think how we can move this to the Rhai org is:
No restriction with your links and README info really. Licensing et al can be chosen separately from other repo's. The Rhai org right now is more a loose collection of related resources. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I work at Neblex, where we embed Rhai for custom transformation steps in integration flows. We've published the functions we kept writing as plain Rhai modules, and they run on the stock engine, so they should be useful to anyone embedding Rhai for data work.
Repo: https://github.com/neblexhq/neblex-rhai-modules (MIT)
What's in it, with examples:
to_iso_date("3 Apr 2026", "mdy")gives2026-04-03; all-number dates take"mdy"or"dmy"instead of guessing.excel_serial_to_iso(46115)handles Excel's serial dates, including the Feb 29, 1900 that Excel counts but never existed.parse_numberreads$1,234.56,EUR 1.234,56,CHF 1'234.50, and ERP-style negatives like(500.00)and1,234-. It returns empty for12a34instead of a wrong value.o'NEILto O'Neil), and match keys for dedupe (The Acme Company, Inc.andACME Coboth becomeacme).Each module has a
cases.jsonof inputs and expected results. A small Rust runner (test-runner/) runs them all against the Rhai engine in CI, each module alone and all loaded together:A few things Rhai made easy: the functions are plain
fns, so a host can load them as a module and call them by name, and the sandbox means a function can only transform the values it's given. Rules that depend on the source (day-month order, decimal separator) are passed as arguments rather than read from global state, so the same module serves sources with different conventions.There's a longer write-up with a four-row before/after here: https://www.neblex.com/blog/custom-data-transformation-functions
Feedback welcome, especially on idioms we could do better in Rhai, or functions you'd expect in a library like this.
All reactions