Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 4835303

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Report possible savings from sharing strings
Belonging to [master]: - #2231
1 parent 9062175 commit 4835303

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

Compiler/FrontEnd/ComponentReference.mo

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,5 +3828,38 @@ algorithm
38283828
File.write(file, "]");
38293829
end writeSubscripts;
38303830

3831+
function getConsumedMemory
3832+
input DAE.ComponentRef inCref;
3833+
output Real szIdents=0;
3834+
output Real szTypes=0;
3835+
output Real szSubs=0;
3836+
protected
3837+
DAE.ComponentRef cr=inCref;
3838+
Boolean b = true;
3839+
algorithm
3840+
while b loop
3841+
(b,cr) := match cr
3842+
case DAE.CREF_IDENT()
3843+
algorithm
3844+
szIdents := szIdents + System.getSizeOfData(cr.ident);
3845+
szTypes := szTypes + System.getSizeOfData(cr.identType);
3846+
szSubs := szSubs + System.getSizeOfData(cr.subscriptLst);
3847+
then (false,cr);
3848+
case DAE.CREF_ITER()
3849+
algorithm
3850+
szIdents := szIdents + System.getSizeOfData(cr.ident);
3851+
szTypes := szTypes + System.getSizeOfData(cr.identType);
3852+
then (false,cr);
3853+
case DAE.CREF_QUAL()
3854+
algorithm
3855+
szIdents := szIdents + System.getSizeOfData(cr.ident);
3856+
szTypes := szTypes + System.getSizeOfData(cr.identType);
3857+
szSubs := szSubs + System.getSizeOfData(cr.subscriptLst);
3858+
then (true,cr.componentRef);
3859+
else (false,cr);
3860+
end match;
3861+
end while;
3862+
end getConsumedMemory;
3863+
38313864
annotation(__OpenModelica_Interface="frontend");
38323865
end ComponentReference;

Compiler/SimCode/SimCodeMain.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,10 +1317,10 @@ protected function serializeNotify<T>
13171317
input T data;
13181318
input String name;
13191319
protected
1320-
Real sz,raw_sz;
1320+
Real sz,raw_sz,nonSharedStringSize;
13211321
algorithm
1322-
(sz,raw_sz) := System.getSizeOfData(data);
1323-
Error.addMessage(Error.SERIALIZED_SIZE, {name, StringUtil.bytesToReadableUnit(sz), StringUtil.bytesToReadableUnit(raw_sz)});
1322+
(sz,raw_sz,nonSharedStringSize) := System.getSizeOfData(data);
1323+
Error.addMessage(Error.SERIALIZED_SIZE, {name, StringUtil.bytesToReadableUnit(sz), StringUtil.bytesToReadableUnit(raw_sz), StringUtil.bytesToReadableUnit(nonSharedStringSize)});
13241324
end serializeNotify;
13251325

13261326
annotation(__OpenModelica_Interface="backend");

Compiler/Util/Error.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ public constant Message UNIONTYPE_MISSING_TYPEVARS = MESSAGE(5044, TRANSLATION()
10211021
public constant Message UNIONTYPE_WRONG_NUM_TYPEVARS = MESSAGE(5045, TRANSLATION(), ERROR(),
10221022
Util.gettext("Uniontype %s has %s type variables, but got %s."));
10231023
public constant Message SERIALIZED_SIZE = MESSAGE(5046, TRANSLATION(), NOTIFICATION(),
1024-
Util.gettext("%s uses %s of memory (%s without GC overhead)."));
1024+
Util.gettext("%s uses %s of memory (%s without GC overhead; %s is consumed by not performing String sharing)."));
10251025

10261026
public constant Message COMPILER_ERROR = MESSAGE(5999, TRANSLATION(), ERROR(),
10271027
Util.notrans("%s"));

Compiler/Util/System.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,8 @@ function getSizeOfData<T>
13111311
input T data;
13121312
output Real sz;
13131313
output Real raw_sz "The size without granule overhead";
1314-
external "C" sz=SystemImpl__getSizeOfData(data, raw_sz) annotation(Library = {"omcruntime"}, Documentation(info="<html>
1314+
output Real nonSharedStringSize "The size that could be saved if String sharing was enabled";
1315+
external "C" sz=SystemImpl__getSizeOfData(data, raw_sz, nonSharedStringSize) annotation(Library = {"omcruntime"}, Documentation(info="<html>
13151316
Counts the number of bytes that were allocated to hold the given data structure.
13161317
Includes constant data and handles cycles.
13171318
</html>"));

Compiler/runtime/systemimplmisc.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
#include <string>
6-
#include <set>
76
#include <stack>
7+
#include <unordered_map>
88

99
using namespace std;
1010

@@ -55,19 +55,20 @@ static inline size_t actualByteSize(size_t sz)
5555
return res;
5656
}
5757
#include <stdio.h>
58-
double SystemImpl__getSizeOfData(void *data, double *raw_size_res)
58+
double SystemImpl__getSizeOfData(void *data, double *raw_size_res, double *nonshared_str_res)
5959
{
60-
size_t sz=0, raw_sz=0;
61-
std::set<void*> handled;
60+
size_t sz=0, raw_sz=0, nonshared_str_sz=0;
61+
std::unordered_map<void*,void*> handled;
6262
std::stack<void*> work;
63+
std::unordered_map<std::string, void*> strings;
6364
work.push(data);
6465
while (!work.empty()) {
6566
void *item = work.top();
6667
work.pop();
6768
if (handled.find(item) != handled.end()) {
6869
continue;
6970
}
70-
handled.insert(item);
71+
handled[item] = 0;
7172
if (MMC_IS_IMMEDIATE(item)) {
7273
/* Uses up zero space */
7374
continue;
@@ -83,8 +84,16 @@ double SystemImpl__getSizeOfData(void *data, double *raw_size_res)
8384
continue;
8485
}
8586
if (MMC_HDRISSTRING(hdr)) {
86-
raw_sz += sizeof(void*)+MMC_STRLEN(item)+1;
87-
sz += actualByteSize(sizeof(void*)+MMC_STRLEN(item)+1);
87+
size_t t = sizeof(void*)+MMC_STRLEN(item)+1;
88+
size_t actual = actualByteSize(t);
89+
std::string s(MMC_STRINGDATA(item));
90+
if (strings.find(s) != strings.end()) {
91+
nonshared_str_sz += actual;
92+
} else {
93+
strings[s] = item;
94+
}
95+
raw_sz += t;
96+
sz += actual;
8897
continue;
8998
}
9099
if (MMC_HDRISSTRUCT(hdr)) {
@@ -99,11 +108,12 @@ double SystemImpl__getSizeOfData(void *data, double *raw_size_res)
99108
}
100109
continue;
101110
}
102-
fprintf(stderr, "abort... bytes=%d num items=%d\n", sz, handled.size());
103-
printAny(item);
111+
fprintf(stderr, "abort... bytes=%ld num items=%ld\n", sz, handled.size());
112+
printAny(item);
104113
abort();
105114
}
106115
*raw_size_res = raw_sz;
116+
*nonshared_str_res = nonshared_str_sz;
107117
return sz;
108118
}
109119

0 commit comments

Comments
 (0)