bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

Performance for loading very long games #46

Closed dlbbld closed 3 years ago

dlbbld commented 3 years ago

When loading a PGN with 250 moves, this takes a second. However, when loading a PGN with 9'000 moves, this goes off. The time it takes more is not in proportion to the additional moves and is much too long. Please see the attached PGN (I adapted the second for import).

Of course a game with 9'000 moves is not realistic, but this long time is unexpected. I hope this observation helps to improve your API.

for (final String fileName : Arrays.asList("nikolic_arsovic_1989.pgn", "longest.pgn")) {
  System.out.printf("Processing %s%n", fileName);
  final PgnHolder pgn = new PgnHolder("C:\\temp\\test\\" + fileName);

  final long msBefore = System.currentTimeMillis();
  pgn.loadPgn();
  final Game game = pgn.getGames().get(0);
  game.loadMoveText();
  final long msDuration = System.currentTimeMillis() - msBefore;
  System.out.printf("Loading duration seconds: %f%n", msDuration / 1000.0);
  System.out.printf("Moves: %s%n", game.getHalfMoves().size() / 2.0);
}

Output is

Processing nikolic_arsovic_1989.pgn
Loading duration seconds: 0.494000
Moves: 269.0
Processing longest.pgn
Loading duration seconds: 116.542000
Moves: 8848.5

longest.pgn.txt nikolic_arsovic_1989.pgn.txt

dlbbld commented 3 years ago

Is there some feedback here? I try some simple math: The second games has around 30 times more moves than the first. But to load, it takes over 200 times longer. I am sure there is only some small problem here and this can be much improved.

bhlangonijr commented 3 years ago

I found the issue, will be fixed in the next released.

dlbbld commented 3 years ago

I expect it even to be the other way round - the performance per move to improve with larger files. File stream is high-speed, and file handling overhead counts less in proportion with more moves.

bhlangonijr commented 3 years ago

Fixed in version 1.2.2

dlbbld commented 3 years ago

The performance is right now. But as it happens right in this game, there is a problem with repetition detection. This problem is not due to the performance improvement, which should be fine, but due to #39 still not being fixed and throwing new errors. I'll mention it there.