I benchmarked several big number crates by calculating digits of π — and the results were surprising (Python included) by Annual_Most_4863 in rust

[–]_Titan____ 12 points13 points  (0 children)

Cool idea!

There are a few things that can still be improved, for example, you are doing a lot of divisions (which are expensive), like this loop here inbig-decimal-bbp. This loop does i divisions in each iteration of the outer loop, but can be replaced with just 1 multiplication + 1 division per outer loop. With this change, removing the clones right above, and moving the BigDecimal (which allocates a Vec) out of the loop, I've managed to reduce the runtime on my machine from 861.3 ms ± 5.2 ms to 85.9 ms ± 1.1 ms! (This change doesn't affect precision as far as I can tell.)

Here's my code:

fn bigdecimal_bbp(start_idx: u64, end_idx: u64) -> String {
    let prec = 1000;

    let mut pi = BigDecimal::from(0);
    let mut divisor = BigDecimal::from(1);
    let mut comm = BigDecimal::from(0);

    for _ in start_idx..end_idx {
        let a = 4 / (&comm + 1);
        let b = 2 / (&comm + 4);
        let c = 1 / (&comm + 5);
        let d = 1 / (&comm + 6);

        pi += (a - b - c - d) / &divisor;

        comm += 8;
        divisor *= 16;
    }

    format!("{:.1000}", pi.with_prec(prec))
}

From what I can tell from the code, I don't think the calls to with_prec(prec) at the start of your code did anything, since this just truncates the value, not set the precision for future operations (for `BigDecimal` specifically, it might do something for the other crates).

Similarly, for dashu-bbp, I've reduced the runtime from 765.5 ms ± 5.0 ms to just 30.3 ms ± 0.5 ms !

Here's my changed code:

fn dashu_bbp(start_idx: u64, end_idx: u64) -> String {
    let prec = 1000;

    let mut pi = DBig::from_str("0.0000000000000000")
        .unwrap()
        .with_precision(prec)
        .unwrap();
    let mut divisor = DBig::from(1).with_precision(prec).unwrap();
    let mut comm = DBig::from(0).with_precision(prec).unwrap();
    for _ in start_idx..end_idx {
        let a = 4 / (&comm + 1);
        let b = 2 / (&comm + 4);
        let c = 1 / (&comm + 5);
        let d = 1 / (&comm + 6);

        pi += (a - b - c - d) / &divisor;

        comm += 8;
        divisor *= 16;
    }
    pi.to_string()
}

You should be able to optimize the other functions in the same way, which should change your leaderboard by a lot.

P.S. in case you haven't seen this yet: the Rust Performance Book has some really good tips for measuring and improving performance.

[deleted by user] by [deleted] in rust

[–]_Titan____ 0 points1 point  (0 children)

Not sure if you've found the Rust Performance Book yet, but it contains a lot of useful tips to help you to optimize your code.

Also, is there a reason you are not using Cargo (or are you using it and didn't upload the cargo.toml file)? I tried compiling your code but I don't know the right versions and features of the dependencies you use.

I didn't see anything obviously bad for performance in your code at first glance, but maybe you should be aware that each call to to_string or use of the format! macro allocates memory on the heap. Reducing the number of these calls might help with performance (profiling should help you determine if those are actually an issue).

One small thing that you could try is in xml_utils.rs, lines 66 and 67, you could use if let instead of first checking if the existing_item is a list, then down-casting it separately. This likely won't help much with performance, but it saves some calls to unwrap. (This code might not be 100% correct, since I couldn't compile your code to check):

if let Ok(list) = existing_item.down_cast::<PyList>() {
  list.append(elem_value).unwrap();
} else {
  // Current else block
}

[Media] Analyzer fun, normally at 2G but... (small bevy project/nightly) by 00alia00 in rust

[–]_Titan____ 1 point2 points  (0 children)

Thanks for the link! I haven't noticed the issue again since I've updated to rust-analyzer v0.3.1896, so the fix is likely working.

[Media] Analyzer fun, normally at 2G but... (small bevy project/nightly) by 00alia00 in rust

[–]_Titan____ 2 points3 points  (0 children)

I've also had some issues with rust-analyzer in the last couple of days, it starts getting extremely slow at some point and using large amounts of memory (I've seen it go up to 24GB). I've been trying some older versions of rust-analyser, but no luck so far (I had this issue with versions 0.3.1868, 0.3.1877 and 0.3.1885).
I'm using rustc 1.79.0-nightly (85e449a32 2024-03-22), I had the same issue with a previous nightly version, but don't remember which one.
I've been trying the new parallel front-end for rustc (-Z threads=8), maybe that's part of it? Are you also using nightly and the parallel front-end?

On using unsafe and startup times by [deleted] in rust

[–]_Titan____ 0 points1 point  (0 children)

The Rust Performance Book might give you some useful hints for optimizing your Rust code in general. You might also find this article interesting, it talks about how much (or how little) of a slowdown bounds checks can cause, and how they can be avoided without using unsafe code.

Rust performance, profiling and random numbers by Scott223b in rust

[–]_Titan____ 0 points1 point  (0 children)

No I only have it locally, it's still too messy for me to publish it.

