Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • dragoon/komodo
  • a.stevan/komodo
  • c.heme/komodo
3 results
Show changes
Commits on Source (2)
  • STEVAN Antoine's avatar
    improve performances by not shuffling vectors (!122) · 47ba0de8
    STEVAN Antoine authored
    in `./bins/inbreeding/`, this MR does
    - refactor the "list item drawing" from `environment.rs` and `strategy.rs` into the `draw_unique_elements` function of new `random.rs` module
    - use a `HashSet` to draw unique indices in the slice of "things" to draw from and then extracts the items corresponding to these indices
    
    ## results
    ```bash
    use ./bins/inbreeding
    use std bench
    
    const PRNG_SEED = 0
    const OPTS = {
        nb_bytes: (10 * 1_024),
        k: 10,
        n: 20,
        nb_measurements: 100,
        nb_scenarii: 10,
        measurement_schedule: 1,
        measurement_schedule_start: 2_000,
        max_t: 2_000,
        strategies: [ "single:5" ],
        environment: null,
    }
    
    def run [rev: string] {
        git co $rev
    
        inbreeding build
    
        let a = bench --rounds 5 {
            inbreeding run --options ($OPTS | update environment "fixed:0") --prng-seed $PRNG_SEED
        }
        let b = bench --rounds 5 {
            inbreeding run --options ($OPTS | update environment "fixed:1") --prng-seed $PRNG_SEED
        }
    
        {
            0: $a,
            1: $b,
        }
    }
    
    let main = run a29b511d
    let mr = run fix-shuffle
    ```
    ```bash
    let table = [
        [env, main, mr, improvement];
    
        ["fixed:0", $main."0".mean, $mr."0".mean, (($main."0".mean - $mr."0".mean) / $main."0".mean * 100)],
        ["fixed:1", $main."1".mean, $mr."1".mean, (($main."1".mean - $mr."1".mean) / $main."1".mean * 100)],
    ]
    
    $table | to md --pretty
    ```
    
    | env     | main                    | mr                      | improvement        |
    | ------- | ----------------------- | ----------------------- | ------------------ |
    | fixed:0 | 8sec 504ms 794µs 784ns | 6sec 353ms 206µs 645ns | 25.298530930431912 |
    | fixed:1 | 727ms 648µs 292ns      | 639ms 443µs 782ns      | 12.12186037811795  |
    
    the improvement is quite nice, even though not huge, but the code is cleaner anyways :pray:
    47ba0de8
  • STEVAN Antoine's avatar
    add tests for the `color.nu` module (!121) · 4212bb72
    STEVAN Antoine authored
    this also bumps Nushell to 0.93.0 to include the "extra" command `fmt`, see [Nushell 0.92.0](https://www.nushell.sh/blog/2024-04-02-nushell_0_92_0.html#incorporating-the-extra-feature-by-default-toc)
    4212bb72
......@@ -6,7 +6,7 @@ stages:
variables:
NUSHELL_ARCH: "x86_64-unknown-linux-musl"
NUSHELL_VERSION: "0.91.0"
NUSHELL_VERSION: "0.93.0"
workflow:
rules:
......
......@@ -67,10 +67,16 @@ export def "color from-ints" [
}
def try-string-to-int []: string -> int {
let s = $in
try {
$"0x($in)" | into int
} catch {
get debug | parse --regex 'CantConvert { to_type: "(?<to>.*)", from_type: "(?<from>.*)", span: Span { (?<span>.*) }, help: Some\("(?<help>.*)"\) }' | into record | error make --unspanned { msg: ($in.help | str replace --all '\"' '"') }
$"0x($s)" | into int
} catch { |e|
let err = $e.debug
| parse --regex 'CantConvert { to_type: "(?<to>.*)", from_type: "(?<from>.*)", span: Span { (?<span>.*) }, help: Some\("(?<help>.*)"\) }'
| into record
let msg = $err.help | str replace --all '\"' '"'
error make --unspanned { msg: $"($msg), found ($s)" }
}
}
......
......@@ -19,6 +19,7 @@ test:
cargo test --examples --verbose
nu tests/cli.nu
nu tests/binary.nu
nu tests/color.nu
example:
nu examples/cli.nu
......
......@@ -13,7 +13,7 @@ below is an example of how to use the binary application with Nushell:
```
> **Note**
> requires Nushell 0.89.0 or later
> requires Nushell 0.93.0 or later
tests for the binary application can also be run with
```bash
......
use rand::{seq::SliceRandom, Rng, RngCore};
use rand::{Rng, RngCore};
use crate::random::draw_unique_elements;
#[derive(Debug, PartialEq)]
pub(super) enum Environment {
......@@ -14,28 +16,23 @@ impl Environment {
/// `update(things, rng)` is `things` with some elements potentially removed according to the
/// [`Environment`] type
pub(super) fn update<T: Clone>(&self, things: &[T], rng: &mut impl RngCore) -> Vec<T> {
let mut things = things.to_vec();
things.shuffle(rng);
match self {
Environment::Fixed { n } => things.iter().take(things.len() - n),
let nb_to_take = match self {
Environment::Fixed { n } => things.len() - n,
Environment::RandomFixed { p, n } => {
if rng.gen::<f64>() > *p {
return things;
return things.to_vec();
}
things.iter().take(things.len() - n)
things.len() - n
}
Environment::RandomDynamic { p, q } => {
if rng.gen::<f64>() > *p {
return things;
return things.to_vec();
}
things
.iter()
.take((things.len() as f64 * (1.0 - q)) as usize)
(things.len() as f64 * (1.0 - q)) as usize
}
}
.cloned()
.collect()
};
draw_unique_elements(things, nb_to_take, rng)
}
pub(super) fn from_str(s: &str) -> Result<Self, String> {
......
......@@ -9,9 +9,11 @@ use komodo::{
fec::{self, Shard},
linalg::Matrix,
};
use rand::{rngs::StdRng, seq::SliceRandom, Rng, RngCore, SeedableRng};
use rand::{rngs::StdRng, Rng, RngCore, SeedableRng};
use random::draw_unique_elements;
mod environment;
mod random;
mod strategy;
use crate::{environment::Environment, strategy::Strategy};
......@@ -38,16 +40,13 @@ fn measure_inbreeding<F: PrimeField>(
sty: &ProgressStyle,
rng: &mut impl RngCore,
) -> f64 {
let mut s: Vec<_> = shards.to_vec();
let mut count = 0;
let pb = mp.add(ProgressBar::new(nb_measurements as u64));
pb.set_style(sty.clone());
pb.set_message("measure");
for _ in 0..nb_measurements {
// get any k of the shards
s.shuffle(rng);
if fec::decode(s.iter().take(k).cloned().collect()).is_ok() {
if fec::decode(draw_unique_elements(shards, k, rng)).is_ok() {
count += 1;
}
pb.inc(1);
......
use rand::{Rng, RngCore};
use std::collections::HashSet;
fn draw_unique_indices(n: usize, vec_len: usize, rng: &mut impl RngCore) -> HashSet<usize> {
let mut indices = HashSet::new();
while indices.len() < n {
let idx = rng.gen_range(0..vec_len);
indices.insert(idx);
}
indices
}
pub(super) fn draw_unique_elements<T: Clone>(
things: &[T],
n: usize,
rng: &mut impl RngCore,
) -> Vec<T> {
let mut res = vec![];
for i in draw_unique_indices(n, things.len(), rng) {
res.push(things[i].clone());
}
res
}
use rand::{seq::SliceRandom, Rng, RngCore};
use rand::{Rng, RngCore};
use crate::random::draw_unique_elements;
#[derive(Debug, PartialEq)]
pub(super) enum Strategy {
......@@ -8,9 +10,6 @@ pub(super) enum Strategy {
impl Strategy {
pub(super) fn draw<T: Clone>(&self, things: &[T], rng: &mut impl RngCore) -> Vec<T> {
let mut things = things.to_vec();
things.shuffle(rng);
let nb_to_take = match self {
Self::Single { n } => *n,
Self::Double { p, n, m } => {
......@@ -22,7 +21,7 @@ impl Strategy {
}
};
things.iter().take(nb_to_take).cloned().collect()
draw_unique_elements(things, nb_to_take, rng)
}
pub(super) fn from_str(s: &str) -> Result<Self, String> {
......
use ../.nushell/color.nu [
"color from-floats",
"color from-ints",
"color from-string",
"color mix",
"color to-hex",
RED,
GREEN,
]
use std assert
def "assert error" [code: closure, --msg: string, --body: string] {
try {
do $code
} catch { |e|
if $msg != null and ($e.msg | ansi strip) != $msg {
error make {
msg: $"(ansi red_bold)assertion: bad error message(ansi reset)",
label: {
text: $"error should have message '($msg)'",
span: (metadata $code).span,
},
help: $"actual: ($e.msg | ansi strip)",
}
}
if $body != null and not ($e.debug | ansi strip | str contains $body) {
let actual = $e.debug | ansi strip | parse "{foo}text: {text}, span: {bar}" | into record | get text
error make {
msg: $"(ansi red_bold)assertion: bad error body(ansi reset)",
label: {
text: $"error should contain '($body)'",
span: (metadata $code).span,
},
help: $"actual: ($actual)",
}
}
return
}
error make --unspanned { msg: "should error" }
}
assert error { || color from-floats 2 1 1 } --msg "invalid RGB channel" --body "should be between 0 and 1, found 2"
assert error { || color from-floats 1 (-2 | into float) 1 } --msg "invalid RGB channel" --body "should be between 0 and 1, found -2"
assert error { || color from-floats 1 1 3.4 } --msg "invalid RGB channel" --body "should be between 0 and 1, found 3.4"
assert error { || color from-ints 256 0 0 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
assert error { || color from-ints 0 256 0 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
assert error { || color from-ints 0 0 256 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
assert error { || color from-string "foo" } --msg "invalid HEX color format" --body "format should be '#RRGGBB', found foo"
assert error { || color from-string "#foo" } --msg "invalid HEX color format" --body "format should be '#RRGGBB', found #foo"
assert error { || color from-string "#xxxxxx" } --msg "hexadecimal digits following \"0x\" should be in 0-9, a-f, or A-F, found xx"
assert error { || color from-string "#0123yy" } --msg "hexadecimal digits following \"0x\" should be in 0-9, a-f, or A-F, found yy"
assert equal (color from-floats 0.1 0.2 0.3) { r: 0.1, g: 0.2, b: 0.3 }
assert equal (color from-ints 1 2 3) { r: (1 / 255), g: (2 / 255), b: (3 / 255) }
assert equal (color from-string "#010203") { r: (1 / 255), g: (2 / 255), b: (3 / 255) }
assert equal (color from-string "#010203" | color to-hex) "#010203"
assert equal (color mix $RED $GREEN 0.5) (color from-floats 0.5 0.5 0.0)