Crates.io | nanokit |
lib.rs | nanokit |
version | 0.2.0 |
source | src |
created_at | 2024-04-05 14:46:44.065556 |
updated_at | 2024-10-11 12:56:59.212368 |
description | A collection of tiny, reusable utility methods that reduce code size and improve performance. |
homepage | |
repository | https://github.com/Sewer56/nanokit-rs |
max_upload_size | |
id | 1197413 |
size | 43,094 |
A collection of tiny, reusable utility methods that reduce code size and improve performance. Readme for developers/contributors is available at README-DEV.MD.
Fast way to concatenate strings:
use nanokit::string_concat::concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2(base_msg, text);
assert_eq!(result, "Hello, world!");
And with any type that implements AsRef<str>
:
use nanokit::string_concat::concat_2;
let base_string = String::from("The quick brown fox ");
let text_string = String::from("jumps over the lazy dog.");
let result = concat_2(base_string, text_string);
assert_eq!(result, "The quick brown fox jumps over the lazy dog.");
This is similar to libraries like concat_strs and string_concat, except that instead of pushing strings onto a preallocated String
instance, we instead unsafely create a new String
instance and adjust the length.
This saves around 150 bytes
of code in codebases that otherwise don't use string concatenation (push_str
or add).
Also saves some instructions.
Additional methods concat_3
, concat_4
, concat_5
exist.
You can save on another 2 instructions per concatenation if you know the final string length
is lesser than isize::MAX
.
use nanokit::string_concat::concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2_no_overflow(base_msg, text);
assert_eq!(result, "Hello, world!");
no-inline-concat
: Disables inlining of string concat functions (saves code size).Provides a method to determine the number of bits needed to store a given number.
let number: u64 = 5;
println!("Bits needed for {}: {}", number, number.bits_needed_to_store());
The produced code does not panic, nor does it branch.
Generated x86_64 assembly for u64
:
bits_needed_to_store:
lzcnt rcx, rdi
mov eax, 64
sub eax, ecx
ret
Note: The number of bits needed to store the value 0
is returned as 0
.
This may not match your expectations. If you expect the result to be 1; try
the count-digits crate by nordzilla. Do note that said crate uses ilog2
, which
may panic.
See CONTRIBUTING for guidance on how to contribute to this project.
nanokit
is part of the Reloaded
suite of libraries.
Licensed under MIT.
If you find this library useful, please contribute back!!