Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
feat: unsafe optimization to reduce memory consumption to 8 bytes on …
Browse files Browse the repository at this point in the history
…64-bit arch; enabled by default
  • Loading branch information
bgeron committed Dec 11, 2020
1 parent 0943e17 commit 6d2b19e
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 242 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ jobs:
run: cargo build --all-targets
- name: Run tests
run: cargo test --all-targets
- name: Run tests for unsafe configuration (default)
run: cargo test --all-targets --features unsafe-opt
- name: Run tests for no-unsafe configuration
run: cargo test --all-targets --no-default-features
- name: Clippy
run: cargo clippy --all-targets -- -D warnings -D clippy::all -D clippy::cargo
- name: Check for correct formatting
run: cargo fmt -- --check --files-with-diff
- name: Cargo audit
run: bash -c 'which cargo-audit || cargo install cargo-audit; cargo audit --deny-warnings'

# Ideally we could run the benchmarks here, but that still requires nightly, which
# is hard to get on GitHub Actions.
#
# - name: Run benchmarks for unsafe configuration
# run: cargo +nightly bench --features unsafe-opt,bench --no-default-features --examples
# - name: Run benchmarks for no-safe configuration
# run: cargo +nightly bench --features bench --no-default-features --examples
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ num-bigint = "0.2.6"
num-traits = "0.2.11"
either = "1.5.3"
num-integer = "0.1.42"
cfg-if = "0.1.10"

[dev-dependencies]
num-iter = "0.1.40"
structopt = "0.3.12"
anyhow = "1.0.35"

[features]

default = ["unsafe-opt"]

# Enable this feature to be able to run the benchmark tests.
# This flag might go away in the near future when benchmarks
# are available on cargo stable.
bench = []
benchall = []

# An optimization that's unsafe
unsafe-opt = []
32 changes: 8 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,19 @@
[![Docs.rs link](https://docs.rs/smallbigint/badge.svg)](https://docs.rs/smallbigint)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

A wrapper around `num_bigint::BigUint` and `num_bigint::BigInt` that
stays out of the heap for small values.
Two types, `Uint` and `Int`, like `smallvec` for big integers. Anything that fits in 32 bits stays on the stack. Numbers that don't fit are stored in a `Box<num_bigint::BigUint>` / `Box<num_bigint::BigInt>`.

In the current implementation, we go to the heap for anything that
doesn't fit in 32 bits.
On 64-bit architectures, by default we use `unsafe` to compress the types to 8 bytes, exploiting pointer alignment. This behavior is triggered by the `unsafe-opt` feature, which is enabled by default.

This crate already has a lot of relevant methods, but it is not really complete
yet. Patches are welcome!
## Implemented traits

## To do, and important:

- Implement `std::fmt::{Binary, LowerHex, Octal, UpperHex}` (easy?)
- Implement `num_bigint::{ToBigInt, ToBigUint}`

## Other traits and methods still to be implemented:
Most important numeric traits have been implemented. Here are some that aren't yet; pull requests are welcome!

- `std::fmt::{Binary, LowerHex, Octal, UpperHex}`
- Bit operations
- `num_traits::Num` (easy?)
- `num_traits::Signed`
- `num_traits::Unsigned`
- `num_integer::Integer`
- `num_integer::Roots`
- `std::iter::Product`
- `std::iter::Sum`
- Other methods implemented directly on BigInt, BigUint

## Not done and seems hard:

- `num_traits::pow::Pow`
- `num_traits::Num`, `num_traits::Signed`, `num_traits::Unsigned`, `num_integer::Integer`, `num_integer::Roots`, `std::iter::Product`, `std::iter::Sum`, `num_traits::pow::Pow`
- Other methods implemented directly on `BigInt`, `BigUint`
- Implement `num_bigint::{ToBigInt, ToBigUint}`

# License

Expand Down
Loading

0 comments on commit 6d2b19e

Please sign in to comment.