Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
parity-test-sync
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
parity-test-sync
Commits
926efb6f
Commit
926efb6f
authored
9 years ago
by
Marek Kotewicz
Browse files
Options
Downloads
Patches
Plain Diff
docs, tests and bugfixes for squeeze
parent
56de0813
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/squeeze.rs
+38
-4
38 additions, 4 deletions
src/squeeze.rs
with
38 additions
and
4 deletions
src/squeeze.rs
+
38
−
4
View file @
926efb6f
//! Helper module that should be used to randomly squeeze
//! caches to a given size in bytes
//!
//! ```
//! extern crate heapsize;
//! extern crate ethcore_util as util;
//! use std::collections::HashMap;
//! use std::mem::size_of;
//! use heapsize::HeapSizeOf;
//! use util::squeeze::Squeeze;
//!
//! fn main() {
//! let initial_size = 60;
//! let mut map: HashMap<u8, u8> = HashMap::with_capacity(initial_size);
//! assert!(map.capacity() >= initial_size);
//! for i in 0..initial_size {
//! map.insert(i as u8, i as u8);
//! }
//!
//! assert_eq!(map.heap_size_of_children(), map.capacity() * 2 * size_of::<u8>());
//! assert_eq!(map.len(), initial_size);
//! let initial_heap_size = map.heap_size_of_children();
//!
//! // squeeze it to size of key and value
//! map.squeeze(2 * size_of::<u8>());
//! assert_eq!(map.len(), 1);
//!
//! // its likely that heap size was reduced, but we can't be 100% sure
//! assert!(initial_heap_size >= map.heap_size_of_children());
//! }
//! ```
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashMap
;
use
std
::
hash
::
Hash
;
use
std
::
hash
::
Hash
;
use
heapsize
::
HeapSizeOf
;
use
heapsize
::
HeapSizeOf
;
...
@@ -14,9 +46,10 @@ impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T:
...
@@ -14,9 +46,10 @@ impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T:
}
}
let
size_of_entry
=
self
.heap_size_of_children
()
/
self
.capacity
();
let
size_of_entry
=
self
.heap_size_of_children
()
/
self
.capacity
();
let
mut
shrinked_size
=
size_of_entry
*
self
.len
();
let
all_entries
=
size_of_entry
*
self
.len
();
let
mut
shrinked_size
=
all_entries
;
while
self
.len
()
>
0
||
shrinked_size
>
size
{
while
self
.len
()
>
0
&&
shrinked_size
>
size
{
// could be optimized
// could be optimized
let
key
=
self
.keys
()
.next
()
.unwrap
()
.clone
();
let
key
=
self
.keys
()
.next
()
.unwrap
()
.clone
();
self
.remove
(
&
key
);
self
.remove
(
&
key
);
...
@@ -25,9 +58,10 @@ impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T:
...
@@ -25,9 +58,10 @@ impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T:
self
.shrink_to_fit
();
self
.shrink_to_fit
();
// if we
havent shrinked
enough, squeeze again
// if we
squeezed something, but not
enough, squeeze again
if
self
.heap_size_of_children
()
>
size
{
if
all_entries
!=
shrinked_size
&&
self
.heap_size_of_children
()
>
size
{
self
.squeeze
(
size
);
self
.squeeze
(
size
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment