Skip to content
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

Improve memory usage for computing hybrid results in the clear #1488

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ipa-core/src/bin/in_the_clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

let input = InputSource::from(&args.input);

let input_rows = input.iter::<TestHybridRecord>().collect::<Vec<_>>();
let input_rows = input.iter::<TestHybridRecord>();

Check warning on line 52 in ipa-core/src/bin/in_the_clear.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/bin/in_the_clear.rs#L52

Added line #L52 was not covered by tests
let expected = hybrid_in_the_clear(
&input_rows,
input_rows,

Check warning on line 54 in ipa-core/src/bin/in_the_clear.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/bin/in_the_clear.rs#L54

Added line #L54 was not covered by tests
usize::try_from(args.max_breakdown_key.get()).unwrap(),
);

Expand Down
91 changes: 56 additions & 35 deletions ipa-core/src/test_fixture/hybrid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, iter::zip};
use std::{borrow::Borrow, collections::HashMap, iter::zip};

use crate::{
ff::{
Expand Down Expand Up @@ -190,38 +190,59 @@
}

enum MatchEntry {
Single(TestHybridRecord),
Pair(TestHybridRecord, TestHybridRecord),
SingleImpression { breakdown_key: u32 },
SingleConversion { value: u32 },
Attributed(Option<(u32, u32)>),
MoreThanTwo,
}

impl MatchEntry {
pub fn add_record(&mut self, new_record: TestHybridRecord) {
pub fn from_record(record: &TestHybridRecord) -> Self {
match record {
TestHybridRecord::TestImpression { breakdown_key, .. } => Self::SingleImpression {
breakdown_key: *breakdown_key,
},
TestHybridRecord::TestConversion { value, .. } => {
Self::SingleConversion { value: *value }
}
}
}

pub fn add_record(&mut self, new_record: &TestHybridRecord) {
match self {
Self::Single(old_record) => {
*self = Self::Pair(old_record.clone(), new_record);
MatchEntry::SingleImpression { breakdown_key, .. } => {
*self = Self::attribute_impression(*breakdown_key, new_record);
}
MatchEntry::SingleConversion { value } => {
*self = Self::attribute_conversion(*value, new_record);
}
_ => *self = Self::MoreThanTwo,
}
}

fn attribute_impression(breakdown_key: u32, new_record: &TestHybridRecord) -> Self {
match new_record {
TestHybridRecord::TestImpression { .. } => Self::Attributed(None),

Check warning on line 225 in ipa-core/src/test_fixture/hybrid.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/test_fixture/hybrid.rs#L225

Added line #L225 was not covered by tests
TestHybridRecord::TestConversion { value, .. } => {
Self::Attributed(Some((breakdown_key, *value)))
}
Self::Pair { .. } | Self::MoreThanTwo => *self = Self::MoreThanTwo,
}
}

fn attribute_conversion(value: u32, new_record: &TestHybridRecord) -> Self {
match new_record {
TestHybridRecord::TestImpression { breakdown_key, .. } => {
Self::Attributed(Some((*breakdown_key, value)))
}
TestHybridRecord::TestConversion {
value: other_value, ..
} => Self::Attributed(Some((0, value + *other_value))),
}
}

pub fn into_breakdown_key_and_value_tuple(self) -> Option<(u32, u32)> {
match self {
Self::Pair(imp, conv) => match (imp, conv) {
(
TestHybridRecord::TestImpression { breakdown_key, .. },
TestHybridRecord::TestConversion { value, .. },
)
| (
TestHybridRecord::TestConversion { value, .. },
TestHybridRecord::TestImpression { breakdown_key, .. },
) => Some((breakdown_key, value)),
(
TestHybridRecord::TestConversion { value: value1, .. },
TestHybridRecord::TestConversion { value: value2, .. },
) => Some((0, value1 + value2)),
_ => None,
},
Self::Attributed(v) => v,
_ => None,
}
}
Expand All @@ -230,28 +251,28 @@
/// # Panics
/// It won't, so long as you can convert a u32 to a usize
#[must_use]
pub fn hybrid_in_the_clear(input_rows: &[TestHybridRecord], max_breakdown: usize) -> Vec<u32> {
pub fn hybrid_in_the_clear<I: IntoIterator<Item: Borrow<TestHybridRecord>>>(
input_rows: I,
max_breakdown: usize,
) -> Vec<u32> {
let mut attributed_conversions = HashMap::<u64, MatchEntry>::new();
for input in input_rows {
match input {
TestHybridRecord::TestConversion { match_key, .. }
| TestHybridRecord::TestImpression { match_key, .. } => {
match input.borrow() {
r @ (TestHybridRecord::TestConversion { match_key, .. }
| TestHybridRecord::TestImpression { match_key, .. }) => {
attributed_conversions
.entry(*match_key)
.and_modify(|e| e.add_record(input.clone()))
.or_insert(MatchEntry::Single(input.clone()));
.and_modify(|e| e.add_record(r))
.or_insert(MatchEntry::from_record(r));
}
}
}

let pairs = attributed_conversions
.into_values()
.filter_map(MatchEntry::into_breakdown_key_and_value_tuple)
.collect::<Vec<_>>();

let mut output = vec![0; max_breakdown];
for (breakdown_key, value) in pairs {
output[usize::try_from(breakdown_key).unwrap()] += value;
for entry in attributed_conversions.into_values() {
if let Some((breakdown_key, value)) = entry.into_breakdown_key_and_value_tuple() {
output[usize::try_from(breakdown_key).unwrap()] += value;
}
}

output
Expand Down
Loading