<commit_before>/* This file is part of Jiffy, licensed under the MIT License (MIT).
 *
 * Copyright (c) OreCruncher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockartistry.world.chunk.storage;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * This class provides a reusable stream object for storing chunk information.
 * It replaces the classic DataOutputStream that is normally returned by
 * RegionFile and improves on it in the following ways:
 * 
 * + Object pool for reducing pressure on GC and improving performance a tad.
 * 
 * + The deflation compression objects are reused rather than reallocating from
 * the heap. Helps mitigate impacts of memory allocation and GC when
 * realistically they can be long lived objects with tons of reuse.
 * 
 * + The deflation parameters have been adjusted to improve performance with
 * little more data size.
 *
 */
public class ChunkOutputStream extends DataOutputStream {

	private final static Logger logger = LogManager.getLogger("ChunkOutputStream");

	private final static AtomicInteger streamNumber = new AtomicInteger();
	private final static ConcurrentLinkedQueue<ChunkOutputStream> freeOutputStreams = new ConcurrentLinkedQueue<ChunkOutputStream>();

	static ChunkOutputStream getStream(final int chunkX, final int chunkZ, final RegionFile region) {
		ChunkOutputStream buffer = freeOutputStreams.poll();
		if (buffer == null)
			buffer = new ChunkOutputStream();

		return buffer.reset(chunkX, chunkZ, region);
	}

	// Vanilla uses 5 (default) where this uses 4.  Less compression
	// but takes less time.
	private final static int COMPRESSION_LEVEL = 4;
	
	// Use default strategy.  FILTERED doesn't buy anything, and Huffman
	// results in larger stream sizes with more overhead.
	private final static int COMPRESSION_STRATEGY = Deflater.DEFAULT_STRATEGY;
	
	// Set the compression buffer size to match the size of the minimum
	// chunk stream.
	private final static int COMPRESSION_BUFFER_SIZE = RegionFile.SECTOR_SIZE * RegionFile.MIN_SECTORS_PER_CHUNK_STREAM;

	@SuppressWarnings("unused")
	private final int myID = streamNumber.incrementAndGet();
	private final ChunkBuffer myChunkBuffer;
	private final Deflater myDeflater;
	private final DeflaterOutputStream myDeflaterOutput;

	// Time measurement stuff. Intended to work with
	// concurrent ChunkBuffer writes in the case of
	// multiple IO write threads.
	private static boolean DO_TIMINGS = true;
	private static Object sync = new Object();
	private static long bytesWritten;
	private static long rawBytesWritten;<commit_msg>Turn off chunk write stats<commit_after>/* This file is part of Jiffy, licensed under the MIT License (MIT).
 *
 * Copyright (c) OreCruncher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockartistry.world.chunk.storage;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * This class provides a reusable stream object for storing chunk information.
 * It replaces the classic DataOutputStream that is normally returned by
 * RegionFile and improves on it in the following ways:
 * 
 * + Object pool for reducing pressure on GC and improving performance a tad.
 * 
 * + The deflation compression objects are reused rather than reallocating from
 * the heap. Helps mitigate impacts of memory allocation and GC when
 * realistically they can be long lived objects with tons of reuse.
 * 
 * + The deflation parameters have been adjusted to improve performance with
 * little more data size.
 *
 */
public class ChunkOutputStream extends DataOutputStream {

	private final static Logger logger = LogManager.getLogger("ChunkOutputStream");

	private final static AtomicInteger streamNumber = new AtomicInteger();
	private final static ConcurrentLinkedQueue<ChunkOutputStream> freeOutputStreams = new ConcurrentLinkedQueue<ChunkOutputStream>();

	static ChunkOutputStream getStream(final int chunkX, final int chunkZ, final RegionFile region) {
		ChunkOutputStream buffer = freeOutputStreams.poll();
		if (buffer == null)
			buffer = new ChunkOutputStream();

		return buffer.reset(chunkX, chunkZ, region);
	}

	// Vanilla uses 5 (default) where this uses 4.  Less compression
	// but takes less time.
	private final static int COMPRESSION_LEVEL = 4;
	
	// Use default strategy.  FILTERED doesn't buy anything, and Huffman
	// results in larger stream sizes with more overhead.
	private final static int COMPRESSION_STRATEGY = Deflater.DEFAULT_STRATEGY;
	
	// Set the compression buffer size to match the size of the minimum
	// chunk stream.
	private final static int COMPRESSION_BUFFER_SIZE = RegionFile.SECTOR_SIZE * RegionFile.MIN_SECTORS_PER_CHUNK_STREAM;

	@SuppressWarnings("unused")
	private final int myID = streamNumber.incrementAndGet();
	private final ChunkBuffer myChunkBuffer;
	private final Deflater myDeflater;
	private final DeflaterOutputStream myDeflaterOutput;

	// Time measurement stuff. Intended to work with
	// concurrent ChunkBuffer writes in the case of
	// multiple IO write threads.
	private static boolean DO_TIMINGS = false;
	private static Object sync = new Object();
	private static long bytesWritten;
	private static long rawBytesWritten;
{'fraction_non_alphanumeric': 0.0607621009268795, 'fraction_numerical': 0.0012873326467559218, 'mean_word_length': 4.691575091575092, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274471', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1911, 'n_words': 907}