Implement reference counting (#592)

This commit is contained in:
Daniel Wirtz
2019-06-05 23:15:39 +02:00
committed by GitHub
parent 3ed76a97f0
commit 0484a6b740
601 changed files with 261645 additions and 146131 deletions

View File

@ -1,16 +1,20 @@
import {
HEADER_SIZE as HEADER_SIZE_AB
} from "./internal/arraybuffer";
/// <reference path="./rt/index.d.ts" />
import {
HASH
} from "./internal/hash";
import { HASH } from "./util/hash";
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@inline const INITIAL_CAPACITY = 4;
@inline const FILL_FACTOR: f64 = 8 / 3;
@inline const FREE_FACTOR: f64 = 3 / 4;
// @ts-ignore: decorator
@inline
const INITIAL_CAPACITY = 4;
// @ts-ignore: decorator
@inline
const FILL_FACTOR: f64 = 8 / 3;
// @ts-ignore: decorator
@inline
const FREE_FACTOR: f64 = 3 / 4;
/** Structure of a map entry. */
@unmanaged class MapEntry<K,V> {
@ -20,13 +24,19 @@ import {
}
/** Empty bit. */
@inline const EMPTY: usize = 1 << 0;
// @ts-ignore: decorator
@inline
const EMPTY: usize = 1 << 0;
/** Size of a bucket. */
@inline const BUCKET_SIZE = sizeof<usize>();
// @ts-ignore: decorator
@inline
const BUCKET_SIZE = sizeof<usize>();
/** Computes the alignment of an entry. */
@inline function ENTRY_ALIGN<K,V>(): usize {
// @ts-ignore: decorator
@inline
function ENTRY_ALIGN<K,V>(): usize {
// can align to 4 instead of 8 if 32-bit and K/V is <= 32-bits
const maxkv = sizeof<K>() > sizeof<V>() ? sizeof<K>() : sizeof<V>();
const align = (maxkv > sizeof<usize>() ? maxkv : sizeof<usize>()) - 1;
@ -34,7 +44,9 @@ import {
}
/** Computes the aligned size of an entry. */
@inline function ENTRY_SIZE<K,V>(): usize {
// @ts-ignore: decorator
@inline
function ENTRY_SIZE<K,V>(): usize {
const align = ENTRY_ALIGN<K,V>();
const size = (offsetof<MapEntry<K,V>>() + align) & ~align;
return size;
@ -54,23 +66,24 @@ export class Map<K,V> {
get size(): i32 { return this.entriesCount; }
constructor() { this.clear(); }
constructor() {
this.clear();
}
clear(): void {
const bucketsSize = INITIAL_CAPACITY * <i32>BUCKET_SIZE;
this.buckets = new ArrayBuffer(bucketsSize);
this.bucketsMask = INITIAL_CAPACITY - 1;
const entriesSize = INITIAL_CAPACITY * <i32>ENTRY_SIZE<K,V>();
this.entries = new ArrayBuffer(entriesSize, true);
this.entries = new ArrayBuffer(entriesSize);
this.entriesCapacity = INITIAL_CAPACITY;
this.entriesOffset = 0;
this.entriesCount = 0;
}
private find(key: K, hashCode: u32): MapEntry<K,V> | null {
var entry = load<MapEntry<K,V>>(
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE,
HEADER_SIZE_AB
var entry = load<MapEntry<K,V>>( // unmanaged!
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE
);
while (entry) {
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
@ -90,9 +103,17 @@ export class Map<K,V> {
set(key: K, value: V): void {
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode);
var entry = this.find(key, hashCode); // unmanaged!
if (entry) {
entry.value = value;
if (isManaged<V>()) {
let oldRef = changetype<usize>(entry.value);
if (changetype<usize>(value) != oldRef) {
entry.value = changetype<V>(__retain(changetype<usize>(value)));
__release(oldRef);
}
} else {
entry.value = value;
}
} else {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
@ -104,24 +125,27 @@ export class Map<K,V> {
}
// append new entry
let entries = this.entries;
entry = changetype<MapEntry<K,V>>(
changetype<usize>(entries) + HEADER_SIZE_AB + this.entriesOffset++ * ENTRY_SIZE<K,V>()
);
entry.key = key;
entry.value = value;
entry = changetype<MapEntry<K,V>>(changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K,V>());
// link with the map
entry.key = isManaged<K>()
? changetype<K>(__retain(changetype<usize>(key)))
: key;
entry.value = isManaged<V>()
? changetype<V>(__retain(changetype<usize>(value)))
: value;
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
entry.taggedNext = load<usize>(bucketPtrBase, HEADER_SIZE_AB);
store<usize>(bucketPtrBase, changetype<usize>(entry), HEADER_SIZE_AB);
if (isManaged<K>()) __gc_link(changetype<usize>(this), changetype<usize>(key)); // tslint:disable-line
if (isManaged<V>()) __gc_link(changetype<usize>(this), changetype<usize>(value)); // tslint:disable-line
entry.taggedNext = load<usize>(bucketPtrBase);
store<usize>(bucketPtrBase, changetype<usize>(entry));
}
}
delete(key: K): bool {
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
if (isManaged<K>()) __release(changetype<usize>(entry.key));
if (isManaged<V>()) __release(changetype<usize>(entry.value));
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate
@ -137,12 +161,12 @@ export class Map<K,V> {
var newBucketsCapacity = <i32>(newBucketsMask + 1);
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>(), true);
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>());
// copy old entries to new entries
var oldPtr = changetype<usize>(this.entries) + HEADER_SIZE_AB;
var oldPtr = changetype<usize>(this.entries);
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
var newPtr = changetype<usize>(newEntries) + HEADER_SIZE_AB;
var newPtr = changetype<usize>(newEntries);
while (oldPtr != oldEnd) {
let oldEntry = changetype<MapEntry<K,V>>(oldPtr);
if (!(oldEntry.taggedNext & EMPTY)) {
@ -151,8 +175,8 @@ export class Map<K,V> {
newEntry.value = oldEntry.value;
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);
newEntry.taggedNext = load<usize>(newBucketPtrBase);
store<usize>(newBucketPtrBase, newPtr);
newPtr += ENTRY_SIZE<K,V>();
}
oldPtr += ENTRY_SIZE<K,V>();
@ -165,27 +189,65 @@ export class Map<K,V> {
this.entriesOffset = this.entriesCount;
}
keys(): K[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
var keys = Array.create<K>(size);
for (let i = 0; i < size; ++i) {
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
if (!(entry.taggedNext & EMPTY)) {
keys.push(entry.key);
}
}
return keys;
}
values(): V[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
var values = Array.create<V>(size);
for (let i = 0; i < size; ++i) {
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
if (!(entry.taggedNext & EMPTY)) {
values.push(entry.value);
}
}
return values;
}
toString(): string {
return "[object Map]";
}
private __gc(): void {
__gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
var entries = this.entries;
__gc_mark(changetype<usize>(entries)); // tslint:disable-line
// RT integration
@unsafe private __visit_impl(cookie: u32): void {
__visit(changetype<usize>(this.buckets), cookie);
var entries = changetype<usize>(this.entries);
if (isManaged<K>() || isManaged<V>()) {
let offset: usize = 0;
let end: usize = this.entriesOffset * ENTRY_SIZE<K,V>();
while (offset < end) {
let entry = changetype<MapEntry<K,V>>(
changetype<usize>(entries) + HEADER_SIZE_AB + offset * ENTRY_SIZE<K,V>()
);
let cur = entries;
let end = cur + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
while (cur < end) {
let entry = changetype<MapEntry<K,V>>(cur);
if (!(entry.taggedNext & EMPTY)) {
if (isManaged<K>()) __gc_mark(changetype<usize>(entry.key)); // tslint:disable-line
if (isManaged<V>()) __gc_mark(changetype<usize>(entry.value)); // tslint:disable-line
if (isManaged<K>()) {
let val = changetype<usize>(entry.key);
if (isNullable<K>()) {
if (val) __visit(val, cookie);
} else __visit(val, cookie);
}
if (isManaged<V>()) {
let val = changetype<usize>(entry.value);
if (isNullable<V>()) {
if (val) __visit(val, cookie);
} else __visit(val, cookie);
}
}
offset += ENTRY_SIZE<K,V>();
cur += ENTRY_SIZE<K,V>();
}
}
__visit(entries, cookie);
}
}