Skip to content
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
18 changes: 13 additions & 5 deletions std/assembly/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ const INITIAL_CAPACITY = 4;

// @ts-ignore: decorator
@inline
const FILL_FACTOR: f64 = 8 / 3;
const FILL_FACTOR_N = 8;

// @ts-ignore: decorator
@inline
const FREE_FACTOR: f64 = 3 / 4;
const FILL_FACTOR_D = 3;

// @ts-ignore: decorator
@inline
const FREE_FACTOR_N = 3;

// @ts-ignore: decorator
@inline
const FREE_FACTOR_D = 4;

/** Structure of a map entry. */
@unmanaged class MapEntry<K,V> {
Expand Down Expand Up @@ -122,7 +130,7 @@ export class Map<K,V> {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
this.rehash(
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
? this.bucketsMask // just rehash if 1/4+ entries are empty
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
);
Expand Down Expand Up @@ -156,15 +164,15 @@ export class Map<K,V> {
var halfBucketsMask = this.bucketsMask >> 1;
if (
halfBucketsMask + 1 >= max<u32>(INITIAL_CAPACITY, this.entriesCount) &&
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
) this.rehash(halfBucketsMask);
return true;
}

private rehash(newBucketsMask: u32): void {
var newBucketsCapacity = <i32>(newBucketsMask + 1);
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
var newEntriesCapacity = newBucketsCapacity * FILL_FACTOR_N / FILL_FACTOR_D;
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>());

// copy old entries to new entries
Expand Down
18 changes: 13 additions & 5 deletions std/assembly/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ const INITIAL_CAPACITY = 4;

// @ts-ignore: decorator
@inline
const FILL_FACTOR: f64 = 8 / 3;
const FILL_FACTOR_N = 8;

// @ts-ignore: decorator
@inline
const FREE_FACTOR: f64 = 3 / 4;
const FILL_FACTOR_D = 3;

// @ts-ignore: decorator
@inline
const FREE_FACTOR_N = 3;

// @ts-ignore: decorator
@inline
const FREE_FACTOR_D = 4;

/** Structure of a set entry. */
@unmanaged class SetEntry<K> {
Expand Down Expand Up @@ -100,7 +108,7 @@ export class Set<T> {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
this.rehash(
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
? this.bucketsMask // just rehash if 1/4+ entries are empty
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
);
Expand Down Expand Up @@ -134,15 +142,15 @@ export class Set<T> {
var halfBucketsMask = this.bucketsMask >> 1;
if (
halfBucketsMask + 1 >= max<u32>(INITIAL_CAPACITY, this.entriesCount) &&
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
) this.rehash(halfBucketsMask);
return true;
}

private rehash(newBucketsMask: u32): void {
var newBucketsCapacity = <i32>(newBucketsMask + 1);
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
var newEntriesCapacity = newBucketsCapacity * FILL_FACTOR_N / FILL_FACTOR_D;
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<T>());

// copy old entries to new entries
Expand Down
Loading