<commit_before>// JSObjectSize.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.js;

import java.lang.ref.*;
import java.util.*;

import ed.db.*;
import ed.util.*;
import ed.js.engine.*;

import ed.lang.python.Python;
import org.python.core.PyObject;

/** @expose
 */
public class JSObjectSize {

    /** The extra space every object takes up: 8 */
    public static final long OBJ_OVERHEAD = 8;

    /** Finds the size of a given object.
     * @param o The object
     * @return The object's size, in bytes
     */
    public static long size( Object o ){
        return size( o , null , null );
    }

    
    public static long size( Object o , SeenPath seen , Object from ){
        if ( o == null )
            return 0;

        final long size = _size( o , seen , from );
        return size;
    }
    
    private static long _size( Object o , SeenPath seen , Object from ){
        if ( o == null ||
             o instanceof Boolean ||
             o instanceof Short ||
             o instanceof Character || 
             o instanceof String ) {
            return 0;
        }

        if ( o instanceof DBRef
             || o instanceof JSDate
             || o instanceof Number
             || o instanceof JSDate
             || o instanceof java.io.File
             || o instanceof ed.log.Level
             || o instanceof ObjectId )
            return OBJ_OVERHEAD + 8;

        if ( o instanceof JSString ) {
            return 127552 + ( 10 * o.toString().length() );
        }
        
        // -------- this is the end of the "primitive" types ------

        if ( seen == null )
            seen = new SeenPath();
        else if ( ! seen.shouldVisit( o , from ) )
            return 0;


        // --------- special section for WeakReferences and other special thigns
        
        if ( o instanceof WeakReference )
            return OBJ_OVERHEAD;

        if ( o instanceof WeakHashMap )
            return OBJ_OVERHEAD + ( ((WeakHashMap)o).size() * OBJ_OVERHEAD );
        

        // --------  objects we know about --------
        
        if ( o instanceof JSFileChunk ){
            JSFileChunk chunk = (JSFileChunk)o;
            Object data = chunk.get( "data" );
            if ( data == null )
                return 32;
            
            return OBJ_OVERHEAD + ((JSBinaryData)data).length();
        }
        <commit_msg>Only count string's constructor once<commit_after>// JSObjectSize.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.js;

import java.lang.ref.*;
import java.util.*;

import ed.db.*;
import ed.util.*;
import ed.js.engine.*;

import ed.lang.python.Python;
import org.python.core.PyObject;

/** @expose
 */
public class JSObjectSize {

    /** The extra space every object takes up: 8 */
    public static final long OBJ_OVERHEAD = 8;
    public static final long STRING_CONS_OVERHEAD = 127552;

    public static boolean addStringCons = false;

    /** Finds the size of a given object.
     * @param o The object
     * @return The object's size, in bytes
     */
    public static long size( Object o ){
        long s = size( o , null , null );
        s += addStringCons ? STRING_CONS_OVERHEAD : 0;
        addStringCons = false;
        return s;
    }
    
    public static long size( Object o , SeenPath seen , Object from ){
        if ( o == null )
            return 0;

        final long size = _size( o , seen , from );
        return size;
    }

    private static long _size( Object o , SeenPath seen , Object from ){
        if ( o == null ||
             o instanceof Boolean ||
             o instanceof Short ||
             o instanceof Character || 
             o instanceof String ) {
            return 0;
        }

        if ( o instanceof DBRef
             || o instanceof JSDate
             || o instanceof Number
             || o instanceof JSDate
             || o instanceof java.io.File
             || o instanceof ed.log.Level
             || o instanceof ObjectId )
            return OBJ_OVERHEAD + 8;
        
        // -------- this is the end of the "primitive" types ------

        if ( seen == null )
            seen = new SeenPath();
        else if ( ! seen.shouldVisit( o , from ) )
            return 0;

        if ( o instanceof JSString ) {
            addStringCons = true;
            return 10 * o.toString().length();
        }

        // --------- special section for WeakReferences and other special thigns
        
        if ( o instanceof WeakReference )
            return OBJ_OVERHEAD;

        if ( o instanceof WeakHashMap )
            return OBJ_OVERHEAD + ( ((WeakHashMap)o).size() * OBJ_OVERHEAD );
        

        // --------  objects we know about --------
        
        if ( o instanceof JSFileChunk ){
            JSFileChunk chunk = (JSFileChunk)o;
            Object data = chunk.get( "data" );
            if ( data == null )
                return 32;
            
            return OBJ_OVERHEAD + ((JSBinaryData)data).length();
        }
        
{'fraction_non_alphanumeric': 0.09678957406230133, 'fraction_numerical': 0.007469802924348379, 'mean_word_length': 1.7218858131487889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4801905', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1643, 'n_words': 645}