From 31518e2cbd8df024c6bb6a5dca33f4f6280be7b9 Mon Sep 17 00:00:00 2001 From: Eric Ihli Date: Sat, 19 Dec 2020 14:18:59 -0800 Subject: [PATCH] Add notes for efficient trie --- src/com/owoga/prhyme/data/efficient.org | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/com/owoga/prhyme/data/efficient.org diff --git a/src/com/owoga/prhyme/data/efficient.org b/src/com/owoga/prhyme/data/efficient.org new file mode 100644 index 0000000..5e5a797 --- /dev/null +++ b/src/com/owoga/prhyme/data/efficient.org @@ -0,0 +1,32 @@ +#+TITLE: Efficiency + +https://www.evanjones.ca/software/java-bytebuffers.html + +You want to allocate a single direct ByteBuffer, and reuse it for all I/O to and +from a particular channel. However, you should serialize and deserialize your +data using byte[] arrays, since accessing individual elements from a ByteBuffer +is slow. + +https://www.novixys.com/blog/java-nio-using-bytebuffer/ + + +https://stackoverflow.com/questions/5210840/when-to-use-byte-array-when-byte-buffer + +There are actually a number of ways to work with bytes. And I agree that it's not always easy to pick the best one: + + the byte[] + the java.nio.ByteBuffer + the java.io.ByteArrayOutputStream (in combination with other streams) + the java.util.BitSet + +The byte[] is just a primitive array, just containing the raw data. So, it does not have convenient methods for building or manipulating the content. + +A ByteBuffer is more like a builder. It creates a byte[]. Unlike arrays, it has more convenient helper methods. (e.g. the append(byte) method). It's not that straightforward in terms of usage. (Most tutorials are way too complicated or of poor quality, but this one will get you somewhere. Take it one step further? then read about the many pitfalls.) + +You could be tempted to say that a ByteBuffer does to byte[], what a StringBuilder does for String. But there is a specific difference/shortcoming of the ByteBuffer class. Just like with arrays, the ByteBuffer has a fixed size. So, when you instantiate it, you already have to specify the size of the buffer. + +That's one of the reasons, why I often prefer to use the ByteArrayOutputStream because it automatically resizes, just like an ArrayList does. (It has a toByteArray() method). Sometimes it's practical, to wrap it in a DataOutputStream. The advantage is that you will have some additional convenience calls, (e.g. writeShort(int) if you need to write 2 bytes.) + +BitSet comes in handy when you want to perform bit-level operations. You can get/set individual bits, and it has logical operator methods like xor(). (The toByteArray() method was only introduced in java 7.) + +Of course depending on your needs you can combine all of them to build your byte[].