Skip to content

[Merged by Bors] - Added scale to unit for memory #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

maltesander
Copy link
Member

Description

  • added method for Memory to be scaled to a desired unit (required for ZooKeeper)

Review Checklist

  • Code contains useful comments
  • (Integration-)Test cases added (or not applicable)
  • Documentation added (or not applicable)
  • Changelog updated (or not applicable)
  • Cargo.toml only contains references to git tags (not specific commits or branches)

Once the review is done, comment bors r+ (or bors merge) to merge. Further information

@maltesander maltesander requested a review from a team May 18, 2022 15:17
src/memory.rs Outdated
/// Scale up or down to the desired `binary_multiple`. Returns a new `Memory` and does
/// not change itself.
pub fn scale_to(&self, binary_multiple: BinaryMultiple) -> Self {
let self_discriminant = self.unit as i32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really love the approach of using the index of variants in the enum for the calculation. I'd much rather implement into for the enum explicitly and handle this in that code or something similar.

For example:

impl Into<i32> for CapacityUnit {
    fn into(self) -> i32 {
       match self {
           CapacityUnit::Kibi => 1000,
           CapacityUnit::Mebi => powi32(1000,2),
           CapacityUnit::Gibi => powi32(1000,3),
           CapacityUnit::Tebi => powi32(1000,4),
           CapacityUnit::Pebi => powi32(1000,5),
           CapacityUnit::Exbi => powi32(1000,6),
       }
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure what to do with that except calculate back the exponents? Would this be a compromise?

impl Into<i32> for BinaryMultiple {
    fn into(self) -> i32 {
        match self {
            BinaryMultiple::Kibi => 0,
            BinaryMultiple::Mebi => 1,
            BinaryMultiple::Gibi => 2,
            BinaryMultiple::Tebi => 3,
            BinaryMultiple::Pebi => 4,
            BinaryMultiple::Exbi => 5,
        }
    }
}

It fixes the exponent values (of the discriminats) from the enum and allows for simple calculation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd use it directly in line 129 of your pr, no?
Basically replace value: self.value * 1024f32.powi(diff), with value: self.value * (self_discriminant / to_discriminant), or similar.
But I am also happy to directly return the exponent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a discussion with @teozkr and @razvan. The into is semantically wrong. Now there is a exponential_scale_factor() method that fixes the "exponents". We added a to_java_heap_value() that can be used to scale and retrieve the pure calculated value as u32.

Copy link
Member

@razvan razvan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For JVM heap sizes you still have to multiply the value by some fraction. I feel, there is no need to expose the Memory struct to the outside world. A function that does what to_java_heap() does but in addition is ensures a given unit value should be enough. This function should of course just return a Result<u32, Error> since the caller knows the unit already.

I'm sorry I didn't implement the to_java_heap() better in the first place.

src/memory.rs Outdated
}
}

pub fn value(&self) -> f32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, these getter don't look rusty.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ill remove them and add a to_java_heap() method directly on the Memory struct?
I think i still want to return Result<(u32, BinaryMultiple), Error> seperatly in case any product requires it?

@maltesander maltesander requested a review from razvan May 19, 2022 09:19
Copy link
Member

@razvan razvan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@maltesander
Copy link
Member Author

bors merge

bors bot pushed a commit that referenced this pull request May 19, 2022
## Description

- added method for Memory to be scaled to a desired unit (required for ZooKeeper)



Co-authored-by: Malte Sander <malte.sander.it@gmail.com>
@bors
Copy link
Contributor

bors bot commented May 19, 2022

Pull request successfully merged into main.

Build succeeded:

@bors bors bot changed the title Added scale to unit for memory [Merged by Bors] - Added scale to unit for memory May 19, 2022
@bors bors bot closed this May 19, 2022
@bors bors bot deleted the added_scale_to_unit_for_memory branch May 19, 2022 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants