为什么Lisp的cons速度慢? [英] Why is consing in Lisp slow?

查看:156
本文介绍了为什么Lisp的cons速度慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在《 On Lisp》一书中读到,应该避免在扩展宏的主体中过度使用cons.为什么将cons视为效率低下的操作? Lisp不会与cons单元共享结构吗?

I read in the book 'On Lisp' that one should avoid excessive use of cons in the body of expanded macros. Why is cons considered to be an inefficient operation? Does Lisp not do structure sharing with the cons cells?

推荐答案

您需要首先了解行话.

CONS 是创建新的cons单元格的原始操作.

CONS is a primitive operation to create a fresh cons cell.

Consing 意味着通常在堆上分配内存,而不仅仅是cons单元:数字的Consing,数组的Consing,CLOS对象的Cons .........

Consing means allocation of memory on the heap in general, not only cons cells: Consing of numbers, Consing of arrays, Consing of CLOS objects, ...

内存管理词汇表"中的定义是:

The definition from the 'memory management glossary' says:

  1. cons (1)在Lisp中,cons是创建列表元素的原始操作(英文为"CONStruct").通过扩展,缺点是创建的元素.
  2. 缺点(2)(有关完整详细信息,请参阅分配),分配过程就是 资源分配.什么时候 程序要求 应用程序内存管理器或 分配器分配一个块 该程序要存储的memory(2) 其数据输入.分配也 称为consing,来自 cons (1).
  1. cons(1) In Lisp, cons is a primitive operation creating a list element (from English "CONStruct"). By extension, a cons is the element created.
  2. cons(2) (for full details, see allocate) Allocation is the process of assigning resources. When requested to by the program, an application memory manager or allocator allocates a block of memory(2) for the program to store its data in. Allocation is also known as consing, from cons(1).

所以,格雷厄姆使用了第二个含义.

So, Graham uses the second meaning.

如果格雷厄姆说避免特别注意.不必要的实用程序会破坏原本高效的程序的性能." -这意味着:避免在堆上分配不必要的内存.但这对于任何代码都是正确的-宏,函数等等.

If Graham says 'Avoid consing especially. A utility which conses unnecessarily can ruin the performance of an otherwise efficient program.' - that means: avoid unnecessary memory allocation on the heap. But this can be true for any code - macro, function, whatever.

当计算机的内存较少且垃圾回收的成本更高时(尤其是当它需要扫描物理内存小于虚拟内存的虚拟内存系统中的换出内存时),情况尤其如此.

That was particular true when computers had less memory and Garbage Collection was more costly (especially when it needed to scan swapped out memory in virtual memory systems, where the physical memory is smaller than the virtual memory).

今天,精打细算已不再是问题,但仍然可以解决问题.

Today consing is less an issue, but can still be relevant.

以一个函数READ-LINE为例,它从流中读取一行并包含"一个新字符串.请注意,该字符串并不是出于顾虑而构建的,而是一个向量.我们的意思是在这里在堆上分配一个字符串".如果您有一个很大的文件,其中包含许多行,那么拥有一个缓冲区并用行字符填充缓冲区的例程会更快(Common Lisp中的矢量都有填充指针).这样,行缓冲区只是一个对象,可以潜在地重用于对该行读取函数的调用.

Take for example a the function READ-LINE, it reads a line from a stream and 'conses' a new string. Note that the string is not built out of conses, but it is a vector. We mean here 'allocates a string on the heap'. If you have a large file with lots of lines, it can be faster to have a routine that gets a buffer and which fills the buffer with the line characters (there are vectors in Common Lisp with a fill pointer for this). That way the line buffer is just one object and can potentially be reused for calls to this line reading function.

请参阅Allegro CL文档中的以下示例:一些减少锥度的技术处理非常大的文本文件时.

See this example in the Allegro CL documentation: Some techniques for reducing consing when processing very large text files.

这篇关于为什么Lisp的cons速度慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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