Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

77
LINES

< > BotCompany Repo | #1009445 // guessDeepObjectSize_recursive (should be pretty exact now)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3197L/21K).

1  
sclass guessDeepObjectSize_recursive_Data {
2  
  long size;
3  
  new L stack;
4  
  Set seen = identityHashSet(); // add objects here to ignore them
5  
  Set<Field> fieldsToIgnore;
6  
  Set<S> realmsToIgnore;
7  
  
8  
  VF1<O> onNewObject;
9  
}
10  
11  
static long guessDeepObjectSize_recursive(O o) {
12  
  if (o == null) ret 0;
13  
  ret guessDeepObjectSize_recursive(new guessDeepObjectSize_recursive_Data, o);
14  
}
15  
16  
static long guessDeepObjectSize_recursive(guessDeepObjectSize_recursive_Data data, O o) {
17  
  if (o == null) ret 0;
18  
  data.stack.add(o);
19  
  while ping (nempty(data.stack)) {
20  
    /*ifdef guessDeepObjectSize_recursive_debug
21  
      print("gos stack size: " + l(data.stack));
22  
    endifdef*/
23  
    guessDeepObjectSize_recursive_step(data, popLast(data.stack));
24  
  }
25  
  ret data.size;
26  
}
27  
28  
svoid guessDeepObjectSize_recursive_step(guessDeepObjectSize_recursive_Data data, O o) {
29  
  if (!data.seen.add(o)) ret;
30  
  
31  
  if (guessDeepObjectSize_shouldIgnoreObject(o)) ret;
32  
  
33  
  callF(data.onNewObject, o);
34  
  
35  
  if (data.realmsToIgnore != null && contains(data.realmsToIgnore, getRealm(o))) ret;
36  
  
37  
  ifdef guessDeepObjectSize_recursive_debug
38  
    print("gos: " + getClassName(o));
39  
  endifdef
40  
  if (o instanceof Class) ret;
41  
42  
  if (isArray(o)) {
43  
    long len;
44  
    if (o instanceof O[]) {
45  
      // len is computed correctly by unsafe_sizeOf
46  
      len = unsafe_sizeOf(o);
47  
      for (O x : (O[]) o)
48  
        if (x != null && !data.seen.contains(x)) data.stack.add(x);
49  
    } else
50  
      len = inMemorySizeOfPrimitiveArray(o);
51  
    data.size += len;
52  
    ret;
53  
  }
54  
  
55  
  data.size += unsafe_sizeOf(o);
56  
  
57  
  if (sizeCalculation_shouldSkipObject(o)) ret;
58  
  
59  
  for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
60  
    if (contains(data.fieldsToIgnore, f)) continue;
61  
    O x;
62  
    try {
63  
      x = f.get(o);
64  
    } catch e {
65  
      print("Error trying to access " + f + ": " + e);
66  
      continue;
67  
    }
68  
    if (x != null && !data.seen.contains(x)) {
69  
      data.stack.add(x);
70  
      ifdef guessDeepObjectSize_recursive_debug
71  
        print("gos field " + f + ": " + getClassName(x));
72  
      endifdef
73  
    }
74  
  }
75  
76  
  //fail("can't handle object type: " + className(o));
77  
}

Author comment

Began life as a copy of #1009411

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1009445
Snippet name: guessDeepObjectSize_recursive (should be pretty exact now)
Eternal ID of this version: #1009445/43
Text MD5: 82f0c622278840902f2b63133438f123
Transpilation MD5: 09bc841fbb2b149c0ecaa592587aff64
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-13 18:08:27
Source code size: 2173 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 484 / 647
Version history: 42 change(s)
Referenced in: [show references]