Skip to content

Commit

Permalink
Wasm implementation is now more similar to how FFI is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Jul 7, 2021
1 parent e0cc801 commit f900235
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 33 deletions.
160 changes: 160 additions & 0 deletions src/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.11.0] - 2021-07-07

Added:

- Add HyperLogLog implementation (#1223)

Changed:

- Update `MinHash.set_abundances` to remove hash if 0 abund; handle negative abundances. (#1575)
- Improving `MinHash.remove_many(...)` performance (#1571)
- Improved intersection and union calculations (#1475)
- Bump MSRV to 1.42 (and other dep fixes) (#1461)
- Rework the `find` functionality for `Index` classes (#1392)
- Rationalize `SourmashSignature.name` and `str(sig)` (#1179)

Fixed:

- Fix needless borrows as suggested by clippy (#1636)
- Fix Rust 1.59 lints (#1600)
- Clean up clippy lints from 1.52 (#1505)
- Fix clippy lints introduced in 1.51 (#1407)
- CI/Rust: update and fix cbindgen config (#1473)
- pin needletail version to keep MSRV at 1.37 (#1393)
- Update proptest requirement from 0.9.6 to 1.0.0 (#1344)
- Fix clippy lints introduced in 1.50 and update nix configs (#1332)
- Update finch requirement from 0.3.0 to 0.4.1 (#1290)
- update rand for test, and activate "js" feature for getrandom (#1275)
- Fix new clippy warnings from Rust 1.49 (#1267)
- CI: small build fixes (#1252)

Removed:

- Remove 10x support in compute (#1229)

## [0.10.0] - 2020-10-08

Added:

- Add `clear` option to set_abundances(...) method (#1046)

Changed:

- Replace mx by scaled (#1139)

Fixed:

- Fix Rust panic error in signature creation (#1172)
- Update typed-builder requirement from 0.6.0 to 0.7.0 (#1121)
- update CI for latest branch name change (#1150)
- Update typed-builder requirement from 0.6.0 to 0.7.0 (#1121)

## [0.9.0] - 2020-07-13

Added:

- Cache md5sum calculation (#1058)
- Expose more of the API for wasm (signature and ComputeParameters) (#1058)
- Getters and setters for ComputeParameters (#1058)

Changed:

- Migrate from failure to thiserror (#1058)
- Bump MSRV to 1.37 (#1058)

Fixed:

- Use the derive feature in serde instead of serde_derive (#1058)
- Use nohash-hasher crate instead of previous NoHashHasher from finch.
- Update typed-builder to 0.6.0 (#1058)
- stricter niffler versions and add new gz feature to it (#1070)

## [0.8.0] - 2020-06-26

Added:

- compute-optimized MinHash (for small scaled or large cardinalities) (#1045)

## [0.7.0] - 2020-05-12

Changed:

- Hide internal representation in core (#986)

Fixed:

- update FFI and cbindgen (#986)

## [0.6.0] - 2020-04-28

Added:

- Nodegraph implementation based on khmer.Nodegraph (#799)

## [0.5.0] - 2020-02-08

Added:

- add_hash_with_abundance method in core library (#892)

Changed:

- More refactoring of MinHash comparison code (#882)
- Replace mins_push and abunds_push with set_abundances (#887)

Fixed:

- add_hash with num doesn't set abundances properly (#891)

## [0.4.0] - 2020-01-26

Added:

- Compute improvements: Parameter sets for defining signatures, add_protein implemented (#845)
- add_many for faster insertion of multiple hashes (#826)

Changed:

- Compare/similarity now have a downsample argument (#856)

Fixed:

- Improve sketching performance with lookup tables for complement and DNA validation (#861) (#865)
- Use tarpaulin instead of grcov (#862)
- set up publishing workflow for NPM and crates.io (#824)

## [0.3.0] - 2020-01-05

Added:

- Similarity with abundance method for MinHash (#808)
- Experimental support for indices in Rust (#773)
- Experimental SBT with MQF internal nodes in Rust (#772)

Changed:

- Make the sourmash crate library-only (#812)

Fixed:

- Use once_cell instead of lazy_static and lazy-init (#815)
- Fix mem leak in get_mins (#807)
- Fixes for WASI and WASM compilation (#771) (#723)

[unreleased]: https://github.com/sourmash-bio/sourmash/compare/r0.11.0...HEAD
[0.11.0]: https://github.com/sourmash-bio/sourmash/compare/r0.10.0...r0.11.0
[0.10.0]: https://github.com/sourmash-bio/sourmash/compare/r0.9.0...r0.10.0
[0.9.0]: https://github.com/sourmash-bio/sourmash/compare/r0.9.0...r0.10.0
[0.8.0]: https://github.com/sourmash-bio/sourmash/compare/r0.8.0...r0.9.0
[0.7.0]: https://github.com/sourmash-bio/sourmash/compare/r0.7.0...r0.8.0
[0.6.0]: https://github.com/sourmash-bio/sourmash/compare/r0.6.0...r0.7.0
[0.5.0]: https://github.com/sourmash-bio/sourmash/compare/r0.5.0...r0.6.0
[0.4.0]: https://github.com/sourmash-bio/sourmash/compare/r0.4.0...r0.5.0
[0.3.0]: https://github.com/sourmash-bio/sourmash/compare/r0.3.0...r0.4.0
3 changes: 0 additions & 3 deletions src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ wasm-bindgen-test = "0.3.0"
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies.assert_cmd]
version = "1.0.1"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false # https://github.com/rustwasm/wasm-pack/issues/886

[dev-dependencies]
assert_matches = "1.3.0"
criterion = "0.3.2"
Expand Down
4 changes: 0 additions & 4 deletions src/core/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
use wasm_bindgen::prelude::*;

use getset::{CopyGetters, Getters, Setters};
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -36,7 +33,6 @@ impl Signature {
}

#[allow(dead_code)]
#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown"), wasm_bindgen)]
#[derive(TypedBuilder, CopyGetters, Getters, Setters)]
pub struct ComputeParameters {
#[getset(get = "pub", set = "pub")]
Expand Down
3 changes: 0 additions & 3 deletions src/core/src/encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use std::iter::Iterator;
use std::str;

use once_cell::sync::Lazy;
#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
use wasm_bindgen::prelude::*;

use crate::Error;

#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown"), wasm_bindgen)]
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u32)]
Expand Down
4 changes: 0 additions & 4 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
use wasm_bindgen::prelude::*;

use crate::encodings::{aa_to_dayhoff, aa_to_hp, revcomp, to_aa, HashFunctions, VALID};
use crate::index::storage::ToWriter;
use crate::sketch::Sketch;
Expand Down Expand Up @@ -242,7 +239,6 @@ impl SigsTrait for Sketch {
}
}

#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown"), wasm_bindgen)]
#[derive(Serialize, Deserialize, Debug, Clone, TypedBuilder)]
pub struct Signature {
#[serde(default = "default_class")]
Expand Down
5 changes: 0 additions & 5 deletions src/core/src/sketch/minhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use crate::signature::SigsTrait;
use crate::sketch::hyperloglog::HyperLogLog;
use crate::Error;

#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
use wasm_bindgen::prelude::*;

pub fn max_hash_for_scaled(scaled: u64) -> u64 {
match scaled {
0 => 0,
Expand All @@ -35,7 +32,6 @@ pub fn scaled_for_max_hash(max_hash: u64) -> u64 {
}
}

#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown"), wasm_bindgen)]
#[derive(Debug, TypedBuilder)]
pub struct KmerMinHash {
num: u32,
Expand Down Expand Up @@ -939,7 +935,6 @@ mod test {
//#############
// A MinHash implementation for low scaled or large cardinalities

#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown"), wasm_bindgen)]
#[derive(Debug, TypedBuilder)]
pub struct KmerMinHashBTree {
num: u32,
Expand Down
58 changes: 44 additions & 14 deletions src/core/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

use wasm_bindgen::prelude::*;

use serde_json;
use sourmash::cmd::ComputeParameters as _ComputeParameters;
use sourmash::encodings::HashFunctions;
use sourmash::signature::Signature as _Signature;
use sourmash::signature::SigsTrait;
use sourmash::sketch::minhash::KmerMinHash as _KmerMinHash;

#[wasm_bindgen]
pub struct KmerMinHash(_KmerMinHash);

use crate::cmd::ComputeParameters;
use crate::encodings::HashFunctions;
use crate::signature::{Signature, SigsTrait};
use crate::sketch::minhash::KmerMinHash;
#[wasm_bindgen]
pub struct Signature(_Signature);

#[wasm_bindgen]
pub struct ComputeParameters(_ComputeParameters);

#[wasm_bindgen]
impl KmerMinHash {
Expand All @@ -32,34 +46,40 @@ impl KmerMinHash {
HashFunctions::murmur64_DNA
};

KmerMinHash::new(
KmerMinHash(_KmerMinHash::new(
scaled as u64,
ksize,
hash_function,
seed as u64,
track_abundance,
num,
)
))
}

#[wasm_bindgen]
pub fn add_sequence_js(&mut self, buf: &str) {
self.add_sequence(buf.as_bytes(), true)
self.0
.add_sequence(buf.as_bytes(), true)
.expect("Error adding sequence");
}

#[wasm_bindgen]
pub fn to_json(&mut self) -> String {
serde_json::to_string(self).unwrap()
serde_json::to_string(&self.0).unwrap()
}
}

#[wasm_bindgen]
impl ComputeParameters {
#[wasm_bindgen(constructor)]
pub fn new_with_params() -> ComputeParameters {
let params = ComputeParameters::default();
params
let params = _ComputeParameters::default();
ComputeParameters(params)
}

#[wasm_bindgen]
pub fn set_ksizes(&mut self, ksizes: Vec<u32>) {
self.0.set_ksizes(ksizes);
}
}

Expand All @@ -69,18 +89,28 @@ impl Signature {
pub fn new_from_params(params: &ComputeParameters) -> Signature {
//let params = ComputeParameters::default();

Signature::from_params(&params)
Signature(_Signature::from_params(&params.0))
}

#[wasm_bindgen]
pub fn add_sequence_js(&mut self, buf: &str) {
self.add_sequence(buf.as_bytes(), true)
self.0
.add_sequence(buf.as_bytes(), true)
.expect("Error adding sequence");
}

#[wasm_bindgen]
pub fn add_from_file(&mut self, fp: web_sys::File) {
unimplemented!()
}

#[wasm_bindgen]
pub fn to_json(&mut self) -> String {
serde_json::to_string(self).unwrap()
serde_json::to_string(&self.0).unwrap()
}

pub fn size(&self) -> usize {
self.0.size()
}
}

Expand Down

0 comments on commit f900235

Please sign in to comment.