Multidimensional byte arrays with LinkedHashMap ... Is there a better way?

I am VERY new to Java programming, so please forgive my newbies :).

I use LinkedHashMap as a file cache for an application that I modify to facilitate development. I do this to reduce I / O overhead and therefore improve performance. The problem is that overhead is introduced in many other ways.

The corresponding source is as follows.


// Retrieve Data From LinkedHashMap byte grid[][][] = null; if(file.exists()) { if (!cache.containsKey(file)) { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis, 16384); ObjectInputStream ois = new ObjectInputStream(bis); cache.put(file, ois.readObject()); ois.close(); } grid = (byte[][][]) cache.get(file); } else { grid = new byte[8][8][]; } 

The following is what I use to save data. The data loading method is the exact opposite.


 ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(baos){{ def.setLevel(2);}}; BufferedOutputStream bos = new BufferedOutputStream(gos, 16384); DataOutputStream dos = new DataOutputStream(bos); // Some code writes to dos dos.close(); byte[cx][cz] = baos.toByteArray(); baos.close(); cache.put(file, grid); 

And here is the ad for the cache.


 private static LinkedHashMap<File, Object> cache = new LinkedHashMap<File, Object>(64, 1.1f, true) {protected boolean removeEldestEntry(Map.Entry<File, Object> eldest) { return size() > 64; } } 

Since I am very unfamiliar with the Java thread label, it is very likely that the code above looks sloppy. I am also sure that there are more efficient ways to do higher, for example, where to put the buffer.

In any case, my main problem is this: whenever I need to do something with one port, I need to convert all the grid data into an object, send it to the cache and write the file. This is a very inefficient way of doing things. I would like to know if there is a better way to do this, so I don't need to get (); the entire array of bytes [8] [8] [], when I need only access to this fragment. I would like to do something like chunk = cache.get [cx] cz, but I'm sure it is not so simple.

In any case, as I said earlier, please excuse the question, if the answer is obvious, I'm just a humble beginner: D. I am very grateful for any input :).

Thanks.

+4
source share
2 answers

If your goal is to reduce I / O overhead, how do I place a byte[][][] object in a wrapper object that adds the concept of a dirty flag?

Thus, you can reduce the number of files written after the change by only writing dirty objects to disk when you do this using the cache, or you are going to delete the old object when pasted into the full cache.

+1
source

I would start by creating a new class - name it ByteMatrix3D - for storing data. And instead of using byte[][][] , I would use a one-dimensional array with calculated offsets (for example, in an 8x8x8 array, the offset [1][2][3] could be calculated as 1 * 64 + 2 * 8 + 3 . This change will eliminate very little object overhead, and also allow you to make additional changes without affecting the higher-level code.

And the first change I would make would be to use a MappedByteBuffer to access files. This will allow the operating system to manage the actual data and make reading and writing transparent to the program.

0
source

Source: https://habr.com/ru/post/1332948/


All Articles