From c41d51df8a314150c46cbbff31f8140a35dfb02c Mon Sep 17 00:00:00 2001 From: ordian Date: Tue, 29 Nov 2022 18:18:24 +0100 Subject: [PATCH] release is coming (#697) * update ethereum-types and uint changelogs * purge parity-util-mem * cargo set-version --bump minor -p kvdb * cargo set-version --bump patch -p uint * cargo set-version --bump patch -p ethereum-types * kvdb-rocksdb: rm unused path field * kvdb-rocksdb: rm unused method * update kvdb&co * rm extra newline --- Cargo.toml | 2 - ethereum-types/CHANGELOG.md | 3 + ethereum-types/Cargo.toml | 2 +- kvdb-memorydb/CHANGELOG.md | 3 + kvdb-memorydb/Cargo.toml | 6 +- kvdb-rocksdb/CHANGELOG.md | 3 + kvdb-rocksdb/Cargo.toml | 7 +- kvdb-rocksdb/src/lib.rs | 47 - kvdb-shared-tests/CHANGELOG.md | 3 + kvdb-shared-tests/Cargo.toml | 4 +- kvdb/CHANGELOG.md | 4 + kvdb/Cargo.toml | 2 +- parity-util-mem/CHANGELOG.md | 92 -- parity-util-mem/Cargo.toml | 56 -- parity-util-mem/README.md | 31 - parity-util-mem/build.rs | 1 - parity-util-mem/derive/CHANGELOG.md | 8 - parity-util-mem/derive/Cargo.toml | 18 - parity-util-mem/derive/lib.rs | 77 -- parity-util-mem/src/allocators.rs | 147 --- parity-util-mem/src/ethereum_impls.rs | 14 - parity-util-mem/src/lib.rs | 150 --- parity-util-mem/src/malloc_size.rs | 911 ------------------- parity-util-mem/src/memory_stats_jemalloc.rs | 32 - parity-util-mem/src/memory_stats_noop.rs | 31 - parity-util-mem/src/primitives_impls.rs | 26 - parity-util-mem/src/sizeof.rs | 51 -- parity-util-mem/tests/derive.rs | 85 -- primitive-types/Cargo.toml | 2 +- primitive-types/impls/num-traits/Cargo.toml | 2 +- primitive-types/impls/serde/Cargo.toml | 2 +- uint/CHANGELOG.md | 3 + uint/Cargo.toml | 2 +- 33 files changed, 33 insertions(+), 1794 deletions(-) delete mode 100644 parity-util-mem/CHANGELOG.md delete mode 100644 parity-util-mem/Cargo.toml delete mode 100644 parity-util-mem/README.md delete mode 100644 parity-util-mem/build.rs delete mode 100644 parity-util-mem/derive/CHANGELOG.md delete mode 100644 parity-util-mem/derive/Cargo.toml delete mode 100644 parity-util-mem/derive/lib.rs delete mode 100644 parity-util-mem/src/allocators.rs delete mode 100644 parity-util-mem/src/ethereum_impls.rs delete mode 100644 parity-util-mem/src/lib.rs delete mode 100644 parity-util-mem/src/malloc_size.rs delete mode 100644 parity-util-mem/src/memory_stats_jemalloc.rs delete mode 100644 parity-util-mem/src/memory_stats_noop.rs delete mode 100644 parity-util-mem/src/primitives_impls.rs delete mode 100644 parity-util-mem/src/sizeof.rs delete mode 100644 parity-util-mem/tests/derive.rs diff --git a/Cargo.toml b/Cargo.toml index fcd164017..60011793a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,9 +10,7 @@ members = [ "rlp", "rlp-derive", "uint", - "parity-util-mem", "primitive-types", "ethereum-types", "ethbloom", - "parity-util-mem/derive" ] diff --git a/ethereum-types/CHANGELOG.md b/ethereum-types/CHANGELOG.md index 43115efa2..1fc54e5e6 100644 --- a/ethereum-types/CHANGELOG.md +++ b/ethereum-types/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.14.1] - 2022-11-29 +- Added `if_ethbloom` conditional macro. [#682](https://github.com/paritytech/parity-common/pull/682) + ## [0.14.0] - 2022-09-20 - Updated `fixed-hash` to 0.8. [#680](https://github.com/paritytech/parity-common/pull/680) - Updated `primitive-types` to 0.12. [#680](https://github.com/paritytech/parity-common/pull/680) diff --git a/ethereum-types/Cargo.toml b/ethereum-types/Cargo.toml index 68c76036c..b5a5302ec 100644 --- a/ethereum-types/Cargo.toml +++ b/ethereum-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ethereum-types" -version = "0.14.0" +version = "0.14.1" authors = ["Parity Technologies "] license = "MIT OR Apache-2.0" homepage = "https://github.com/paritytech/parity-common" diff --git a/kvdb-memorydb/CHANGELOG.md b/kvdb-memorydb/CHANGELOG.md index f90495763..c8f3c12fe 100644 --- a/kvdb-memorydb/CHANGELOG.md +++ b/kvdb-memorydb/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.13.0] - 2022-11-29 +- Removed `parity-util-mem` support. [#696](https://github.com/paritytech/parity-common/pull/696) + ## [0.12.0] - 2022-09-20 ### Breaking - Updated `kvdb` to 0.12. [662](https://github.com/paritytech/parity-common/pull/662) diff --git a/kvdb-memorydb/Cargo.toml b/kvdb-memorydb/Cargo.toml index ca52d8a1b..3b82d5fda 100644 --- a/kvdb-memorydb/Cargo.toml +++ b/kvdb-memorydb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kvdb-memorydb" -version = "0.12.0" +version = "0.13.0" authors = ["Parity Technologies "] repository = "https://github.com/paritytech/parity-common" description = "A key-value in-memory database that implements the `KeyValueDB` trait" @@ -10,10 +10,10 @@ rust-version = "1.56.1" [dependencies] parking_lot = "0.12.0" -kvdb = { version = "0.12", path = "../kvdb" } +kvdb = { version = "0.13", path = "../kvdb" } [dev-dependencies] -kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.10" } +kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.11" } [features] default = [] diff --git a/kvdb-rocksdb/CHANGELOG.md b/kvdb-rocksdb/CHANGELOG.md index 6f289070a..32bbd3810 100644 --- a/kvdb-rocksdb/CHANGELOG.md +++ b/kvdb-rocksdb/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.17.0] - 2022-11-29 +- Removed `parity-util-mem` support. [#696](https://github.com/paritytech/parity-common/pull/696) + ## [0.16.0] - 2022-09-20 - Removed `owning_ref` from dependencies :tada:. [#662](https://github.com/paritytech/parity-common/pull/662) - No longer attempt to repair on `open`. [#667](https://github.com/paritytech/parity-common/pull/667) diff --git a/kvdb-rocksdb/Cargo.toml b/kvdb-rocksdb/Cargo.toml index a932b34ad..5e920fee5 100644 --- a/kvdb-rocksdb/Cargo.toml +++ b/kvdb-rocksdb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kvdb-rocksdb" -version = "0.16.0" +version = "0.17.0" authors = ["Parity Technologies "] repository = "https://github.com/paritytech/parity-common" description = "kvdb implementation backed by RocksDB" @@ -14,8 +14,7 @@ harness = false [dependencies] smallvec = "1.0.0" -kvdb = { path = "../kvdb", version = "0.12" } -log = "0.4.8" +kvdb = { path = "../kvdb", version = "0.13" } num_cpus = "1.10.1" parking_lot = "0.12.0" regex = "1.3.1" @@ -36,7 +35,7 @@ version = "0.19.0" alloc_counter = "0.0.4" criterion = "0.4" ethereum-types = { path = "../ethereum-types" } -kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.10" } +kvdb-shared-tests = { path = "../kvdb-shared-tests", version = "0.11" } rand = "0.8.0" tempfile = "3.1.0" keccak-hash = { path = "../keccak-hash" } diff --git a/kvdb-rocksdb/src/lib.rs b/kvdb-rocksdb/src/lib.rs index 487cd578b..5c231413d 100644 --- a/kvdb-rocksdb/src/lib.rs +++ b/kvdb-rocksdb/src/lib.rs @@ -21,7 +21,6 @@ use rocksdb::{ }; use kvdb::{DBKeyValue, DBOp, DBTransaction, DBValue, KeyValueDB}; -use log::warn; #[cfg(target_os = "linux")] use regex::Regex; @@ -258,30 +257,12 @@ impl DBAndColumns { .cf_handle(&name) .ok_or_else(|| other_io_err(format!("invalid column name: {name}"))) } - - fn static_property_or_warn(&self, col: usize, prop: &str) -> usize { - let cf = match self.cf(col) { - Ok(cf) => cf, - Err(_) => { - warn!("RocksDB column index out of range: {}", col); - return 0 - }, - }; - match self.db.property_int_value_cf(cf, prop) { - Ok(Some(v)) => v as usize, - _ => { - warn!("Cannot read expected static property of RocksDb database: {}", prop); - 0 - }, - } - } } /// Key-Value database. pub struct Database { inner: DBAndColumns, config: DatabaseConfig, - path: PathBuf, opts: Options, write_opts: WriteOptions, read_opts: ReadOptions, @@ -372,7 +353,6 @@ impl Database { Ok(Database { inner: DBAndColumns { db, column_names }, config: config.clone(), - path: path.as_ref().to_owned(), opts, read_opts, write_opts, @@ -757,33 +737,6 @@ mod tests { Ok(()) } - #[test] - fn mem_tables_size() { - let tempdir = TempfileBuilder::new().prefix("").tempdir().unwrap(); - - let config = DatabaseConfig { - max_open_files: 512, - memory_budget: HashMap::new(), - compaction: CompactionProfile::default(), - columns: 11, - keep_log_file_num: 1, - enable_statistics: false, - secondary: None, - max_total_wal_size: None, - create_if_missing: true, - }; - - let db = Database::open(&config, tempdir.path().to_str().unwrap()).unwrap(); - - let mut batch = db.transaction(); - for i in 0u32..10000u32 { - batch.put(i / 1000 + 1, &i.to_le_bytes(), &(i * 17).to_le_bytes()); - } - db.write(batch).unwrap(); - - assert!(db.inner.static_property_or_warn(0, "rocksdb.cur-size-all-mem-tables") > 512); - } - #[test] #[cfg(target_os = "linux")] fn df_to_rotational() { diff --git a/kvdb-shared-tests/CHANGELOG.md b/kvdb-shared-tests/CHANGELOG.md index 6085d10ad..767ffbcd9 100644 --- a/kvdb-shared-tests/CHANGELOG.md +++ b/kvdb-shared-tests/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.11.0] - 2022-11-29 +- Removed `parity-util-mem` support. [#696](https://github.com/paritytech/parity-common/pull/696) + ## [0.10.0] - 2022-09-20 ### Breaking - Migrated to 2021 edition, enforcing MSRV of `1.56.1`. [#601](https://github.com/paritytech/parity-common/pull/601) diff --git a/kvdb-shared-tests/Cargo.toml b/kvdb-shared-tests/Cargo.toml index 2766290f9..6da808795 100644 --- a/kvdb-shared-tests/Cargo.toml +++ b/kvdb-shared-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kvdb-shared-tests" -version = "0.10.0" +version = "0.11.0" authors = ["Parity Technologies "] edition = "2021" rust-version = "1.56.1" @@ -8,4 +8,4 @@ description = "Shared tests for kvdb functionality, to be executed against actua license = "MIT OR Apache-2.0" [dependencies] -kvdb = { path = "../kvdb", version = "0.12" } +kvdb = { path = "../kvdb", version = "0.13" } diff --git a/kvdb/CHANGELOG.md b/kvdb/CHANGELOG.md index d6e7b9c05..5e4305c6f 100644 --- a/kvdb/CHANGELOG.md +++ b/kvdb/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog]. ## [Unreleased] + +## [0.13.0] - 2022-11-29 +- Removed `parity-util-mem` support. [#696](https://github.com/paritytech/parity-common/pull/696) + ## [0.12.0] - 2022-09-20 ### Breaking - Removed `fn restore` from `KeyValueDB` trait. [#662](https://github.com/paritytech/parity-common/pull/662) diff --git a/kvdb/Cargo.toml b/kvdb/Cargo.toml index a926dffcb..c66b024b8 100644 --- a/kvdb/Cargo.toml +++ b/kvdb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kvdb" -version = "0.12.0" +version = "0.13.0" authors = ["Parity Technologies "] repository = "https://github.com/paritytech/parity-common" description = "Generic key-value trait" diff --git a/parity-util-mem/CHANGELOG.md b/parity-util-mem/CHANGELOG.md deleted file mode 100644 index f7b73a4ee..000000000 --- a/parity-util-mem/CHANGELOG.md +++ /dev/null @@ -1,92 +0,0 @@ -# Changelog - -The format is based on [Keep a Changelog]. - -[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ - -## [Unreleased] - -## [0.12.0] - 2022-09-20 -### Breaking -- Updated `tikv-jemallocator` to 0.5. [#661](https://github.com/paritytech/parity-common/pull/661) -- Updated `lru` to 0.8. [#675](https://github.com/paritytech/parity-common/pull/675) -- Removed `weealloc-global` feature. [#678](https://github.com/paritytech/parity-common/pull/678) -- Updated `primitive-types` to 0.12. [#680](https://github.com/paritytech/parity-common/pull/680) -- Updated `ethereum-types` to 0.14. [#680](https://github.com/paritytech/parity-common/pull/680) - -## [0.11.0] - 2022-02-04 -### Breaking -- Migrated to 2021 edition, enforcing MSRV of `1.56.1`. [#601](https://github.com/paritytech/parity-common/pull/601) -- Updated `primitive-types` to 0.11. [#623](https://github.com/paritytech/parity-common/pull/623) -- Updated `ethereum-types` to 0.13. [#623](https://github.com/paritytech/parity-common/pull/623) -- Updated `lru` to 0.7. [#595](https://github.com/paritytech/parity-common/pull/595) -- Updated `parking_lot` to 0.12. [#619](https://github.com/paritytech/parity-common/pull/619) -- Updated `hashbrown` to 0.12. [#612](https://github.com/paritytech/parity-common/pull/612) - -## [0.10.2] - 2021-09-20 -- Switched from `jemallocator` to `tikv-jemallocator`. [#589](https://github.com/paritytech/parity-common/pull/589) - -## [0.10.1] - 2021-09-15 -- Added support for memory stats gathering, ported over from `polkadot`. [#588](https://github.com/paritytech/parity-common/pull/588) - -## [0.10.0] - 2021-07-02 -- Fixed `malloc_usable_size` for FreeBSD. [#553](https://github.com/paritytech/parity-common/pull/553) - -### Breaking -- Updated `ethereum-types` to 0.12. [#556](https://github.com/paritytech/parity-common/pull/556) -- Updated `primitive-types` to 0.10. [#556](https://github.com/paritytech/parity-common/pull/556) -- Updated `hashbrown` to 0.11. [#533](https://github.com/paritytech/parity-common/pull/533) - -## [0.9.0] - 2021-01-27 -### Breaking -- Updated `ethereum-types` to 0.11. [#510](https://github.com/paritytech/parity-common/pull/510) -- Updated `primitive-types` to 0.9. [#510](https://github.com/paritytech/parity-common/pull/510) - -## [0.8.0] - 2021-01-05 -- Updated dlmalloc to 0.2.1. [#452](https://github.com/paritytech/parity-common/pull/452) -### Breaking -- Updated `ethereum-types` to 0.10. [#463](https://github.com/paritytech/parity-common/pull/463) -- Updated `parking_lot` to 0.11.1. [#470](https://github.com/paritytech/parity-common/pull/470) - -## [0.7.0] - 2020-06-24 -- Added `const_size` to `MallocSizeOf` to optimize it for flat collections. [#398](https://github.com/paritytech/parity-common/pull/398) -- Exported `MallocShallowSizeOf`. [#399](https://github.com/paritytech/parity-common/pull/399) -- Updated dependencies. - -## [0.6.1] - 2020-04-15 -- Fix compilation on Windows for no-std. [#375](https://github.com/paritytech/parity-common/pull/375) -- Prevent multiple versions from being linked into the same program. [#363](https://github.com/paritytech/parity-common/pull/363) - -## [0.6.0] - 2020-03-13 -- Updated dependencies. [#361](https://github.com/paritytech/parity-common/pull/361) - -## [0.5.2] - 2020-03-13 -- License changed from GPL3 to dual MIT/Apache2. [#342](https://github.com/paritytech/parity-common/pull/342) -- Updated mimalloc dependency. [#352](https://github.com/paritytech/parity-common/pull/352) -- Use malloc for `usable_size` on Android. [#355](https://github.com/paritytech/parity-common/pull/355) - -## [0.5.1] - 2019-02-05 -- Add different mode for malloc_size_of_is_0 macro dealing with generics. [#334](https://github.com/paritytech/parity-common/pull/334) - -## [0.5.0] - 2019-02-05 -- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332) - -## [0.4.2] - 2020-02-04 -- Implementation of `MallocSizeOf` for `BTreeSet`. [#325](https://github.com/paritytech/parity-common/pull/325) -- Split off implementation of `MallocSizeOf` for `primitive-types`. [#323](https://github.com/paritytech/parity-common/pull/323) - -## [0.4.1] - 2020-01-06 -- Implementation of `MallocSizeOf` for SmallVec no longer requires ethereum `ethereum-impls` feature. [#307](https://github.com/paritytech/parity-common/pull/307) - -## [0.4.0] - 2020-01-01 -- Added implementation of `MallocSizeOf` for non-std `hashbrown::HashMap` and `lru::LRUMap`. [#293](https://github.com/paritytech/parity-common/pull/293) -- Introduced our own version of `#[derive(MallocSizeOf)]` [#291](https://github.com/paritytech/parity-common/pull/291) -- Added implementation of `MallocSizeOf` for `parking_lot` locking primitives. [#290](https://github.com/paritytech/parity-common/pull/290) -- Added default implementation of `MallocSizeOf` for tuples up to 12. [#300](https://github.com/paritytech/parity-common/pull/300) - -## [0.3.0] - 2019-12-19 -- Remove `MallocSizeOf` impls for `ElasticArray` and implement it for `SmallVec` (32 and 36). [#282](https://github.com/paritytech/parity-common/pull/282) - -## [0.2.1] - 2019-10-24 -### Dependencies -- Updated dependencies. [#239](https://github.com/paritytech/parity-common/pull/239) diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml deleted file mode 100644 index 4e9e8ccfa..000000000 --- a/parity-util-mem/Cargo.toml +++ /dev/null @@ -1,56 +0,0 @@ -[package] -name = "parity-util-mem" -version = "0.12.0" -authors = ["Parity Technologies "] -repository = "https://github.com/paritytech/parity-common" -description = "Collection of memory related utilities" -license = "MIT OR Apache-2.0" -edition = "2021" -rust-version = "1.56.1" - -# Prevent multiple versions from being linked into the same program. -links = "parity-util-mem-ban-duplicates" -# `links` requires a build script to be present: -# https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key -# so we use an empty build script -build = "build.rs" - -[dependencies] -cfg-if = "1.0.0" -dlmalloc = { version = "0.2.1", features = ["global"], optional = true } -lru = { version = "0.8", optional = true } -hashbrown = { version = "0.12", optional = true } -mimalloc = { version = "0.1.18", optional = true } -libmimalloc-sys = { version = "0.1.14", optional = true } -parity-util-mem-derive = { path = "derive", version = "0.1" } -impl-trait-for-tuples = "0.2.0" - -smallvec = { version = "1.0.0", optional = true } -ethereum-types = { version = "0.14.0", optional = true, path = "../ethereum-types" } -parking_lot = { version = "0.12.0", optional = true } -primitive-types = { version = "0.12", path = "../primitive-types", default-features = false, optional = true } - -[target.'cfg(target_os = "windows")'.dependencies] -winapi = { version = "0.3.8", features = ["heapapi"] } - -[target.'cfg(not(target_os = "windows"))'.dependencies.tikv-jemallocator] -version = "0.5.0" -optional = true - -[target.'cfg(not(target_os = "windows"))'.dependencies.tikv-jemalloc-ctl] -version = "0.5.0" -optional = true - -[features] -default = ["std", "ethereum-impls", "lru", "hashbrown", "smallvec", "primitive-types"] -std = ["parking_lot"] -# use dlmalloc as global allocator -dlmalloc-global = ["dlmalloc", "estimate-heapsize"] -# use jemalloc as global allocator -jemalloc-global = ["tikv-jemallocator", "tikv-jemalloc-ctl"] -# use mimalloc as global allocator -mimalloc-global = ["mimalloc", "libmimalloc-sys"] -# implement additional types -ethereum-impls = ["ethereum-types", "primitive-types"] -# Full estimate: no call to allocator -estimate-heapsize = [] diff --git a/parity-util-mem/README.md b/parity-util-mem/README.md deleted file mode 100644 index 8b7dd7bd1..000000000 --- a/parity-util-mem/README.md +++ /dev/null @@ -1,31 +0,0 @@ -# parity-util-mem - -Collection of memory related utilities. - -## WARNING - -When `parity-util-mem` is used as a dependency with any of the global allocator features enabled, -it must be the sole place where a global allocator is defined. -The only exception to this rule is when used in a `no_std` context or when the `estimate-heapsize` feature is used. - -Because of that, it must be present in the dependency tree with a single version. -Starting from version 0.6.1, having duplicate versions of `parity-util-mem` will lead -to a compile-time error. It still will be possible to have 0.5 and 0.6.1 versions in the same binary though. - -Unless heeded you risk UB; see discussion in [issue 364]. - -[issue 364]: https://github.com/paritytech/parity-common/issues/364 - -## Features - -- estimate-heapsize : Do not use allocator, but `size_of` or `size_of_val`. - -Others features define global allocator, see `src/alloc.rs`. - -## Dependency - -This crate groups common dependency, a patched copy of unpublished [`malloc_size_of`](https://github.com/servo/servo/tree/master/components/malloc_size_of) from servo project is copied and partially reexported. - -`Malloc_size_of` code is used internally as a module with a few modification to be able to implement type locally. - -For existing code using deprecated `HeapsizeOf` crate, calls to `heapsize_of_children` should be replace by calls to `size_of`. diff --git a/parity-util-mem/build.rs b/parity-util-mem/build.rs deleted file mode 100644 index f328e4d9d..000000000 --- a/parity-util-mem/build.rs +++ /dev/null @@ -1 +0,0 @@ -fn main() {} diff --git a/parity-util-mem/derive/CHANGELOG.md b/parity-util-mem/derive/CHANGELOG.md deleted file mode 100644 index c9a41d07a..000000000 --- a/parity-util-mem/derive/CHANGELOG.md +++ /dev/null @@ -1,8 +0,0 @@ -# Changelog - -The format is based on [Keep a Changelog]. - -[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ - -## [Unreleased] -- Migrated to 2021 edition, enforcing MSRV of `1.56.1`. [#601](https://github.com/paritytech/parity-common/pull/601) diff --git a/parity-util-mem/derive/Cargo.toml b/parity-util-mem/derive/Cargo.toml deleted file mode 100644 index 02d6a9d71..000000000 --- a/parity-util-mem/derive/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "parity-util-mem-derive" -version = "0.1.0" -authors = ["Parity Technologies "] -license = "MIT OR Apache-2.0" -description = "Crate for memory reporting" -repository = "https://github.com/paritytech/pariry-common/parity-util-mem/derive" -edition = "2021" -rust-version = "1.56.1" - -[lib] -path = "lib.rs" -proc-macro = true - -[dependencies] -proc-macro2 = "1" -syn = { version = "1", features = ["full"] } -synstructure = "0.12" diff --git a/parity-util-mem/derive/lib.rs b/parity-util-mem/derive/lib.rs deleted file mode 100644 index 78e718635..000000000 --- a/parity-util-mem/derive/lib.rs +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A crate for deriving the MallocSizeOf trait. -//! -//! This is a copy of Servo malloc_size_of_derive code, modified to work with -//! our `parity_util_mem` library - -extern crate proc_macro2; -#[macro_use] -extern crate syn; -#[macro_use] -extern crate synstructure; - -decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of)] => malloc_size_of_derive); - -fn malloc_size_of_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { - let match_body = s.each(|binding| { - let ignore = binding.ast().attrs.iter().any(|attr| match attr.parse_meta().unwrap() { - syn::Meta::Path(ref path) | syn::Meta::List(syn::MetaList { ref path, .. }) - if path.is_ident("ignore_malloc_size_of") => - { - panic!( - "#[ignore_malloc_size_of] should have an explanation, \ - e.g. #[ignore_malloc_size_of = \"because reasons\"]" - ); - }, - syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) if path.is_ident("ignore_malloc_size_of") => true, - _ => false, - }); - if ignore { - None - } else if let syn::Type::Array(..) = binding.ast().ty { - Some(quote! { - for item in #binding.iter() { - sum += parity_util_mem::MallocSizeOf::size_of(item, ops); - } - }) - } else { - Some(quote! { - sum += parity_util_mem::MallocSizeOf::size_of(#binding, ops); - }) - } - }); - - let ast = s.ast(); - let name = &ast.ident; - let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); - let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone(); - for param in ast.generics.type_params() { - let ident = ¶m.ident; - where_clause - .predicates - .push(parse_quote!(#ident: parity_util_mem::MallocSizeOf)); - } - - let tokens = quote! { - impl #impl_generics parity_util_mem::MallocSizeOf for #name #ty_generics #where_clause { - #[inline] - #[allow(unused_variables, unused_mut, unreachable_code)] - fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize { - let mut sum = 0; - match *self { - #match_body - } - sum - } - } - }; - - tokens -} diff --git a/parity-util-mem/src/allocators.rs b/parity-util-mem/src/allocators.rs deleted file mode 100644 index 576e93314..000000000 --- a/parity-util-mem/src/allocators.rs +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! default allocator management -//! Features are: -//! - windows: -//! - no features: default implementation from servo `heapsize` crate -//! - dlmalloc: default to `estimate_size` -//! - jemalloc: default windows allocator is used instead -//! - mimalloc: use mimallocator crate -//! - arch x86: -//! - no features: use default alloc -//! - jemalloc: use tikv-jemallocator crate -//! - dlmalloc: default to `estimate_size` -//! - mimalloc: use mimallocator crate -//! - arch x86/macos: -//! - no features: use default alloc, requires using `estimate_size` -//! - jemalloc: use tikv-jemallocator crate -//! - dlmalloc: default to `estimate_size` -//! - mimalloc: use mimallocator crate -//! - arch wasm32: -//! - no features: default to `estimate_size` -//! - dlmalloc: default to `estimate_size` -//! - jemalloc: compile error -//! - mimalloc: compile error (until https://github.com/microsoft/mimalloc/pull/32 is merged) - -#[cfg(feature = "std")] -use crate::malloc_size::MallocUnconditionalSizeOf; -use crate::malloc_size::{MallocSizeOf, MallocSizeOfOps, VoidPtrToSizeFn}; -#[cfg(not(feature = "std"))] -use core::ffi::c_void; -#[cfg(feature = "std")] -use std::os::raw::c_void; - -mod usable_size { - - use super::*; - - cfg_if::cfg_if! { - - if #[cfg(any( - target_arch = "wasm32", - feature = "estimate-heapsize", - feature = "dlmalloc-global", - ))] { - - // do not try system allocator - - /// Warning this is for compatibility only. - /// This function does panic: `estimate-heapsize` feature needs to be activated - /// to avoid this function call. - pub unsafe extern "C" fn malloc_usable_size(_ptr: *const c_void) -> usize { - unreachable!("estimate heapsize only") - } - - } else if #[cfg(target_os = "windows")] { - - use winapi::um::heapapi::{GetProcessHeap, HeapSize, HeapValidate}; - use winapi::ctypes::c_void as winapi_c_void; - - /// Get the size of a heap block. - /// Call windows allocator through `winapi` crate - pub unsafe extern "C" fn malloc_usable_size(mut ptr: *const c_void) -> usize { - - let heap = GetProcessHeap(); - - if HeapValidate(heap, 0, ptr as *const winapi_c_void) == 0 { - ptr = *(ptr as *const *const c_void).offset(-1); - } - - HeapSize(heap, 0, ptr as *const winapi_c_void) as usize - } - - } else if #[cfg(feature = "jemalloc-global")] { - - /// Use of jemalloc usable size C function through jemallocator crate call. - pub unsafe extern "C" fn malloc_usable_size(ptr: *const c_void) -> usize { - tikv_jemallocator::usable_size(ptr) - } - - } else if #[cfg(feature = "mimalloc-global")] { - - /// Use of mimalloc usable size C function through mimalloc_sys crate call. - pub unsafe extern "C" fn malloc_usable_size(ptr: *const c_void) -> usize { - // mimalloc doesn't actually mutate the value ptr points to, - // but requires a mut pointer in the API - libmimalloc_sys::mi_usable_size(ptr as *mut _) - } - - } else if #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "freebsd", - ))] { - // Linux/BSD call system allocator (currently malloc). - extern "C" { - pub fn malloc_usable_size(ptr: *const c_void) -> usize; - } - - } else { - // default allocator for non linux or windows system use estimate - pub unsafe extern "C" fn malloc_usable_size(_ptr: *const c_void) -> usize { - unreachable!("estimate heapsize or feature allocator needed") - } - - } - - } - - /// No enclosing function defined. - #[inline] - pub fn new_enclosing_size_fn() -> Option { - None - } -} - -/// Get a new instance of a MallocSizeOfOps -pub fn new_malloc_size_ops() -> MallocSizeOfOps { - MallocSizeOfOps::new(usable_size::malloc_usable_size, usable_size::new_enclosing_size_fn(), None) -} - -/// Extension methods for `MallocSizeOf` trait, do not implement -/// directly. -/// It allows getting heapsize without exposing `MallocSizeOfOps` -/// (a single default `MallocSizeOfOps` is used for each call). -pub trait MallocSizeOfExt: MallocSizeOf { - /// Method to launch a heapsize measurement with a - /// fresh state. - fn malloc_size_of(&self) -> usize { - let mut ops = new_malloc_size_ops(); - ::size_of(self, &mut ops) - } -} - -impl MallocSizeOfExt for T {} - -#[cfg(feature = "std")] -impl MallocSizeOf for std::sync::Arc { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.unconditional_size_of(ops) - } -} diff --git a/parity-util-mem/src/ethereum_impls.rs b/parity-util-mem/src/ethereum_impls.rs deleted file mode 100644 index c296d2d40..000000000 --- a/parity-util-mem/src/ethereum_impls.rs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Implementation of `MallocSize` for common ethereum types: fixed hashes -//! and uints. - -use ethereum_types::{Bloom, H128, H264, H32, H520, H64, U64}; - -malloc_size_of_is_0!(U64, H32, H64, H128, H264, H520, Bloom); diff --git a/parity-util-mem/src/lib.rs b/parity-util-mem/src/lib.rs deleted file mode 100644 index 4531c63f9..000000000 --- a/parity-util-mem/src/lib.rs +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Crate for parity memory management related utilities. -//! It includes global allocator choice, heap measurement and -//! memory erasure. - -#![cfg_attr(not(feature = "std"), no_std)] - -#[cfg(not(feature = "std"))] -extern crate alloc; - -cfg_if::cfg_if! { - if #[cfg(all( - feature = "jemalloc-global", - not(target_os = "windows"), - not(target_arch = "wasm32") - ))] { - /// Global allocator - #[global_allocator] - pub static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; - - mod memory_stats_jemalloc; - use memory_stats_jemalloc as memory_stats; - } else if #[cfg(feature = "dlmalloc-global")] { - /// Global allocator - #[global_allocator] - pub static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc; - - mod memory_stats_noop; - use memory_stats_noop as memory_stats; - } else if #[cfg(all( - feature = "mimalloc-global", - not(target_arch = "wasm32") - ))] { - /// Global allocator - #[global_allocator] - pub static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; - - mod memory_stats_noop; - use memory_stats_noop as memory_stats; - } else { - // default allocator used - mod memory_stats_noop; - use memory_stats_noop as memory_stats; - } -} - -pub mod allocators; - -#[cfg(any( - all(any(target_os = "macos", target_os = "ios"), not(feature = "jemalloc-global"),), - feature = "estimate-heapsize" -))] -pub mod sizeof; - -/// This is a copy of patched crate `malloc_size_of` as a module. -/// We need to have it as an inner module to be able to define our own traits implementation, -/// if at some point the trait become standard enough we could use the right way of doing it -/// by implementing it in our type traits crates. At this time moving this trait to the primitive -/// types level would impact too much of the dependencies to be easily manageable. -#[macro_use] -mod malloc_size; - -#[cfg(feature = "ethereum-impls")] -pub mod ethereum_impls; - -#[cfg(feature = "primitive-types")] -pub mod primitives_impls; - -pub use allocators::MallocSizeOfExt; -pub use malloc_size::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; - -pub use parity_util_mem_derive::*; - -/// Heap size of structure. -/// -/// Structure can be anything that implements MallocSizeOf. -pub fn malloc_size(t: &T) -> usize { - MallocSizeOf::size_of(t, &mut allocators::new_malloc_size_ops()) -} - -/// An error related to the memory stats gathering. -#[derive(Clone, Debug)] -pub struct MemoryStatsError(memory_stats::Error); - -#[cfg(feature = "std")] -impl std::fmt::Display for MemoryStatsError { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { - self.0.fmt(fmt) - } -} - -#[cfg(feature = "std")] -impl std::error::Error for MemoryStatsError {} - -/// Snapshot of collected memory metrics. -#[non_exhaustive] -#[derive(Debug, Clone)] -pub struct MemoryAllocationSnapshot { - /// Total resident memory, in bytes. - pub resident: u64, - /// Total allocated memory, in bytes. - pub allocated: u64, -} - -/// Accessor to the allocator internals. -#[derive(Clone)] -pub struct MemoryAllocationTracker(self::memory_stats::MemoryAllocationTracker); - -impl MemoryAllocationTracker { - /// Create an instance of an allocation tracker. - pub fn new() -> Result { - self::memory_stats::MemoryAllocationTracker::new() - .map(MemoryAllocationTracker) - .map_err(MemoryStatsError) - } - - /// Create an allocation snapshot. - pub fn snapshot(&self) -> Result { - self.0.snapshot().map_err(MemoryStatsError) - } -} - -#[cfg(feature = "std")] -#[cfg(test)] -mod test { - use super::{malloc_size, MallocSizeOf, MallocSizeOfExt}; - use std::sync::Arc; - - #[test] - fn test_arc() { - let val = Arc::new("test".to_string()); - let s = val.malloc_size_of(); - assert!(s > 0); - } - - #[test] - fn test_dyn() { - trait Augmented: MallocSizeOf {} - impl Augmented for Vec {} - let val: Arc = Arc::new(vec![0u8; 1024]); - assert!(malloc_size(&*val) > 1000); - } -} diff --git a/parity-util-mem/src/malloc_size.rs b/parity-util-mem/src/malloc_size.rs deleted file mode 100644 index 907726674..000000000 --- a/parity-util-mem/src/malloc_size.rs +++ /dev/null @@ -1,911 +0,0 @@ -// Copyright 2016-2017 The Servo Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A crate for measuring the heap usage of data structures in a way that -//! integrates with Firefox's memory reporting, particularly the use of -//! mozjemalloc and DMD. In particular, it has the following features. -//! - It isn't bound to a particular heap allocator. -//! - It provides traits for both "shallow" and "deep" measurement, which gives -//! flexibility in the cases where the traits can't be used. -//! - It allows for measuring blocks even when only an interior pointer can be -//! obtained for heap allocations, e.g. `HashSet` and `HashMap`. (This relies -//! on the heap allocator having suitable support, which mozjemalloc has.) -//! - It allows handling of types like `Rc` and `Arc` by providing traits that -//! are different to the ones for non-graph structures. -//! -//! Suggested uses are as follows. -//! - When possible, use the `MallocSizeOf` trait. (Deriving support is -//! provided by the `malloc_size_of_derive` crate.) -//! - If you need an additional synchronization argument, provide a function -//! that is like the standard trait method, but with the extra argument. -//! - If you need multiple measurements for a type, provide a function named -//! `add_size_of` that takes a mutable reference to a struct that contains -//! the multiple measurement fields. -//! - When deep measurement (via `MallocSizeOf`) cannot be implemented for a -//! type, shallow measurement (via `MallocShallowSizeOf`) in combination with -//! iteration can be a useful substitute. -//! - `Rc` and `Arc` are always tricky, which is why `MallocSizeOf` is not (and -//! should not be) implemented for them. -//! - If an `Rc` or `Arc` is known to be a "primary" reference and can always -//! be measured, it should be measured via the `MallocUnconditionalSizeOf` -//! trait. -//! - If an `Rc` or `Arc` should be measured only if it hasn't been seen -//! before, it should be measured via the `MallocConditionalSizeOf` trait. -//! - Using universal function call syntax is a good idea when measuring boxed -//! fields in structs, because it makes it clear that the Box is being -//! measured as well as the thing it points to. E.g. -//! ` as MallocSizeOf>::size_of(field, ops)`. - -//! This is an extended version of the Servo internal malloc_size crate. -//! We should occasionally track the upstream changes/fixes and reintroduce them here, whenever applicable. - -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; -#[cfg(feature = "std")] -mod rstd { - pub use std::*; -} -#[cfg(not(feature = "std"))] -mod rstd { - pub use core::*; - pub mod collections { - pub use alloc::collections::*; - pub use vec_deque::VecDeque; - } -} - -#[cfg(feature = "std")] -use std::sync::Arc; - -#[cfg(not(feature = "std"))] -pub use alloc::boxed::Box; -#[cfg(not(feature = "std"))] -use core::ffi::c_void; -#[cfg(feature = "std")] -use rstd::hash::Hash; -use rstd::{ - marker::PhantomData, - mem::size_of, - ops::{Deref, DerefMut, Range}, -}; -#[cfg(feature = "std")] -use std::hash::BuildHasher; -#[cfg(feature = "std")] -use std::os::raw::c_void; - -/// A C function that takes a pointer to a heap allocation and returns its size. -pub type VoidPtrToSizeFn = unsafe extern "C" fn(ptr: *const c_void) -> usize; - -/// A closure implementing a stateful predicate on pointers. -pub type VoidPtrToBoolFnMut = dyn FnMut(*const c_void) -> bool; - -/// Operations used when measuring heap usage of data structures. -pub struct MallocSizeOfOps { - /// A function that returns the size of a heap allocation. - size_of_op: VoidPtrToSizeFn, - - /// Like `size_of_op`, but can take an interior pointer. Optional because - /// not all allocators support this operation. If it's not provided, some - /// memory measurements will actually be computed estimates rather than - /// real and accurate measurements. - enclosing_size_of_op: Option, - - /// Check if a pointer has been seen before, and remember it for next time. - /// Useful when measuring `Rc`s and `Arc`s. Optional, because many places - /// don't need it. - have_seen_ptr_op: Option>, -} - -impl MallocSizeOfOps { - pub fn new( - size_of: VoidPtrToSizeFn, - malloc_enclosing_size_of: Option, - have_seen_ptr: Option>, - ) -> Self { - MallocSizeOfOps { - size_of_op: size_of, - enclosing_size_of_op: malloc_enclosing_size_of, - have_seen_ptr_op: have_seen_ptr, - } - } - - /// Check if an allocation is empty. This relies on knowledge of how Rust - /// handles empty allocations, which may change in the future. - fn is_empty(ptr: *const T) -> bool { - // The correct condition is this: - // `ptr as usize <= ::std::mem::align_of::()` - // But we can't call align_of() on a ?Sized T. So we approximate it - // with the following. 256 is large enough that it should always be - // larger than the required alignment, but small enough that it is - // always in the first page of memory and therefore not a legitimate - // address. - return ptr as *const usize as usize <= 256 - } - - /// Call `size_of_op` on `ptr`, first checking that the allocation isn't - /// empty, because some types (such as `Vec`) utilize empty allocations. - pub unsafe fn malloc_size_of(&self, ptr: *const T) -> usize { - if MallocSizeOfOps::is_empty(ptr) { - 0 - } else { - (self.size_of_op)(ptr as *const c_void) - } - } - - /// Is an `enclosing_size_of_op` available? - pub fn has_malloc_enclosing_size_of(&self) -> bool { - self.enclosing_size_of_op.is_some() - } - - /// Call `enclosing_size_of_op`, which must be available, on `ptr`, which - /// must not be empty. - pub unsafe fn malloc_enclosing_size_of(&self, ptr: *const T) -> usize { - assert!(!MallocSizeOfOps::is_empty(ptr)); - (self.enclosing_size_of_op.unwrap())(ptr as *const c_void) - } - - /// Call `have_seen_ptr_op` on `ptr`. - pub fn have_seen_ptr(&mut self, ptr: *const T) -> bool { - let have_seen_ptr_op = self.have_seen_ptr_op.as_mut().expect("missing have_seen_ptr_op"); - have_seen_ptr_op(ptr as *const c_void) - } -} - -/// Trait for measuring the "deep" heap usage of a data structure. This is the -/// most commonly-used of the traits. -pub trait MallocSizeOf { - /// Measure the heap usage of all descendant heap-allocated structures, but - /// not the space taken up by the value itself. - /// If `T::size_of` is a constant, consider implementing `constant_size` as well. - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize; - - /// Used to optimize `MallocSizeOf` implementation for collections - /// like `Vec` and `HashMap` to avoid iterating over them unnecessarily. - /// The `Self: Sized` bound is for object safety. - fn constant_size() -> Option - where - Self: Sized, - { - None - } -} - -/// Trait for measuring the "shallow" heap usage of a container. -pub trait MallocShallowSizeOf { - /// Measure the heap usage of immediate heap-allocated descendant - /// structures, but not the space taken up by the value itself. Anything - /// beyond the immediate descendants must be measured separately, using - /// iteration. - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize; -} - -/// Like `MallocSizeOf`, but with a different name so it cannot be used -/// accidentally with derive(MallocSizeOf). For use with types like `Rc` and -/// `Arc` when appropriate (e.g. when measuring a "primary" reference). -pub trait MallocUnconditionalSizeOf { - /// Measure the heap usage of all heap-allocated descendant structures, but - /// not the space taken up by the value itself. - fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize; -} - -/// `MallocUnconditionalSizeOf` combined with `MallocShallowSizeOf`. -pub trait MallocUnconditionalShallowSizeOf { - /// `unconditional_size_of` combined with `shallow_size_of`. - fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize; -} - -/// Like `MallocSizeOf`, but only measures if the value hasn't already been -/// measured. For use with types like `Rc` and `Arc` when appropriate (e.g. -/// when there is no "primary" reference). -pub trait MallocConditionalSizeOf { - /// Measure the heap usage of all heap-allocated descendant structures, but - /// not the space taken up by the value itself, and only if that heap usage - /// hasn't already been measured. - fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize; -} - -/// `MallocConditionalSizeOf` combined with `MallocShallowSizeOf`. -pub trait MallocConditionalShallowSizeOf { - /// `conditional_size_of` combined with `shallow_size_of`. - fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize; -} - -#[cfg(not(any( - all(any(target_os = "macos", target_os = "ios"), not(feature = "jemalloc-global"),), - feature = "estimate-heapsize" -)))] -pub mod inner_allocator_use { - - use super::*; - - #[cfg(not(feature = "std"))] - use alloc::string::String; - - impl MallocShallowSizeOf for Box { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - unsafe { ops.malloc_size_of(&**self) } - } - } - - impl MallocShallowSizeOf for Vec { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - unsafe { ops.malloc_size_of(self.as_ptr()) } - } - } - - // currently this seems only fine with jemalloc - #[cfg(feature = "std")] - #[cfg(all(feature = "jemalloc-global", not(target_os = "windows")))] - impl MallocUnconditionalShallowSizeOf for Arc { - fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - unsafe { ops.malloc_size_of(arc_ptr(self)) } - } - } - - #[cfg(feature = "std")] - #[cfg(not(all(feature = "jemalloc-global", not(target_os = "windows"))))] - impl MallocUnconditionalShallowSizeOf for Arc { - fn unconditional_shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - size_of::() - } - } - - impl MallocSizeOf for String { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - unsafe { ops.malloc_size_of(self.as_ptr()) } - } - } -} - -impl<'a, T: ?Sized> MallocSizeOf for &'a T { - fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - // Zero makes sense for a non-owning reference. - 0 - } - fn constant_size() -> Option { - Some(0) - } -} - -impl MallocSizeOf for Box { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.shallow_size_of(ops) + (**self).size_of(ops) - } -} - -#[impl_trait_for_tuples::impl_for_tuples(12)] -impl MallocSizeOf for Tuple { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut result = 0; - for_tuples!( #( result += Tuple.size_of(ops); )* ); - result - } - fn constant_size() -> Option { - let mut result = Some(0); - for_tuples!( #( result = result.and_then(|s| Tuple::constant_size().map(|t| s + t)); )* ); - result - } -} - -impl MallocSizeOf for Option { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if let Some(val) = self.as_ref() { - val.size_of(ops) - } else { - 0 - } - } - fn constant_size() -> Option { - T::constant_size().filter(|s| *s == 0) - } -} - -impl MallocSizeOf for Result { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - match *self { - Ok(ref x) => x.size_of(ops), - Err(ref e) => e.size_of(ops), - } - } - fn constant_size() -> Option { - // Result has constant size iff T::constant_size == E::constant_size - T::constant_size().and_then(|t| E::constant_size().filter(|e| *e == t)) - } -} - -impl MallocSizeOf for rstd::cell::Cell { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.get().size_of(ops) - } - fn constant_size() -> Option { - T::constant_size() - } -} - -impl MallocSizeOf for rstd::cell::RefCell { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.borrow().size_of(ops) - } - fn constant_size() -> Option { - T::constant_size() - } -} - -#[cfg(feature = "std")] -impl<'a, B: ?Sized + ToOwned> MallocSizeOf for std::borrow::Cow<'a, B> -where - B::Owned: MallocSizeOf, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - match *self { - std::borrow::Cow::Borrowed(_) => 0, - std::borrow::Cow::Owned(ref b) => b.size_of(ops), - } - } -} - -impl MallocSizeOf for [T] { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = 0; - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } -} - -impl MallocSizeOf for Vec { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } -} - -impl MallocShallowSizeOf for rstd::collections::VecDeque { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.has_malloc_enclosing_size_of() { - if let Some(front) = self.front() { - // The front element is an interior pointer. - unsafe { ops.malloc_enclosing_size_of(&*front) } - } else { - // This assumes that no memory is allocated when the VecDeque is empty. - 0 - } - } else { - // An estimate. - self.capacity() * size_of::() - } - } -} - -impl MallocSizeOf for rstd::collections::VecDeque { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } -} - -#[cfg(feature = "std")] -impl MallocShallowSizeOf for std::collections::HashSet -where - T: Eq + Hash, - S: BuildHasher, -{ - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.has_malloc_enclosing_size_of() { - // The first value from the iterator gives us an interior pointer. - // `ops.malloc_enclosing_size_of()` then gives us the storage size. - // This assumes that the `HashSet`'s contents (values and hashes) - // are all stored in a single contiguous heap allocation. - self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) - } else { - // An estimate. - self.capacity() * (size_of::() + size_of::()) - } - } -} - -#[cfg(feature = "std")] -impl MallocSizeOf for std::collections::HashSet -where - T: Eq + Hash + MallocSizeOf, - S: BuildHasher, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } -} - -impl MallocSizeOf for rstd::cmp::Reverse { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.0.size_of(ops) - } - fn constant_size() -> Option { - I::constant_size() - } -} - -#[cfg(feature = "std")] -impl MallocShallowSizeOf for std::collections::HashMap { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - // See the implementation for std::collections::HashSet for details. - if ops.has_malloc_enclosing_size_of() { - self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) - } else { - self.capacity() * (size_of::() + size_of::() + size_of::()) - } - } -} - -#[cfg(feature = "std")] -impl MallocSizeOf for std::collections::HashMap -where - K: MallocSizeOf, - V: MallocSizeOf, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let (Some(k), Some(v)) = (K::constant_size(), V::constant_size()) { - n += self.len() * (k + v) - } else { - n = self.iter().fold(n, |acc, (k, v)| acc + k.size_of(ops) + v.size_of(ops)) - } - n - } -} - -impl MallocShallowSizeOf for rstd::collections::BTreeMap { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.has_malloc_enclosing_size_of() { - self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) - } else { - self.len() * (size_of::() + size_of::() + size_of::()) - } - } -} - -impl MallocSizeOf for rstd::collections::BTreeMap -where - K: MallocSizeOf, - V: MallocSizeOf, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let (Some(k), Some(v)) = (K::constant_size(), V::constant_size()) { - n += self.len() * (k + v) - } else { - n = self.iter().fold(n, |acc, (k, v)| acc + k.size_of(ops) + v.size_of(ops)) - } - n - } -} - -impl MallocShallowSizeOf for rstd::collections::BTreeSet { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.has_malloc_enclosing_size_of() { - // See implementation for HashSet how this works. - self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) - } else { - // An estimate. - self.len() * (size_of::() + size_of::()) - } - } -} - -impl MallocSizeOf for rstd::collections::BTreeSet -where - T: MallocSizeOf, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } -} - -// XXX: we don't want MallocSizeOf to be defined for Rc and Arc. If negative -// trait bounds are ever allowed, this code should be uncommented. -// (We do have a compile-fail test for this: -// rc_arc_must_not_derive_malloc_size_of.rs) -// impl !MallocSizeOf for Arc { } -// impl !MallocShallowSizeOf for Arc { } - -#[cfg(feature = "std")] -fn arc_ptr(s: &Arc) -> *const T { - &(**s) as *const T -} - -#[cfg(feature = "std")] -impl MallocUnconditionalSizeOf for Arc { - fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.unconditional_shallow_size_of(ops) + (**self).size_of(ops) - } -} - -#[cfg(feature = "std")] -impl MallocConditionalShallowSizeOf for Arc { - fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.have_seen_ptr(arc_ptr(self)) { - 0 - } else { - self.unconditional_shallow_size_of(ops) - } - } -} - -#[cfg(feature = "std")] -impl MallocConditionalSizeOf for Arc { - fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if ops.have_seen_ptr(arc_ptr(self)) { - 0 - } else { - self.unconditional_size_of(ops) - } - } -} - -/// If a mutex is stored directly as a member of a data type that is being measured, -/// it is the unique owner of its contents and deserves to be measured. -/// -/// If a mutex is stored inside of an Arc value as a member of a data type that is being measured, -/// the Arc will not be automatically measured so there is no risk of overcounting the mutex's -/// contents. -/// -/// The same reasoning applies to RwLock. -#[cfg(feature = "std")] -impl MallocSizeOf for std::sync::Mutex { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.lock().unwrap().size_of(ops) - } -} - -#[cfg(feature = "std")] -impl MallocSizeOf for parking_lot::Mutex { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.lock().size_of(ops) - } -} - -#[cfg(feature = "std")] -impl MallocSizeOf for std::sync::RwLock { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.read().unwrap().size_of(ops) - } -} - -#[cfg(feature = "std")] -impl MallocSizeOf for parking_lot::RwLock { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.read().size_of(ops) - } -} - -/// Implement notion of 0 allocation size for some type(s). -/// -/// if used for generics, by default it will require that generaic arguments -/// should implement `MallocSizeOf`. This can be avoided with passing "any: " -/// in front of type list. -/// -/// ```rust -/// use parity_util_mem::{malloc_size, malloc_size_of_is_0}; -/// -/// struct Data

{ -/// phantom: std::marker::PhantomData

, -/// } -/// -/// malloc_size_of_is_0!(any: Data

); -/// -/// // MallocSizeOf is NOT implemented for [u8; 333] -/// assert_eq!(malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0); -/// ``` -/// -/// and when no "any: " -/// -/// ```rust -/// use parity_util_mem::{malloc_size, malloc_size_of_is_0}; -/// -/// struct Data(pub T); -/// -/// // generic argument (`T`) must be `impl MallocSizeOf` -/// malloc_size_of_is_0!(Data); -/// -/// assert_eq!(malloc_size(&Data(0u8)), 0); -/// ``` -#[macro_export] -macro_rules! malloc_size_of_is_0( - ($($ty:ty),+) => ( - $( - impl $crate::MallocSizeOf for $ty { - #[inline(always)] - fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { - 0 - } - #[inline(always)] - fn constant_size() -> Option { Some(0) } - } - )+ - ); - (any: $($ty:ident<$($gen:ident),+>),+) => ( - $( - impl<$($gen),+> $crate::MallocSizeOf for $ty<$($gen),+> { - #[inline(always)] - fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { - 0 - } - #[inline(always)] - fn constant_size() -> Option { Some(0) } - } - )+ - ); - ($($ty:ident<$($gen:ident),+>),+) => ( - $( - impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> { - #[inline(always)] - fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize { - 0 - } - #[inline(always)] - fn constant_size() -> Option { Some(0) } - } - )+ - ); -); - -malloc_size_of_is_0!(bool, char, str); -malloc_size_of_is_0!(u8, u16, u32, u64, u128, usize); -malloc_size_of_is_0!(i8, i16, i32, i64, i128, isize); -malloc_size_of_is_0!(f32, f64); - -malloc_size_of_is_0!(rstd::sync::atomic::AtomicBool); -malloc_size_of_is_0!(rstd::sync::atomic::AtomicIsize); -malloc_size_of_is_0!(rstd::sync::atomic::AtomicUsize); - -malloc_size_of_is_0!(Range, Range, Range, Range, Range); -malloc_size_of_is_0!(Range, Range, Range, Range, Range); -malloc_size_of_is_0!(Range, Range); -malloc_size_of_is_0!(any: PhantomData); - -/// Measurable that defers to inner value and used to verify MallocSizeOf implementation in a -/// struct. -#[derive(Clone)] -pub struct Measurable(pub T); - -impl Deref for Measurable { - type Target = T; - - fn deref(&self) -> &T { - &self.0 - } -} - -impl DerefMut for Measurable { - fn deref_mut(&mut self) -> &mut T { - &mut self.0 - } -} - -#[cfg(feature = "hashbrown")] -impl MallocShallowSizeOf for hashbrown::HashMap { - fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - // See the implementation for std::collections::HashSet for details. - if ops.has_malloc_enclosing_size_of() { - self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) - } else { - self.capacity() * (size_of::() + size_of::() + size_of::()) - } - } -} - -#[cfg(feature = "hashbrown")] -impl MallocSizeOf for hashbrown::HashMap -where - K: MallocSizeOf, - V: MallocSizeOf, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.shallow_size_of(ops); - if let (Some(k), Some(v)) = (K::constant_size(), V::constant_size()) { - n += self.len() * (k + v) - } else { - n = self.iter().fold(n, |acc, (k, v)| acc + k.size_of(ops) + v.size_of(ops)) - } - n - } -} - -#[cfg(feature = "lru")] -impl MallocSizeOf for lru::LruCache -where - K: MallocSizeOf + rstd::cmp::Eq + rstd::hash::Hash, - V: MallocSizeOf, - S: rstd::hash::BuildHasher, -{ - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = 0; - if let (Some(k), Some(v)) = (K::constant_size(), V::constant_size()) { - n += self.len() * (k + v) - } else { - n = self.iter().fold(n, |acc, (k, v)| acc + k.size_of(ops) + v.size_of(ops)) - } - n - } -} - -malloc_size_of_is_0!( - [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8], [u8; 9], [u8; 10], [u8; 11], [u8; 12], - [u8; 13], [u8; 14], [u8; 15], [u8; 16], [u8; 17], [u8; 18], [u8; 19], [u8; 20], [u8; 21], [u8; 22], [u8; 23], - [u8; 24], [u8; 25], [u8; 26], [u8; 27], [u8; 28], [u8; 29], [u8; 30], [u8; 31], [u8; 32] -); - -macro_rules! impl_smallvec { - ($size: expr) => { - #[cfg(feature = "smallvec")] - impl MallocSizeOf for smallvec::SmallVec<[T; $size]> - where - T: MallocSizeOf, - { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = if self.spilled() { self.capacity() * core::mem::size_of::() } else { 0 }; - if let Some(t) = T::constant_size() { - n += self.len() * t; - } else { - n = self.iter().fold(n, |acc, elem| acc + elem.size_of(ops)) - } - n - } - } - }; -} - -impl_smallvec!(32); // kvdb uses this -impl_smallvec!(36); // trie-db uses this - -#[cfg(feature = "std")] -malloc_size_of_is_0!(std::time::Instant); -#[cfg(feature = "std")] -malloc_size_of_is_0!(std::time::Duration); - -#[cfg(all(test, feature = "std"))] // tests are using std implementations -mod tests { - use crate::{allocators::new_malloc_size_ops, MallocSizeOf, MallocSizeOfOps}; - use smallvec::SmallVec; - use std::{collections::BTreeSet, mem}; - impl_smallvec!(3); - - #[test] - fn test_smallvec_stack_allocated_type() { - let mut v: SmallVec<[u8; 3]> = SmallVec::new(); - let mut ops = new_malloc_size_ops(); - assert_eq!(v.size_of(&mut ops), 0); - v.push(1); - v.push(2); - v.push(3); - assert_eq!(v.size_of(&mut ops), 0); - assert!(!v.spilled()); - v.push(4); - assert!(v.spilled(), "SmallVec spills when going beyond the capacity of the inner backing array"); - assert_eq!(v.size_of(&mut ops), 4); // 4 u8s on the heap - } - - #[test] - fn test_smallvec_boxed_stack_allocated_type() { - let mut v: SmallVec<[Box; 3]> = SmallVec::new(); - let mut ops = new_malloc_size_ops(); - assert_eq!(v.size_of(&mut ops), 0); - v.push(Box::new(1u8)); - v.push(Box::new(2u8)); - v.push(Box::new(3u8)); - assert!(v.size_of(&mut ops) >= 3); - assert!(!v.spilled()); - v.push(Box::new(4u8)); - assert!(v.spilled(), "SmallVec spills when going beyond the capacity of the inner backing array"); - let mut ops = new_malloc_size_ops(); - let expected_min_allocs = mem::size_of::>() * 4 + 4; - assert!(v.size_of(&mut ops) >= expected_min_allocs); - } - - #[test] - fn test_smallvec_heap_allocated_type() { - let mut v: SmallVec<[String; 3]> = SmallVec::new(); - let mut ops = new_malloc_size_ops(); - assert_eq!(v.size_of(&mut ops), 0); - v.push("COW".into()); - v.push("PIG".into()); - v.push("DUCK".into()); - assert!(!v.spilled()); - assert!(v.size_of(&mut ops) >= "COW".len() + "PIG".len() + "DUCK".len()); - v.push("ÖWL".into()); - assert!(v.spilled()); - let mut ops = new_malloc_size_ops(); - let expected_min_allocs = mem::size_of::() * 4 + "ÖWL".len() + "COW".len() + "PIG".len() + "DUCK".len(); - assert!(v.size_of(&mut ops) >= expected_min_allocs); - } - - #[test] - fn test_large_vec() { - const N: usize = 128 * 1024 * 1024; - let val = vec![1u8; N]; - let mut ops = new_malloc_size_ops(); - assert!(val.size_of(&mut ops) >= N); - assert!(val.size_of(&mut ops) < 2 * N); - } - - #[test] - fn btree_set() { - let mut set = BTreeSet::new(); - for t in 0..100 { - set.insert(vec![t]); - } - // ~36 per value - assert!(crate::malloc_size(&set) > 3000); - } - - #[test] - fn special_malloc_size_of_0() { - struct Data

{ - phantom: std::marker::PhantomData

, - } - - malloc_size_of_is_0!(any: Data

); - - // MallocSizeOf is not implemented for [u8; 333] - assert_eq!(crate::malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0); - } - - #[test] - fn constant_size() { - struct AlwaysTwo(Vec); - - impl MallocSizeOf for AlwaysTwo { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.0.size_of(ops) - } - fn constant_size() -> Option { - Some(2) - } - } - - assert_eq!(AlwaysTwo::constant_size(), Some(2)); - assert_eq!(std::cmp::Reverse::::constant_size(), Some(0)); - assert_eq!(std::cell::RefCell::::constant_size(), Some(0)); - assert_eq!(std::cell::Cell::::constant_size(), Some(0)); - assert_eq!(Result::<(), ()>::constant_size(), Some(0)); - assert_eq!(<(AlwaysTwo, (), [u8; 32], AlwaysTwo)>::constant_size(), Some(2 + 2)); - assert_eq!(Option::::constant_size(), Some(0)); - assert_eq!(<&String>::constant_size(), Some(0)); - - assert_eq!(::constant_size(), None); - assert_eq!(std::borrow::Cow::::constant_size(), None); - assert_eq!(Result::<(), String>::constant_size(), None); - assert_eq!(Option::::constant_size(), None); - } -} diff --git a/parity-util-mem/src/memory_stats_jemalloc.rs b/parity-util-mem/src/memory_stats_jemalloc.rs deleted file mode 100644 index 22081d64c..000000000 --- a/parity-util-mem/src/memory_stats_jemalloc.rs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2021 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -pub use tikv_jemalloc_ctl::Error; -use tikv_jemalloc_ctl::{epoch, stats}; - -#[derive(Clone)] -pub struct MemoryAllocationTracker { - epoch: tikv_jemalloc_ctl::epoch_mib, - allocated: stats::allocated_mib, - resident: stats::resident_mib, -} - -impl MemoryAllocationTracker { - pub fn new() -> Result { - Ok(Self { epoch: epoch::mib()?, allocated: stats::allocated::mib()?, resident: stats::resident::mib()? }) - } - - pub fn snapshot(&self) -> Result { - // update stats by advancing the allocation epoch - self.epoch.advance()?; - - let allocated: u64 = self.allocated.read()? as _; - let resident: u64 = self.resident.read()? as _; - Ok(crate::MemoryAllocationSnapshot { allocated, resident }) - } -} diff --git a/parity-util-mem/src/memory_stats_noop.rs b/parity-util-mem/src/memory_stats_noop.rs deleted file mode 100644 index cf077c6f8..000000000 --- a/parity-util-mem/src/memory_stats_noop.rs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2021 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#[derive(Clone, Debug)] -pub struct Unimplemented; -pub use Unimplemented as Error; - -#[cfg(feature = "std")] -impl std::fmt::Display for Unimplemented { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { - fmt.write_str("unimplemented") - } -} - -#[derive(Clone)] -pub struct MemoryAllocationTracker {} - -impl MemoryAllocationTracker { - pub fn new() -> Result { - Err(Error) - } - - pub fn snapshot(&self) -> Result { - unimplemented!(); - } -} diff --git a/parity-util-mem/src/primitives_impls.rs b/parity-util-mem/src/primitives_impls.rs deleted file mode 100644 index cf98bc211..000000000 --- a/parity-util-mem/src/primitives_impls.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Implementation of `MallocSize` primitive types. - -use primitive_types::{H160, H256, H512, U128, U256, U512}; - -malloc_size_of_is_0!(U128, U256, U512, H160, H256, H512); - -#[cfg(test)] -mod tests { - - use primitive_types::H256; - - #[test] - fn smoky() { - let v = vec![H256::zero(), H256::zero()]; - - assert!(crate::MallocSizeOfExt::malloc_size_of(&v) >= 64); - } -} diff --git a/parity-util-mem/src/sizeof.rs b/parity-util-mem/src/sizeof.rs deleted file mode 100644 index 3d60913e4..000000000 --- a/parity-util-mem/src/sizeof.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Estimation for heapsize calculation. Usable to replace call to allocator method (for some -//! allocators or simply because we just need a deterministic cunsumption measurement). - -use crate::malloc_size::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf}; -#[cfg(not(feature = "std"))] -use alloc::boxed::Box; -#[cfg(not(feature = "std"))] -use alloc::string::String; -#[cfg(not(feature = "std"))] -use alloc::sync::Arc; -#[cfg(not(feature = "std"))] -use alloc::vec::Vec; -#[cfg(not(feature = "std"))] -use core::mem::{size_of, size_of_val}; - -#[cfg(feature = "std")] -use std::mem::{size_of, size_of_val}; -#[cfg(feature = "std")] -use std::sync::Arc; - -impl MallocShallowSizeOf for Box { - fn shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - size_of_val(&**self) - } -} - -impl MallocSizeOf for String { - fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - self.capacity() * size_of::() - } -} - -impl MallocShallowSizeOf for Vec { - fn shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - self.capacity() * size_of::() - } -} - -impl MallocUnconditionalShallowSizeOf for Arc { - fn unconditional_shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { - size_of::() - } -} diff --git a/parity-util-mem/tests/derive.rs b/parity-util-mem/tests/derive.rs deleted file mode 100644 index 63825ba61..000000000 --- a/parity-util-mem/tests/derive.rs +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2020 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![cfg(feature = "std")] - -use parity_util_mem::{MallocSizeOf, MallocSizeOfExt}; - -#[test] -fn derive_vec() { - #[derive(MallocSizeOf)] - struct Trivia { - v: Vec, - } - - let t = Trivia { v: vec![0u8; 1024] }; - - assert!(t.malloc_size_of() > 1000); -} - -#[test] -fn derive_hashmap() { - #[derive(MallocSizeOf, Default)] - struct Trivia { - hm: std::collections::HashMap>, - } - - let mut t = Trivia::default(); - - t.hm.insert(1, vec![0u8; 2048]); - - assert!(t.malloc_size_of() > 2000); -} - -#[test] -fn derive_ignore() { - #[derive(MallocSizeOf, Default)] - struct Trivia { - hm: std::collections::HashMap>, - #[ignore_malloc_size_of = "I don't like vectors"] - v: Vec, - } - - let mut t = Trivia::default(); - - t.hm.insert(1, vec![0u8; 2048]); - t.v = vec![0u8; 1024]; - assert!(t.malloc_size_of() < 3000); -} - -#[test] -#[cfg(all(feature = "lru", feature = "hashbrown"))] -fn derive_morecomplex() { - #[derive(MallocSizeOf)] - struct Trivia { - hm: hashbrown::HashMap>, - cache: lru::LruCache>, - } - - let mut t = Trivia { hm: hashbrown::HashMap::new(), cache: lru::LruCache::unbounded() }; - - t.hm.insert(1, vec![0u8; 2048]); - t.cache.put(1, vec![0u8; 2048]); - t.cache.put(2, vec![0u8; 4096]); - - assert!(t.malloc_size_of() > 8000); -} - -#[test] -fn derive_tuple() { - #[derive(MallocSizeOf)] - struct Trivia { - tp1: (), - tp2: (Vec, Vec), - } - - let t = Trivia { tp1: (), tp2: (vec![7u8; 1024], vec![9u8; 1024]) }; - - assert!(t.malloc_size_of() > 2000); - assert!(t.malloc_size_of() < 3000); -} diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index ea159ade9..06c1901ce 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -10,7 +10,7 @@ rust-version = "1.60.0" [dependencies] fixed-hash = { version = "0.8", path = "../fixed-hash", default-features = false } -uint = { version = "0.9.0", path = "../uint", default-features = false } +uint = { version = "0.9.5", path = "../uint", default-features = false } impl-serde = { version = "0.4.0", path = "impls/serde", default-features = false, optional = true } impl-codec = { version = "0.6.0", path = "impls/codec", default-features = false, optional = true } impl-num-traits = { version = "0.1.0", path = "impls/num-traits", default-features = false, optional = true } diff --git a/primitive-types/impls/num-traits/Cargo.toml b/primitive-types/impls/num-traits/Cargo.toml index bbcd3f02f..4209de17b 100644 --- a/primitive-types/impls/num-traits/Cargo.toml +++ b/primitive-types/impls/num-traits/Cargo.toml @@ -11,4 +11,4 @@ rust-version = "1.56.1" [dependencies] num-traits = { version = "0.2", default-features = false } integer-sqrt = "0.1" -uint = { version = "0.9.1", path = "../../../uint", default-features = false } +uint = { version = "0.9.5", path = "../../../uint", default-features = false } diff --git a/primitive-types/impls/serde/Cargo.toml b/primitive-types/impls/serde/Cargo.toml index 91915edf9..b572a2806 100644 --- a/primitive-types/impls/serde/Cargo.toml +++ b/primitive-types/impls/serde/Cargo.toml @@ -19,7 +19,7 @@ serde = { version = "1.0.101", default-features = false, features = ["alloc"] } criterion = "0.3.0" serde_derive = "1.0.101" serde_json = "1.0.41" -uint = { version = "0.9.0", path = "../../../uint" } +uint = { version = "0.9.5", path = "../../../uint" } [[bench]] name = "impl_serde" diff --git a/uint/CHANGELOG.md b/uint/CHANGELOG.md index 8e4690719..103758218 100644 --- a/uint/CHANGELOG.md +++ b/uint/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog]. ## [Unreleased] +## [0.9.5] - 2022-11-29 +- Implemented bitwise assign traits. [#690](https://github.com/paritytech/parity-common/pull/690) + ## [0.9.4] - 2022-09-20 - Made `one` const. [#650](https://github.com/paritytech/parity-common/pull/650) - Made `max_value` const. [#652](https://github.com/paritytech/parity-common/pull/652) diff --git a/uint/Cargo.toml b/uint/Cargo.toml index 0e4ed8aea..b1d2e8752 100644 --- a/uint/Cargo.toml +++ b/uint/Cargo.toml @@ -4,7 +4,7 @@ homepage = "http://parity.io" repository = "https://github.com/paritytech/parity-common" license = "MIT OR Apache-2.0" name = "uint" -version = "0.9.4" +version = "0.9.5" authors = ["Parity Technologies "] readme = "README.md" edition = "2021"