0

This is somewhat a 2 question in one. It's for a video game and I am using libgdx, so it must be relatively fast.

I have object that will be read and written to a derby database. Some of those objects data structure will be defined at runtime, so I cannot make a POJO with getters and setters and using the persistance API for example.

Instead a data object will look like this

import java.sql.Types;

import com.badlogic.gdx.utils.Array;

public class Creature extends PBDBsystem_table
{
    protected static Array<PBDBsystem_field> s_field_list = new Array<PBDBsystem_field>();
    protected static String s_table_name = new String ("creature");

    Creature ()
    {
        create ( s_table_name, s_field_list );
    }

    public static boolean init()
    {
        s_field_list.add ( new PBDBsystem_field ("pk", Types.INTEGER, 0, "", true, true ));
        s_field_list.add( new PBDBsystem_field ("name", Types.VARCHAR, 30 ));
        s_field_list.add( new PBDBsystem_field ("cost", Types.INTEGER, 0, "1" ));
        s_field_list.add( new PBDBsystem_field ("strength", Types.INTEGER ));

        create_table ( s_table_name, s_field_list);

        return true;
    }
}

For your information, the full length constructor is defined as:

public PBDBsystem_field ( String name, int type, int size, String default_value, boolean not_null, boolean primary_key )

What this basically do is that init() will create a list of field in an Array that will remain statically in the class and a reference to the table name and field list is passed to the parent class though create(). So that each object has a reference on it's data structure (Which is allocated only once). Then the parent class define the data container for each object like this:

private ArrayMap<String, String> p_data_list; /** structure to contain database data */

Right now I am placing all fields as strings. The key is the field name, and the value is the field data. I am using generic getters and setters in the parent class that require a key like for example:

public void set ( String key, String value)
    {
        p_data_list.put( key, value);

    }

    public String get ( String key )
    {
        return p_data_list.get(key);
    }

Question 1: Is it recommended to store everything as string as it will sometimes demand conversion to int which could slow things down. I was thinking of using for example the Interger class instead of primitives and make an array map of Object instead of String. But I should still get conversion and casting issues, but they should remain in the parent class if I do all the generic getters/setter correctly.

Ok, during the creation of the object, I had by reflex created a new string instance for each field in the table like this.

protected void create ( String table_name, Array<PBDBsystem_field> field_list )
    {
        p_table_name = table_name;
        p_field_list = field_list;

        p_data_list = new ArrayMap <String, String> ();

        p_data_list.ordered = false;

        for ( PBDBsystem_field field: p_field_list)
        {
            p_data_list.put ( field.name, new String() );
        }
    }

This method reference the table name and field list to the parent class, then for each field it create a new string to contain the data. But if I modify the content of a field using the set method above, it will use another string to insert it in the array map. Which leads me to

Question 2: Will it do a deep copy into the array map's string, or will it simply reference the string that was passed in the set method? Meaning that there is no reason to actually instantiate a string for each field. I should rather leave everything to null.

Considering it's for a video game, I want to minimize the use of the garbage collector by making reusable object or reduce pointless object destruction.

larienna
  • 151
  • 4
  • 12
  • Why fill the `p_data_list` initially at all? If a value for key is not set, then trying to get it will return `null`. That is usually good enough. If you really want to prefill them with empty strings (e.g. to avoid NullPointerExceptions), create one empty String via `String dummy =""` and use this as value for all keys. I guess you are coming from C and are not used to Strings being immutable? – Clemens Klein-Robbenhaar Mar 08 '15 at 16:42
  • It's true that I am less familiar with Java. I tried to avoid dynamic allocation when coding in C at all cost. But Java does not allow this, so it requires some adaptation. I tried not pre-filling the field and it works, I just have to check for null pointer when going through the list. – larienna Mar 11 '15 at 11:29
  • I would not worry too much about performance before being able to measure it. With the "just in time" compiler and the garbage collector under the hood, you are in for quite some surprises anyway, both positive and negative. Save your thoughts for writing good benchmark code, e.g.: http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Clemens Klein-Robbenhaar Mar 11 '15 at 20:17

0 Answers0