Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[investigation] possible load optimization: use Seekable/Parallel reading #36

Open
sszuev opened this issue Apr 1, 2022 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@sszuev
Copy link
Contributor

sszuev commented Apr 1, 2022

In case the serialization format (lang) is not provided we have to iterate all supported formats to find the best suitable.
This peace of code can be optimized using file rewinding or parallel running.
To compare:

Standard sequence reading

    private static void loadStd(Path p, int n, long lines) throws IOException {
        int step = n / 100;
        for (int i = 0; i < n; i++) {
            if (i % step == 0) {
                System.out.print("#");
            }
            try (BufferedReader r = Files.newBufferedReader(p)) {
                if (lines != r.lines().count()) {
                    throw new IllegalStateException();
                }
            }
        }
        System.out.println();
    }

vs SeekableByteChannel reading

    private static void loadNio(Path p, int n, long lines) throws IOException {
        int step = n / 100;
        try (SeekableByteChannel chanel = Files.newByteChannel(p, StandardOpenOption.READ)) {
            for (int i = 0; i < n; i++) {
                if (i % step == 0) {
                    System.out.print("#");
                }
                BufferedReader r = new BufferedReader(Channels.newReader(chanel, StandardCharsets.UTF_8));
                if (lines != r.lines().count()) {
                    throw new IllegalStateException();
                }
                chanel.position(0);
            }
        }
        System.out.println();
    }

vs concurrent reading

    private static void loadParallel(Path p, int n, long lines) throws ExecutionException, InterruptedException {
        System.out.println("Go");
        ExecutorService service = Executors.newWorkStealingPool();
        CompletableFuture<?>[] futures = IntStream.range(0, n).mapToObj(x -> CompletableFuture.runAsync(() -> {
            try (BufferedReader r = Files.newBufferedReader(p)) {
                if (lines != r.lines().count()) {
                    throw new IllegalStateException();
                }
                System.out.println(Thread.currentThread() + " is done.");
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }, service)).toArray(CompletableFuture[]::new);
        CompletableFuture.allOf(futures).get();
        System.out.println("Done");
    }
@sszuev sszuev added the enhancement New feature or request label Apr 1, 2022
@sszuev sszuev changed the title load optimization: use SeekableInputStream if possible load optimization: use Seekable/Parallel reading if possible Apr 9, 2022
@sszuev sszuev changed the title load optimization: use Seekable/Parallel reading if possible [investigation] possible load optimization: use Seekable/Parallel reading Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant