erlang otp包里的 cryto很好的贯彻了下面的原则,是个很好的教材:
9 Drivers
This chapter provides a (very) brief overview on how to write efficient drivers. It is assumed that you already have a good understanding of drivers.
9.1 Drivers and concurrency
The run-time system will always take a lock before running any code in a driver.
By default, that lock will be at the driver level, meaning that if several ports has been opened to the same driver, only code for one port at the same time can be running.
A driver can be configured to instead have one lock for each port.
If a driver is used in a functional way (i.e. it holds no state, but only does some heavy calculation and returns a result), several ports with registered names can be opened beforehand and the port to be used can be chosen based on the scheduler ID like this:
-define(PORT_NAMES(),
{some_driver_01, some_driver_02, some_driver_03, some_driver_04,
some_driver_05, some_driver_06, some_driver_07, some_driver_08,
some_driver_09, some_driver_10, some_driver_11, some_driver_12,
some_driver_13, some_driver_14, some_driver_15, some_driver_16}).
client_port() ->
element(erlang:system_info(scheduler_id) rem tuple_size(?PORT_NAMES()) + 1,
?PORT_NAMES()).
As long as there are no more than 16 schedulers, there will never be any lock contention on the port lock for the driver.
9.2 Avoiding copying of binaries when calling a driver
There are basically two ways to avoid copying a binary that is sent to a driver.
If the Data argument for port_control/3 is a binary, the driver will be passed a pointer to the contents of the binary and the binary will not be copied. If the Data argument is an iolist (list of binaries and lists), all binaries in the iolist will be copied.
Therefore, if you want to send both a pre-existing binary and some additional data to a driver without copying the binary, you must call port_control/3 twice; once with the binary and once with the additional data. However, that will only work if there is only one process communicating with the port (because otherwise another process could call the driver in-between the calls).
Another way to avoid copying binaries is to implement an outputv callback (instead of an output callback) in the driver. If a driver has an outputv callback, refc binaries passed in an iolist in the Data argument for port_command/2 will be passed as references to the driver.
9.3 Returning small binaries from a driver
The run-time system can represent binaries up to 64 bytes as heap binaries. They will always be copied when sent in a messages, but they will require less memory if they are not sent to another process and garbage collection is cheaper.
If you know that the binaries you return are always small, you should use driver API calls that do not require a pre-allocated binary, for instance driver_output() or driver_output_term() using the ERL_DRV_BUF2BINARY format, to allow the run-time to construct a heap binary.
9.4 Returning big binaries without copying from a driver
To avoid copying data when a big binary is sent or returned from the driver to an Erlang process, the driver must first allocate the binary and then send it to an Erlang process in some way.
Use driver_alloc_binary() to allocate a binary.
There are several ways to send a binary created with driver_alloc_binary().
•From the control callback, a binary can be returned provided that set_port_control() has been called with the flag value PORT_CONTROL_FLAG_BINARY.
•A single binary can be sent with driver_output_binary().
•Using driver_output_term() or driver_send_term(), a binary can be included in an Erlang term.
分享到:
相关推荐
7. 源码分析:书中可能包含一些实际项目或示例代码,帮助读者深入理解Erlang和OTP的实践应用。 书中的"Manning.Erlang.and.OTP.in.Action.2010.pdf"很可能是该书的电子版,读者可以通过这个PDF文件全面学习Erlang和...
通过阅读书中的内容和实践提供的示例,读者可以掌握Erlang的核心特性,并有能力构建高效、可靠的分布式系统。书中的"book0024.pdf"很可能是该书的电子版,包含了完整的章节和教学材料,供读者深入学习和参考。
niffed 是一个包装器,可让您在 Erlang 驱动程序中使用 nif API。 甚至可以同时加载与驱动程序和 nif 相同的代码。 nif 调用是通过驱动程序控制接口通过简单的调度程序进行的。 这使得将 nif 代码移植到驱动程序中...
5. **高效的数据结构**:Erlang的列表、二元组和散列表等数据结构提供了高效的操作,便于快速处理游戏中的各种数据,如玩家信息、游戏状态等。 6. **OTP(Open Telecom Platform)框架**:OTP提供了一套标准库和...
Erlang and OTP in Action Martin Logan, Eric Merritt, and Richard Carlsson MEAP Began: August 2008 Softbound print: May 2010 (est.) | 500 pages ISBN: 1933988789 Part One: Getting Past Pure Erlang; ...
6. **并行算法**:书中可能包含实例,演示如何使用Erlang实现高效的并行算法,提升计算性能。 《Erlang入门手册》则可能包含以下基础内容: 1. **Erlang语法**:涵盖基本的变量、数据类型(如原子、列表、元组和二...
erlang-24.3.3-1.el9.x86_64.rpm centos
Erlang是一种面向并发的、函数式编程语言,由瑞典电信设备制造商Ericsson开发,用于构建高可用性、...这些资源将帮助你成为一个熟练的Erlang开发者,充分利用其并发特性和强大的容错能力来构建高效、可靠的软件系统。
3. **内存管理**:Erlang虚拟机(BEAM)具有高效的内存管理,有助于RabbitMQ处理大量短生命周期的消息。 4. **并发处理**:Erlang进程的并发能力使得RabbitMQ能高效地处理来自多个生产者和消费者的并发请求。 5. *...
Erlang:RabbitMQ 是用 Erlang 编写的,因此需要 Erlang 运行时。确保安装了兼容的 Erlang 版本;Erlang:RabbitMQ 是用 Erlang 编写的,因此需要 Erlang 运行时。确保安装了兼容的 Erlang 版本;Erlang:RabbitMQ ...
在Erlang编程语言中,进程是其核心特性之一,它们是并发执行的实体,类似于其他语言中的线程。在Erlang中,进程间通信(IPC)是通过消息传递来实现的,而`link`机制是这个通信模型中非常重要的一部分。本教程将通过...
https://github.com/rabbitmq/erlang-rpm.git源码在centos6.8x64系统下编译的rpm文件
erlang-23.1-1.el8.x86_ ,erlang官网下载很慢,所以提供该下载链接。
GraphQL-Erlang教程该存储库包含有关graphql-erlang系统的教程。 它实现了SWAPI(的子集)作为示例项目,以阐明应如何在完整实现中使用该系统。 这个想法是,它可以用作您自己的GraphQL模式实现的起点。文献资料该...
`mongodb-erlang`是一个用于Erlang环境的MongoDB驱动程序,它允许Erlang应用程序与MongoDB数据库进行交互,提供了丰富的接口来执行各种数据库操作。 一、MongoDB-Erlang驱动程序介绍 `mongodb-erlang`驱动是Erlang...
1. **Erlang MySQL 驱动**:Erlang MySQL 驱动是连接 MySQL 数据库的关键组件,它实现了 Erlang 与 MySQL 之间的通信协议。例如,`mysql_client` 或 `emysql` 是两个流行的 Erlang MySQL 驱动。这些驱动提供了 API,...
### Erlang深度分析知识点概述 #### 1. Erlang虚拟机(VM)分析 - **概念**: Erlang VM,也称为BEAM (Bytecode for the Erlang Abstract Machine),是Erlang语言的...- **实现**: 通过`erlang:open_port/2`函数创建一个...
**Erlang与二进制数据处理** Erlang是一种函数式编程语言,以其在并发、分布式计算和容错领域的强大能力而闻名。在Erlang中,二进制数据是一种重要的数据类型,广泛用于处理网络协议、文件读写、序列化等场景。二...
1. **事件驱动**:Nitrogen基于Erlang的进程模型,实现了事件驱动的编程模式,允许高效处理用户交互和异步操作。 2. **组件库**:包含了一系列预定义的Web组件,如按钮、文本框、表格等,方便快速构建用户界面。 3...