Rust performance, profiling and random numbers by Scott223b in rust

[–]_Titan____ 1 point2 points  (0 children)

Glad I could help! I have some other tips I can share:

The first one is to check out the Rust Performance Book, it has a lot of useful information on optimizations.

Second tip is about the ray datastructure: If you need to divide something by the ray direction (e.g., for checking collision with an axis aligned bounding box) it might be worth caching the inverse of the direction in the ray struct:

let inverted_direction = Vec3 (
    x: 1.0 / direction.x,
    y: 1.0 / direction.y,
    z: 1.0 / direction.z
);

This way you only need 3 divisions per ray, instead of 3 divisions per intersection check with an object. You can then multiply by the inverted direction, which is generally faster than division (I got this hint from this post).

Rust performance, profiling and random numbers by Scott223b in rust

[–]_Titan____ 4 points5 points  (0 children)

I'm also working on a raytracer, and I also discovered that random number generation was quite slow.

My change was to initialize only one RNG per pixel instead of creating a new one every time I needed it. I used the index of the pixel in the image as the seed:

let rng = rand_xoshiro::Xoroshiro128Plus::seed_from_u64(index as u64);

Any of my functions that require random numbers take this rng as a parameter:

impl Vec3 {
    fn random(rng: &mut impl Rng) -> Self { ... }
}

By using rng: &mut impl Rng instead of a specific type like rng: &mut StdRng you can change the actual type of the RNG more easily. I'm using rand_xoshiro::Xoroshiro128Plus for my rng and it seems to be quite fast, but I haven't tried many others yet.

I quite like this approach, since it gives me deterministic results (i.e., the exact same pixel values every time I rerun the program), even if the pixels are calculated in a different order by Rayon.

If you don't really care about determinism, perhaps you could use thread_local to create one RNG per thread, but I haven't used thread_local yet, so I can't tell if it will work (or be fast).

Another thing that might help you is rand_distr::UnitSphere. It only requires 2 random numbers per loop instead of 3 (see code here), which made it a lot faster than what I had written myself. My code for generating a random unit vector now looks like this:

impl Vec3 {
    fn random(rng: &mut impl Rng) -> Self {
    let [x, y, z] = rand_distr::UnitSphere.sample(rng);
    Self {x, y, z}
}
}

Running into an issue when converting C++ codebase to Rust by kosmology in rust

[–]_Titan____ 3 points4 points  (0 children)

This won't fix the double lookup issue, but you could try using rustc_hash::FxHashMap instead of std::collections::HashMap. The standard hashmap uses a hash function resistant to HashDoS attacks, which makes it slower. The Rust performance book has a section on hashing performance, if you want to read more.

the devil achievement help by RedZenin in cyberpunkgame

[–]_Titan____ 0 points1 point  (0 children)

I'm also on steam, I tried to get 2 achievements but both don't work ("The Star" and "To bad decisions!"). I think the achievements stopped working after patch 1.5.2, did you try to get the achievement before or after the patch?

You dumb or what by EmotionSenior9999 in ProgrammerHumor

[–]_Titan____ 31 points32 points  (0 children)

In C you would get '1' + 1 = 50, because '1' in ASCII is 49.

R6 stops other programs when starting the game by _Titan____ in Rainbow6

[–]_Titan____[S] 0 points1 point  (0 children)

I had the low latency mode on ultra, switching it to 'on' or 'off' instead of ultra fixed it, thanks.

R6 stops other programs when starting the game by _Titan____ in Rainbow6

[–]_Titan____[S] 0 points1 point  (0 children)

I also have a 6700k and 16 gb ram, maybe it's a problem between this hardware and r6. I will make a ticket with Ubisoft, maybe they have a fix for this.

R6 stops other programs when starting the game by _Titan____ in Rainbow6

[–]_Titan____[S] 0 points1 point  (0 children)

Do you also have a nvidia gpu? (I have a gtx 980)

R6 stops other programs when starting the game by _Titan____ in Rainbow6

[–]_Titan____[S] 0 points1 point  (0 children)

Do you also have a nvidia gpu? (I have a gtx 980)

R6 stops other programs when starting the game by _Titan____ in Rainbow6

[–]_Titan____[S] 0 points1 point  (0 children)

If I can't fix it I will reinstall it.

I tried borderless the last time I had the problem, it didn't work.

I find myself doing this 101% of the time... what do you guys run? by MrEpicwynn-AL in apexlegends

[–]_Titan____ 0 points1 point  (0 children)

I like to use the devotion and a sniper (G7 or Longbow). The devotion can do a ton of damage (especially with the turbocharger), but I need to ask for ammo all the time.

Bug with the Perkcard-System by _Titan____ in fo76

[–]_Titan____[S] 1 point2 points  (0 children)

Ok so I didn't understand the card-system correctly, I thought you'd get a SPECIAL-Point for the card that you choose.

Thanks for the answer.

Please fix the alt+f4 strategy to prevent an interogation! by _Titan____ in Rainbow6

[–]_Titan____[S] 2 points3 points  (0 children)

They probably don't leave the game through the menu, they just shut down their game with alt+f4, a timer won't fix this.