Skip to content

Commit dcbe5bb

Browse files
committed
Added Serializer.extractByLabels.
This is mainly used to quickly extract Chunks without needing the full serialized result.
1 parent ec8186f commit dcbe5bb

File tree

6 files changed

+129
-17
lines changed

6 files changed

+129
-17
lines changed

lib/compiler/NewAssembler.oz

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,23 +431,13 @@ define
431431
end
432432

433433
meth LookForXRegsInPattern(Pattern)
434-
proc {Loop Qs}
435-
case Qs
436-
of nil then
437-
skip
438-
[] _#_#patmatcapture(Idx)#Qr then
439-
if Idx >= @xRegCount then
440-
xRegCount := Idx+1
441-
end
442-
{Loop Qr}
443-
[] _#_#_#Qr then
444-
{Loop Qr}
445-
end
446-
end
447-
448-
Qs = {BootSerializer.serialize {BootSerializer.new} [Pattern#_]}
434+
Qs = {BootSerializer.extractByLabels Pattern r(patmatcapture:_)}
449435
in
450-
{Loop Qs}
436+
{ForAll Qs proc {$ _#patmatcapture(Idx)}
437+
if Idx >= @xRegCount then
438+
xRegCount := Idx + 1
439+
end
440+
end}
451441
end
452442

453443
meth output($)

platform-test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(BASE_FUNCTORS
1313
#"finalize.oz" "gc.oz"
1414
"state.oz" "thread.oz"
1515
"vm.oz"
16-
"reflection.oz"
16+
"reflection.oz" "serializer.oz"
1717
)
1818
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/base")
1919
foreach(FUNCTOR ${BASE_FUNCTORS})

platform-test/base/serializer.oz

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
%%%
2+
%%% Authors:
3+
%%% Kenny Chan <[email protected]>
4+
%%%
5+
%%% Copyright:
6+
%%% Kenny Chan 2014
7+
%%%
8+
%%% Last change:
9+
%%% $Date$ by $Author$
10+
%%% $Revision$
11+
%%%
12+
%%% This file is part of Mozart, an implementation of Oz 3:
13+
%%% http://www.mozart-oz.org
14+
%%%
15+
%%% See the file "LICENSE" or
16+
%%% http://www.mozart-oz.org/LICENSE.html
17+
%%% for information on usage and redistribution
18+
%%% of this file, and for a DISCLAIMER OF ALL
19+
%%% WARRANTIES.
20+
%%%
21+
22+
functor
23+
import
24+
Serializer at 'x-oz://boot/Serializer'
25+
26+
export
27+
Return
28+
29+
define
30+
Return = serializer([
31+
extractByLabels1(
32+
proc {$}
33+
Q = {NewChunk a(1)}
34+
R = {NewChunk b(Q)}
35+
T = {NewChunk c(2)}
36+
S = [R Q fun {$} T end]
37+
L = {Serializer.extractByLabels S r(chunk:_)}
38+
in
39+
true = (Q \= R)
40+
true = (R \= T)
41+
true = (Q \= T)
42+
3 = {Length L}
43+
32 = for sum:S Obj#Rec in L do
44+
chunk(_) = Rec
45+
if Obj == Q then
46+
{S 2}
47+
elseif Obj == R then
48+
{S 7}
49+
elseif Obj == T then
50+
{S 23}
51+
else
52+
{S 71}
53+
end
54+
end
55+
end
56+
)
57+
58+
extractByLabels2(
59+
proc {$}
60+
proc {TestFunc X _}
61+
case X
62+
of Y#r(Z) then {TestFunc Y Z}
63+
[] Y|_ then {TestFunc Y _}
64+
else skip
65+
end
66+
end
67+
68+
L = {Serializer.extractByLabels TestFunc r(patmatcapture:_)}
69+
in
70+
for _#Rec in L do
71+
patmatcapture(A) = Rec
72+
in
73+
true = {IsNumber A}
74+
end
75+
end
76+
)
77+
])
78+
79+
end

vm/vm/main/modules/modserializer.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public:
6565
}
6666
}
6767
};
68+
69+
class ExtractByLabels: public Builtin<ExtractByLabels> {
70+
public:
71+
ExtractByLabels(): Builtin("extractByLabels") {}
72+
73+
static void call(VM vm, In object, In labelsRecord, Out result) {
74+
result = Serializer::extractByLabels(vm, object, labelsRecord);
75+
}
76+
};
6877
};
6978

7079
}

vm/vm/main/serializer-decl.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public:
108108
public:
109109
UnstableNode doSerialize(VM vm, RichNode todo);
110110

111+
static UnstableNode extractByLabels(VM vm, RichNode object, RichNode labels);
112+
111113
private:
112114
UnstableNode done;
113115
};

vm/vm/main/serializer.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,36 @@ UnstableNode Serializer::doSerialize(VM vm, RichNode todo) {
9292
return UnstableNode(vm, done);
9393
}
9494

95+
UnstableNode Serializer::extractByLabels(VM vm, RichNode object, RichNode labels) {
96+
SerializationCallback cb(vm);
97+
98+
Dottable validLabels(labels);
99+
OzListBuilder resultList(vm);
100+
101+
VMAllocatedList<NodeBackup> nodeBackups;
102+
103+
auto n = OptVar::build(vm);
104+
cb.copy(n, object);
105+
106+
while (!cb.todoFrom.empty()) {
107+
auto from = cb.todoFrom.pop_front(vm);
108+
if (!from.is<Serialized>()) {
109+
auto serialized = from.type()->serialize(vm, &cb, from);
110+
auto label = RecordLike(serialized).label(vm);
111+
if (validLabels.hasFeature(vm, label)) {
112+
resultList.push_back(vm, buildSharp(vm, from, serialized));
113+
}
114+
nodeBackups.push_front(vm, from.makeBackup());
115+
from.reinit(vm, Serialized::build(vm, 0));
116+
}
117+
}
118+
119+
for (auto& nodeBackup : nodeBackups) {
120+
nodeBackup.restore();
121+
}
122+
nodeBackups.clear(vm);
123+
124+
return resultList.get(vm);
125+
}
126+
95127
}

0 commit comments

Comments
 (0)