Erlang中的大型可变字节数组 [英] Large mutable byte array in Erlang

查看:140
本文介绍了Erlang中的大型可变字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Erlang中编写一个简单的Minecraft服务器应用程序,现在我关心如何有效地存储和修改块数据。
对于那些不了解Minecraft内部的人:我需要在内存中存储多达32kB大小的二进制(100-1000)。直到这一点,Erlang的内置二进制文件就足够了。但服务器必须经常阅读并更改这些二进制文件中的某些字节(通过其id),我不想一直复制它们。

一个很好的功能将被导入,从/到Erlang的标准二进制文件导出。



有没有任何Erlang扩展或数据库,或者我可以用于这个?

解决方案

由于二进制文件是只读的,我可以想到以下几种方法(假设您期望更高的更改率):


  1. 使用叶子中具有相对较小的不可变二进制文件的树状结构。在这种情况下,当您修改数据时,您只需要重新创建小叶子二进制+所有节点到根目录。假设变化是局部的某些位置,我想,你可以从八叉树开始。

  2. 使用大二进制文件+更改列表(可以是简单的列表功能)。当您需要修改世界时,只需在列表中添加新功能即可。当有人要求世界状态时,请使用二进制代码并应用列表中的所有更改。不时地挤压所有变化,并准备一个新的基准状态二进制。这可以与以前的方法(树中具有二进制/更改的叶子)结合使用。

  3. 将可变世界管理移动到外部代码。您可以使用 NIF 端口。我认为这将是最快的方式。此外,我认为实施它会比较容易。第一个版本的API可以像 world:new(X,Y,Z) - > REF(); world:get(Ref,X,Y,Z); world:set(Ref,X,Y,Z,Value);


As I am writing a simple Minecraft server application in Erlang, I am now concerned with the question of how to efficiently store and modify chunk data. For those who don't know about Minecraft's internals: I need to store a lot of binaries (100-1000) of up to 32kB size in memory. Until this point Erlang's builtin binaries are sufficient. But the server has to read and change some bytes (by their id) in these binaries quite often and I don't want to copy them around all the time.
A nice to have feature would be import and export from/to Erlang's standard binaries.

Is there any Erlang extension or database or whatever I could use for this?

解决方案

Since binaries are read-only, I can think of the following approaches (assuming you expect high rate of changes):

  1. Use tree-like structure with relatively small immutable binaries in the leafs. In that case, when you modify you data, you only need to re-create small leaf binary + all nodes up to the root. Assuming that changes are "local" to some position, I think, you can start with octo-tree.
  2. Use "big" binaries + list of changes (that could be as simple list of functions). When you need to modify world, just add new function to the list. When someone asks for the world state, take base binary and apply all changes from the list. From time to time "squash" all changes and prepare a new baseline state binary. This could be combined with previous approach (tree with pairs of binary/changes in the leafs).
  3. Move mutable world management to the external code. You can use NIFs or Ports. I think, that would be the fastest way. Also, I think it would be relatively easy to implement it. The first version of API could be as simple as world:new(X, Y, Z) -> ref(); world:get(Ref, X, Y, Z); world:set(Ref, X, Y, Z, Value);

这篇关于Erlang中的大型可变字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