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).

sclass guessDeepObjectSize_recursive_Data {
  long size;
  new L stack;
  Set seen = identityHashSet(); // add objects here to ignore them
  Set<Field> fieldsToIgnore;
  Set<S> realmsToIgnore;
  
  VF1<O> onNewObject;
}

static long guessDeepObjectSize_recursive(O o) {
  if (o == null) ret 0;
  ret guessDeepObjectSize_recursive(new guessDeepObjectSize_recursive_Data, o);
}

static long guessDeepObjectSize_recursive(guessDeepObjectSize_recursive_Data data, O o) {
  if (o == null) ret 0;
  data.stack.add(o);
  while ping (nempty(data.stack)) {
    /*ifdef guessDeepObjectSize_recursive_debug
      print("gos stack size: " + l(data.stack));
    endifdef*/
    guessDeepObjectSize_recursive_step(data, popLast(data.stack));
  }
  ret data.size;
}

svoid guessDeepObjectSize_recursive_step(guessDeepObjectSize_recursive_Data data, O o) {
  if (!data.seen.add(o)) ret;
  
  if (guessDeepObjectSize_shouldIgnoreObject(o)) ret;
  
  callF(data.onNewObject, o);
  
  if (data.realmsToIgnore != null && contains(data.realmsToIgnore, getRealm(o))) ret;
  
  ifdef guessDeepObjectSize_recursive_debug
    print("gos: " + getClassName(o));
  endifdef
  if (o instanceof Class) ret;

  if (isArray(o)) {
    long len;
    if (o instanceof O[]) {
      // len is computed correctly by unsafe_sizeOf
      len = unsafe_sizeOf(o);
      for (O x : (O[]) o)
        if (x != null && !data.seen.contains(x)) data.stack.add(x);
    } else
      len = inMemorySizeOfPrimitiveArray(o);
    data.size += len;
    ret;
  }
  
  data.size += unsafe_sizeOf(o);
  
  if (sizeCalculation_shouldSkipObject(o)) ret;
  
  for (Field f : nonStaticNonPrimitiveFieldObjects(o)) {
    if (contains(data.fieldsToIgnore, f)) continue;
    O x;
    try {
      x = f.get(o);
    } catch e {
      print("Error trying to access " + f + ": " + e);
      continue;
    }
    if (x != null && !data.seen.contains(x)) {
      data.stack.add(x);
      ifdef guessDeepObjectSize_recursive_debug
        print("gos field " + f + ": " + getClassName(x));
      endifdef
    }
  }

  //fail("can't handle object type: " + className(o));
}

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: 481 / 642
Version history: 42 change(s)
Referenced in: [show references]