I'm creating a trie, which my application will hold in it memory. Trie will have a lot of nodes and I'm thinking about how to reduce space usage. Of cause I will use trie to DAWG algorithm to reduse number of nodes, but as far as I know it's not enough.
Here is a node class
class Node{
char letter;
boolean EOW; // end of word
Node child; // first child
Node next; // next Node on this level
}
As far as I know object of this class will have 14 Bytes (2 Bytes given for char, 4 for boolean variable and 2*4 will be reserved for references)
I think that I can replace char by byte. That will save 1 Byte. However I don't know how much time that will take in type casting. And likely this is a bad desigin.
Also boolean takes 4 Bytes, perhaps you know what I can use instead of boolean?
So I need you to help me reduce size of nodes. Thanks in advance.
If letter
takes only 5 bits and eow
one bit, you can pack them in a single byte
to save memory.
char letter = ...;
boolean eow = ...;
byte packed = (byte) ((eow ? 0b10_0000 : 0) | letter);
letter = (char) (packed & 0b1_1111);
eow = (packed & 0b10_0000) != 0;
If you don't need the weirder half of UTF-16 characters, you can use the highest bit of letter
as the EOW marker.
For example, here the eoWletterA
variable has the letter 'a' encoded with the EOW bit:
char eoWletterA = 'a' + 0x8000;
char letter = (char) (eoWletterA & 0x7FFF);
boolean eow = BigInteger.valueOf(eoWletterA).testBit(15);
Your trie should be encapsulated properly. Make sure that the EOW bit can't be accidentally set when storing a character to the trie.
UPDATE: Note that removing the boolean
variable from the Node may or may not make a difference in the memory footprint of a Node object in a JVM. You can examine the object memory footprint with the following tool: https://stackoverflow.com/a/52682/1207523
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.