Skip to content

Commit

Permalink
New implementation of reactive tables. (#113)
Browse files Browse the repository at this point in the history
* New implementation of reactive tables.

Up until now, the values used in a reactive table had to implement
a bunch of traits (such as Equality, Orderable etc ...).
The reason was that the implementation was relying on a sort of
those values to function properly (compute the diffs etc ...).
The other weird thing is that the implementation was returning
a sorted array of the values inserted and was perform a "unique"
operation on them for no good reason other than it was convenient
for the old implementation.

This new implementation fixes that, the values don't have any constraint
other than being frozen (the interned pointer is used to do the operations).
The values returned by "get" preserve repetitions.

PS: Turns out the new implementation fixed a long standing bug in the
incremental type-checker when a module was changing name.

PS2: I used the opportunity to also fix issue #72.

* removed constraints on Reactive table type parameter

* revert changes in skip_to_native
  • Loading branch information
pikatchu committed Nov 22, 2019
1 parent b9681f9 commit cccd57b
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 104 deletions.
2 changes: 2 additions & 0 deletions lkg/runtime/native/include/skip/Reactive-extc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lkg/runtime/native/src/Reactive.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions src/frontend/skipExpand.sk
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module alias A = SkipAst;
/*****************************************************************************/
module ChildMap;

const childCache: Reactive.DiffCache = Reactive.DiffCache::create();
const childCache: Reactive.Table<String> = Reactive.Table::create();

memoized fun getChildren(className: String): SSet {
childCache[className].foldl(
Expand All @@ -74,6 +74,13 @@ memoized fun getChildren(className: String): SSet {

untracked fun populateCache(classDefs: UMap<A.Class_def>): void {
fileMap = mutable Map[];
cacheMap = mutable Map[];
for (classDef in classDefs) {
(fileRange, className) = classDef.name;
fileName = fileRange.filename;
fileMap![fileName] = mutable Vector[];
cacheMap![(fileName, className)] = Array[];
};
for (classDef in classDefs) addClass(fileMap, classDef);
for (filename => assocs in fileMap) {
defMap = mutable Map<String, mutable Vector<String>>[];
Expand All @@ -86,8 +93,12 @@ untracked fun populateCache(classDefs: UMap<A.Class_def>): void {
};
};
for (parentName => childSet in defMap) {
childCache.set(filename, parentName, freeze(childSet))
}
cacheMap![(filename, parentName)] = childSet.toArray();
};
};
for (key => childSet in cacheMap) {
(fileName, parentName) = key;
childCache.set(fileName, parentName, childSet);
}
}

Expand All @@ -103,11 +114,8 @@ untracked private fun addClass(
| A.Tclass((_, x)) -> x
| _ -> invariant_violation("The name should have been expanded")
};
if (map.containsKey(fileName)) {
map[fileName].push((parentStr, childStr))
} else {
map![fileName] = mutable Vector[(parentStr, childStr)];
}
invariant(map.containsKey(fileName));
map[fileName].push((parentStr, childStr));
}
}

Expand Down Expand Up @@ -171,7 +179,7 @@ module end;

module ModuleMap;

const moduleCache: Reactive.DiffCache = Reactive.DiffCache::create();
const moduleCache: Reactive.Table<String> = Reactive.Table::create();

fun containsKey(key: String): Bool {
moduleCache.get(key).size() > 0
Expand Down Expand Up @@ -211,7 +219,7 @@ untracked fun populateCache(moduleDefs: SkipExpand.Module_map_t): void {
};
for (fileName => moduleDefs2 in fileMap) {
for (moduleName => defs in moduleDefs2) {
moduleCache.set(fileName, moduleName, defs.chill())
moduleCache.set(fileName, moduleName, defs.toArray())
}
};
}
Expand All @@ -221,7 +229,7 @@ module end;
module GlobalEnv;

private memoized fun getDefinitions(defName: String): A.Program {
files = SkipExpand.fileCache.get(defName);
files = SkipExpand.fileCache.get(defName).sorted().collect(Vector).unique();
asts = SkipParse.parse_files(files, true);
moduleDefsVector = asts.map(SkipExpand.definitions);
module_defs = moduleDefsVector.foldl(SkipExpand.mergeModuleMap, SortedMap[]);
Expand Down Expand Up @@ -296,7 +304,7 @@ class Defs{
type_defs: UMap<A.Type_alias_def>,
}

class DefNames(defs: Vector<String>) {
class DefNames(defs: Array<String>) {
private fun binarySearch(elt: String, i: Int, j: Int): Bool {
if (i > j) return false;
pivot = (i + j) / 2;
Expand All @@ -320,7 +328,7 @@ class DefNames(defs: Vector<String>) {
this.binarySearch("type:" + typeName, 0, this.defs.size() - 1)
}

fun names(): Vector<String> {
fun names(): Array<String> {
this.defs.map(x -> x.split(":")[1])
}
}
Expand All @@ -336,7 +344,10 @@ fun name_of_module(x: Module_): String {

fun module_names_find(x: Module_): ?DefNames {
// TODO: this should return none when the module is not defined
Some(DefNames(ModuleMap.moduleCache.get(name_of_module(x))))
mresult = ModuleMap.moduleCache.get(name_of_module(x))
.sorted()
.collect(Vector);
Some(DefNames(mresult.unique().toArray()))
}

fun module_map_find(x: Module_, y: Module_map_t): ?Defs {
Expand Down Expand Up @@ -415,7 +426,7 @@ fun empty_defs(mod_name: Module_): Defs {
}

fun empty_defs_names(): DefNames {
DefNames(Vector[])
DefNames(Array[])
}

fun makeEnv(): Env {
Expand Down Expand Up @@ -744,7 +755,7 @@ fun malias_type_def_<Ta>(
* file).
*/
/*****************************************************************************/
const fileCache: Reactive.DiffCache = Reactive.DiffCache::create();
const fileCache: Reactive.Table<String> = Reactive.Table::create();

mutable private class FileMapBuilder{private mutable currentModule: Module_} {
static fun create(): mutable this {
Expand All @@ -759,7 +770,7 @@ mutable private class FileMapBuilder{private mutable currentModule: Module_} {
(fileRange, _) = name;
defName = make_qualified_name(fileRange, this.currentModule, name);
filename = fileRange.filename;
fileCache.set(filename, defName.i1, Vector[filename])
fileCache.set(filename, defName.i1, Array[filename])
}

untracked private mutable fun file(defs: List<A.Definition>): void {
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/skipTyping.sk
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,8 @@ fun merge_extend(

memoized fun fun_def(funName: SkipAst.Name): TAst.Fun_def {
fd = SkipNaming.getFun(funName);
named_fun_def(fd)
result = named_fun_def(fd);
result
}

fun named_fun_def(fd: SkipNamedAst.Fun_def): TAst.Fun_def {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/native/include/skip/Reactive-extc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/runtime/native/src/Reactive.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cccd57b

Please sign in to comment.