Random Timouts on Java8 Streams


#1

Hi everyone,

I’m issuing random timouts on java8 streams on the code4life contest.
The lists are max 3 element size.
Does anyone have the same issue? If so is there any solution to fix ?
Im not getting the “Change to C++” or the “Go back to for loop” as serious solutions :smiley:

Thanks


#2

When you have a timeout it doesn't always mean that code is slow, but just that you have not provided anything in time, so double-ckeck that you don't have a branch when no output is printed.


#3

Hi,

I already encountered timeouts with streams and java 8.
I don't know why, but the FIRST execution of a stream can take a long time. I workaround the issue on other contest by simply ensuring the streams are used at least once during the first turn with a dummy code. such as:

public Player(){
List warmup = Arrays.asList("toto", "tata");
warmup.stream().min((s1, s2)-> Integer.compare(s1.length(), s2.length())).get();
}

Hope that helps :slight_smile:


#4

http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html


We can achieve both of these goals by using the invokedynamic feature from JSR 292 to separate the binary representation of lambda creation in the bytecode from the mechanics of evaluating the lambda expression at runtime. Instead of generating bytecode to create the object that implements the lambda expression (such as calling a constructor for an inner class), we describe a recipe for constructing the lambda, and delegate the actual construction to the language runtime.


So looks like lambda is constructed in runtime (during the first call?)


#5

Hi Manwe,

I ended up with the same conclusion as you before seeing your post.
Effectively the first call of a stream can take more tha 50ms.

I put this code as the first instruction for my program before the game loop : Collections.emptyList().stream().count();
The problem has gone.

Thanks.


#6

Thanks guys for sharing this. I've almost decided to switch to C# due to this timeouts, but applying your solution solved the problem.


#7

You don’t have to init your Java streams in the first turn anymore. We’re doing it before now. Don’t ask me the details :smiley:


#8

I would like to know the details !!


#9

The details are easy to guess. Because of this message : A* Craft - Bugs

They simply call an other main than our main. And their main use/preload streams classes.