[ 
https://issues.apache.org/jira/browse/IO-726?focusedWorklogId=607842&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-607842
 ]

ASF GitHub Bot logged work on IO-726:
-------------------------------------

                Author: ASF GitHub Bot
            Created on: 07/Jun/21 12:58
            Start Date: 07/Jun/21 12:58
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on a change in pull request #215:
URL: https://github.com/apache/commons-io/pull/215#discussion_r646560785



##########
File path: src/main/java/org/apache/commons/io/ByteBufferCleaner.java
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+/**
+ * Utility to manually clean a direct {@link ByteBuffer}. Without manual

Review comment:
       Move this class to the input package and make it package private to 
reduce the public API surface, we can make it public later.

##########
File path: 
src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import static org.apache.commons.io.IOUtils.EOF;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.ByteBufferCleaner;
+
+/**
+ * An {@link InputStream} that utilizes memory mapped files to improve
+ * performance. A sliding window of the file is mapped to memory to avoid
+ * mapping the entire file to memory at one time. The size of the sliding 
buffer
+ * is user configurable.
+ * 
+ * For most operating systems, mapping a file into memory is more expensive 
than
+ * reading or writing a few tens of kilobytes of data. From the standpoint of
+ * performance it is generally only worth mapping relatively large files into
+ * memory. Use of this class can provide approximately a 25% increase in
+ * throughput when reading large files. <br>
+ * Note: Use of this class does not necessarily obviate the need to use a
+ * {@link BufferedInputStream}. Depending on the use case, the use of buffering
+ * may still further improve performance. For example:
+ * 
+ * <pre>
+ * new BufferedInputStream(new GzipInputStream(new 
MemoryMappedFileInputStream(path))))
+ * </pre>
+ * 
+ * will greatly outperform:
+ * 
+ * <pre>
+ * new GzipInputStream(new MemoryMappedFileInputStream(path))

Review comment:
       This comparison does not make sense. You should compare using this class 
with a code fragment that does not use it.

##########
File path: 
src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import static org.apache.commons.io.IOUtils.EOF;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.ByteBufferCleaner;
+
+/**
+ * An {@link InputStream} that utilizes memory mapped files to improve
+ * performance. A sliding window of the file is mapped to memory to avoid
+ * mapping the entire file to memory at one time. The size of the sliding 
buffer
+ * is user configurable.
+ * 
+ * For most operating systems, mapping a file into memory is more expensive 
than
+ * reading or writing a few tens of kilobytes of data. From the standpoint of
+ * performance it is generally only worth mapping relatively large files into
+ * memory. Use of this class can provide approximately a 25% increase in
+ * throughput when reading large files. <br>

Review comment:
       I would not make any performance claims without backing it up with 
evidwnce like a test anyone can run on their combination of OS/JRE/file. IOW, 
provide a test where the user can specify the file size in a sys prop or 
command line for example.

##########
File path: 
src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import static org.apache.commons.io.IOUtils.EOF;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.ByteBufferCleaner;
+
+/**
+ * An {@link InputStream} that utilizes memory mapped files to improve
+ * performance. A sliding window of the file is mapped to memory to avoid
+ * mapping the entire file to memory at one time. The size of the sliding 
buffer
+ * is user configurable.
+ * 
+ * For most operating systems, mapping a file into memory is more expensive 
than
+ * reading or writing a few tens of kilobytes of data. From the standpoint of
+ * performance it is generally only worth mapping relatively large files into
+ * memory. Use of this class can provide approximately a 25% increase in
+ * throughput when reading large files. <br>
+ * Note: Use of this class does not necessarily obviate the need to use a
+ * {@link BufferedInputStream}. Depending on the use case, the use of buffering
+ * may still further improve performance. For example:
+ * 
+ * <pre>
+ * new BufferedInputStream(new GzipInputStream(new 
MemoryMappedFileInputStream(path))))
+ * </pre>
+ * 
+ * will greatly outperform:
+ * 
+ * <pre>
+ * new GzipInputStream(new MemoryMappedFileInputStream(path))
+ * </pre>
+ * 
+ * @since 2.9.0
+ */

Review comment:
       We are up to 2.10.0 now 😉

