Question

Is there any way to create Map or Set type data structures at compile time?

The reason I ask this is because I'm working with App Engine and I have some data structures that need to be sorted and searched, but they're constant, so I'd like to avoid the time cost of creating them at start-up.

Was it helpful?

Solution

As with any kind of performance optimization, the first thing to ask yourself is whether the time taken to set up these structures at run time is really affecting performance. How big area your structures? How long do they take to set up? If you haven't measured this you are engaging in premature optimization, which as we know is the root of all evil.

Assuming you have done this, then let's look at the options. How much time can you really save? Your best bet is to use some form of serialization, but you are going to have to write that yourself; even if you define a file format to hold the content, the file is going to have to be parsed, and the in-memory data structures will have to be created. That's going to take time, and it's unlikely to be substantially faster than just creating the Maps or Sets and populating them. In some languages you could theoretically save the bit pattern of the memory for these structures, but even if you can do that you are making yourself vulnerable to any little change in compiler version, and any errors you make will be virtually impossible to debug.

In short, don't do this unless you are sure you need to. Even then, you probably shouldn't do this. The only time you should is if the (probably very small) saving of time will absolutely mean the difference between success and failure of your project.

OTHER TIPS

If you're using Spring you can create and populate those data structures in configuration and cache them, but I don't see how you can avoid creating and populating them at runtime. You have to run that code sometime. Sounds like pre-mature optimization to me.

At compile time I don't think its possible. At run time it is possible. Your best solution is to use the app engine and maybe use the cron job trick to keep it hot cron job link

Then you can define the list in a static block of code which will cache on first run of the application

e.g

private static List<Object> list = new ArrayList<Object>();

static {
    for(Value value : getValues()){
        list.add(value);
    }
}

Also see spring annotations for post construct so you can do the same but in a non static environment by annotating a method with @PostConstruct which will be executed when your object is created.

No; creation of ALL your objects always happens at runtime.

That being said, you can declare and initialize Sets together like this:

private static Set<String> set = new HashSet<String>(Arrays.asList("abc", "def"));

This will still be executed at runtime though.

To handle Maps, you'd have to use a static initializer instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top