dungba88 commented on code in PR #12624: URL: https://github.com/apache/lucene/pull/12624#discussion_r1407526003
########## lucene/core/src/java/org/apache/lucene/util/fst/ReadWriteDataOutput.java: ########## @@ -0,0 +1,76 @@ +/* + * 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.lucene.util.fst; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.lucene.store.ByteBuffersDataInput; +import org.apache.lucene.store.ByteBuffersDataOutput; +import org.apache.lucene.store.DataOutput; + +/** + * An adapter class to use {@link ByteBuffersDataOutput} as a {@link FSTReader}. It allows the FST + * to be readable immediately after writing + */ +final class ReadWriteDataOutput extends DataOutput implements FSTReader, Freezable { + + private final ByteBuffersDataOutput dataOutput; + private ByteBuffersDataInput dataInput; + private List<ByteBuffer> byteBuffers; + + public ReadWriteDataOutput(ByteBuffersDataOutput dataOutput) { + this.dataOutput = dataOutput; + } + + @Override + public void writeByte(byte b) { + dataOutput.writeByte(b); + } + + @Override + public void writeBytes(byte[] b, int offset, int length) { + dataOutput.writeBytes(b, offset, length); + } + + @Override + public long ramBytesUsed() { + return dataOutput.ramBytesUsed(); + } + + @Override + public void freeze() { + // these operations are costly, so we want to compute it once and cache + byteBuffers = dataOutput.toWriteableBufferList(); Review Comment: I tried several ways to optimize the `getReverseBytesReader()` method, such as bypassing all `asReadOnlyBuffer()` and accessing the byte array directly, but they wouldn't come close compared to BytesStore: - The `ByteBuffersDataOutput.blocks` is a Deque and thus not random accessible. We need to copy to a list, and this takes time as the number of blocks grow. - It took ~15 seconds to verify (`Util.get()`) 100K times, compared to only ~2 seconds with BytesStore. - I also tried to use PagedBytes, but PagedBytes requires a call to freeze() before getting the DataInput anyway. Hence I had to re-add `Freezable()` here, so that the costly operations will only be called once. The alternative way is to cache, but `getReverseBytesReader()` can be called concurrently hence we need some kind of ConcurrentHashMap with only 2 items, which is a weird way to cache. Another way that is not so efficient as Freezable, but avoid the added interface is to simply use a volatile variable and have a simple null check (the volatile is there to enforce a write barrier). ``` private volatile ByteBuffersDataInput dataInput; private volatile List<ByteBuffer> byteBuffers; public FST.BytesReader getReverseBytesReader() { if (byteBuffers == null) { byteBuffers = dataOutput.toWriteableBufferList(); } if (dataInput == null) { dataInput = new ByteBuffersDataInput(byteBuffers); } // use the variables } ``` -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org