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

87
LINES

< > BotCompany Repo | #1029151 // RecursiveObjectSize - successor to guessDeepObjectSize_recursive

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

Transpiled version (10713L) is out of date.

1  
// I guess "transitive total object size" or something
2  
// would be a better name.
3  
sclass RecursiveObjectSize {
4  
  long size;
5  
  new SimpleStack stack;
6  
  Set seen = new CompactIdentityHashSet; // add objects here to ignore them
7  
  Set<Field> fieldsToIgnore;
8  
  Set<S> realmsToIgnore;
9  
  
10  
  settable int dummySizeForUnknownObject = 24;
11  
  
12  
  settable bool skipTransientFields;
13  
  
14  
  swappable void onNewObject(O o) {}
15  
  swappable void onSizeChanged() {}
16  
  swappable bool shouldIgnore(O o) { false; }
17  
  
18  
  void resize(int expectedObjectCount) {
19  
    seen = new CompactIdentityHashSet(expectedObjectCount);
20  
  }
21  
  
22  
  void incSize(long n) {
23  
    size += n;
24  
    onSizeChanged();
25  
  }
26  
  
27  
  long recurse(O o) {
28  
    if (o == null) ret 0;
29  
    seen.add(o);
30  
    stack.add(o);
31  
    
32  
    while ping (nempty(stack))
33  
      step(popLast(stack));
34  
    ret size;
35  
  }
36  
  
37  
  void step(O o) {
38  
    if (guessDeepObjectSize_shouldIgnoreObject(o)) ret;
39  
    if (shouldIgnore(o)) ret;
40  
    onNewObject(o);
41  
  
42  
    if (realmsToIgnore != null && contains(realmsToIgnore, getRealm(o))) ret;
43  
  
44  
    ifdef RecursiveObjectSize_debug
45  
      print("gos: " + getClassName(o));
46  
    endifdef
47  
    if (o instanceof Class) ret;
48  
  
49  
    if (isInstanceOfSyntheticClass(o))
50  
      ret with incSize(dummySizeForUnknownObject());
51  
      
52  
    incSize(unsafe_sizeOf(o));
53  
    
54  
    if (o cast O[]) {
55  
      for (O x : o)
56  
        if (x != null && seen.add(x)) stack.add(x);
57  
      ret;
58  
    }
59  
    
60  
    if (sizeCalculation_shouldSkipObject(o)) ret;
61  
    
62  
    for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
63  
      if (contains(fieldsToIgnore, f)) continue;
64  
      if (skipTransientFields && isTransient(f)) continue;
65  
      
66  
      O x;
67  
      try {
68  
        x = f.get(o);
69  
      } catch e {
70  
        print("Error trying to access " + f + ": " + e);
71  
        continue;
72  
      }
73  
      if (x != null && seen.add(x)) {
74  
        stack.add(x);
75  
        ifdef RecursiveObjectSize_debug
76  
          print("gos field " + f + ": " + getClassName(x));
77  
        endifdef
78  
      }
79  
    }
80  
  
81  
    //fail("can't handle object type: " + className(o));
82  
  }
83  
  
84  
  bool hasSeen(O o) {
85  
    ret seen.contains(o);
86  
  }
87  
}

Author comment

Began life as a copy of #1009445

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029151
Snippet name: RecursiveObjectSize - successor to guessDeepObjectSize_recursive
Eternal ID of this version: #1029151/19
Text MD5: 9d540d7f6815bf003b90086d7ddee8f4
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-24 16:43:16
Source code size: 2195 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 272 / 606
Version history: 18 change(s)
Referenced in: [show references]