##########
File path: src/test/java/org/apache/commons/io/ByteBufferCleanerTest.java
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.nio.ByteBuffer;
+
+import org.apache.commons.lang3.RandomUtils;
+import org.junit.jupiter.api.Test;
+
+class ByteBufferCleanerTest {

Review comment:
       The style of this component is to make test classes and methods public.

##########
File path: 
src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import static org.apache.commons.io.IOUtils.EOF;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.ByteBufferCleaner;
+
+/**
+ * An {@link InputStream} that utilizes memory mapped files to improve
+ * performance. A sliding window of the file is mapped to memory to avoid
+ * mapping the entire file to memory at one time. The size of the sliding 
buffer
+ * is user configurable.
+ * 
+ * For most operating systems, mapping a file into memory is more expensive 
than
+ * reading or writing a few tens of kilobytes of data. From the standpoint of
+ * performance it is generally only worth mapping relatively large files into
+ * memory. Use of this class can provide approximately a 25% increase in
+ * throughput when reading large files. <br>

Review comment:
       Close all HTML tags. You might as well use a paragraph here.
   

##########
File path: 
src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.input;
+
+import static org.apache.commons.io.IOUtils.EOF;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.io.ByteBufferCleaner;
+
+/**
+ * An {@link InputStream} that utilizes memory mapped files to improve
+ * performance. A sliding window of the file is mapped to memory to avoid
+ * mapping the entire file to memory at one time. The size of the sliding 
buffer
+ * is user configurable.
+ * 
+ * For most operating systems, mapping a file into memory is more expensive 
than
+ * reading or writing a few tens of kilobytes of data. From the standpoint of
+ * performance it is generally only worth mapping relatively large files into
+ * memory. Use of this class can provide approximately a 25% increase in
+ * throughput when reading large files. <br>
+ * Note: Use of this class does not necessarily obviate the need to use a
+ * {@link BufferedInputStream}. Depending on the use case, the use of buffering
+ * may still further improve performance. For example:
+ * 
+ * <pre>
+ * new BufferedInputStream(new GzipInputStream(new 
MemoryMappedFileInputStream(path))))
+ * </pre>
+ * 
+ * will greatly outperform:
+ * 
+ * <pre>
+ * new GzipInputStream(new MemoryMappedFileInputStream(path))
+ * </pre>
+ * 
+ * @since 2.9.0
+ */
+public class MemoryMappedFileInputStream extends InputStream {
+    /**
+     * Default size of the sliding memory mapped buffer. We use 256K, equal to 
65536
+     * pages (given a 4K page size). Increasing the value beyond the default 
size
+     * will generally not provide any increase in throughput.
+     */
+    private static final int DEFAULT_BUFFER_SIZE = 256 * 1024;
+    private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new 
byte[0]).asReadOnlyBuffer();
+    private static final boolean IS_CLEANING_SUPPORTED = 
ByteBufferCleaner.isSupported();
+    private final int bufferSize;
+    private final FileChannel channel;
+    private ByteBuffer buffer = EMPTY_BUFFER;
+    private boolean closed = false;
+    /**
+     * The starting position (within the file) of the next sliding buffer.
+     */
+    private long nextBufferPosition = 0;
+
+    public MemoryMappedFileInputStream(final Path file) throws IOException {
+        this(file, DEFAULT_BUFFER_SIZE);
+    }
+
+    public MemoryMappedFileInputStream(final Path file, final int bufferSize) 
throws IOException {
+        this.bufferSize = bufferSize;
+        this.channel = FileChannel.open(file, StandardOpenOption.READ);
+    }
+
+    @Override
+    public int read() throws IOException {
+        ensureOpen();
+        if (!buffer.hasRemaining()) {
+            nextBuffer();
+            if (!buffer.hasRemaining()) {
+                return EOF;
+            }
+        }
+        return Short.toUnsignedInt(buffer.get());
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {

Review comment:
       Use final where you can.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 607842)
    Time Spent: 2h 10m  (was: 2h)

> Add MemoryMappedInputStream
> ---------------------------
>
>                 Key: IO-726
>                 URL: https://issues.apache.org/jira/browse/IO-726
>             Project: Commons IO
>          Issue Type: New Feature
>          Components: Streams/Writers
>    Affects Versions: 2.8.0
>            Reporter: S Hollander
>            Priority: Major
>             Fix For: 2.10.0
>
>          Time Spent: 2h 10m
>  Remaining Estimate: 0h
>
> A very useful tool for improving performance with disk-based IO is the use of 
> memory mapped files. I propose an {{InputStream}} that provides a bridge 
> between memory mapped files and the {{InputStream}} ecosystem.
> In my limited performance tests, the use of the MemoryMappedInputStream 
> provided a 25% increase in throughput.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to