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.

// I guess "transitive total object size" or something
// would be a better name.
sclass RecursiveObjectSize {
  long size;
  new SimpleStack stack;
  Set seen = new CompactIdentityHashSet; // add objects here to ignore them
  Set<Field> fieldsToIgnore;
  Set<S> realmsToIgnore;
  
  settable int dummySizeForUnknownObject = 24;
  
  settable bool skipTransientFields;
  
  swappable void onNewObject(O o) {}
  swappable void onSizeChanged() {}
  swappable bool shouldIgnore(O o) { false; }
  
  void resize(int expectedObjectCount) {
    seen = new CompactIdentityHashSet(expectedObjectCount);
  }
  
  void incSize(long n) {
    size += n;
    onSizeChanged();
  }
  
  long recurse(O o) {
    if (o == null) ret 0;
    seen.add(o);
    stack.add(o);
    
    while ping (nempty(stack))
      step(popLast(stack));
    ret size;
  }
  
  void step(O o) {
    if (guessDeepObjectSize_shouldIgnoreObject(o)) ret;
    if (shouldIgnore(o)) ret;
    onNewObject(o);
  
    if (realmsToIgnore != null && contains(realmsToIgnore, getRealm(o))) ret;
  
    ifdef RecursiveObjectSize_debug
      print("gos: " + getClassName(o));
    endifdef
    if (o instanceof Class) ret;
  
    if (isInstanceOfSyntheticClass(o))
      ret with incSize(dummySizeForUnknownObject());
      
    incSize(unsafe_sizeOf(o));
    
    if (o cast O[]) {
      for (O x : o)
        if (x != null && seen.add(x)) stack.add(x);
      ret;
    }
    
    if (sizeCalculation_shouldSkipObject(o)) ret;
    
    for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
      if (contains(fieldsToIgnore, f)) continue;
      if (skipTransientFields && isTransient(f)) continue;
      
      O x;
      try {
        x = f.get(o);
      } catch e {
        print("Error trying to access " + f + ": " + e);
        continue;
      }
      if (x != null && seen.add(x)) {
        stack.add(x);
        ifdef RecursiveObjectSize_debug
          print("gos field " + f + ": " + getClassName(x));
        endifdef
      }
    }
  
    //fail("can't handle object type: " + className(o));
  }
  
  bool hasSeen(O o) {
    ret seen.contains(o);
  }
}

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: 361 / 712
Version history: 18 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)