C 的记忆库?

发布于 2024-11-08 17:56:11 字数 503 浏览 0 评论 0原文

对于我正在从事的项目,有许多状态可以依靠计算返回相同的结果(并且没有副作用)。显而易见的解决方案是对所有昂贵的功能使用记忆化。

我需要有处理多个状态的记忆(这样我就可以使一个缓存集无效而不使另一个缓存集无效)。有谁知道有一个好的 C 库可以处理这类事情吗? (请注意,它不能是 C++,我们说的是 C。)

我在 Python 中使用过一些很好的实现,这些实现使用装饰器能够灵活地记住一堆不同的函数。我有点想知道是否有一个通用库可以用 C 做类似的事情(尽管可能使用显式函数包装而不是方便的语法)。我只是认为,当它是一个足够常见的问题时,必须单独为每个函数添加缓存是愚蠢的,必须有一些现成的解决方案。

我想要的特征如下:

  1. 可以缓存具有各种类型输入和输出的函数
  2. 管理多个不同的缓存(因此您可以拥有短期和长期缓存)
  3. 具有良好的使缓存失效的功能
  4. 旨在由包装函数使用,而不是改变现有的函数

有人知道可以处理所有或大部分这些要求的 C 实现吗?

For a project I'm working on, there are a number of states where calculations can be relied upon to return the same results (and have no side effects). The obvious solution would be to use memoization for all the costly functions.

I would need to have memoization that handles more than one state (so that I could invalidate one cache set without invalidating another). Does anybody know a good C library for this sort of thing? (Note that it can't be C++, we're talking C.)

I've worked with some good implementations in Python that use decorators to be able to flexibly memoize a bunch of different functions. I'm kind of wondering is there's a generic library that could do similar things with C (though probably with explicit function wrapping rather than convenient syntax). I just think it would be silly to have to add caching to each function individually when it's a common enough issue there must be some off-the-shelf solutions for it.

The characteristics I would look for are the following:

  1. Can cache functions with various types of input and output
  2. Manages multiple different caches (so you can have short-term and long term caching)
  3. Has good functions for invalidating caches
  4. Intended to be used by wrapping functions, rather than altering existing functions

Anybody know a C implementation that can handle all or most of these requisites?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白色秋天 2024-11-15 17:56:11

好吧,由于没有 C 的记忆库,而且我正在寻找一个用于记忆代码库中现有 C 函数的嵌入式解决方案,所以我制作了自己的小记忆库,并在 APL 2.0 下发布。希望人们会发现这很有用,并且它不会在其他编译器上崩溃和烧毁。如果确实有问题,请在这里给我发消息,我会在有时间的时候进行调查(可能会以月为增量来衡量)。

这个库并不是为了速度而构建的,但它可以工作并且已经过测试,以确保它使用起来相当简单,并且在我的测试中不会显示任何内存泄漏。从根本上说,这让我可以向类似于我在 Python 中习惯的装饰器模式的函数添加记忆功能。

该库目前在 SourceForge 上作为 C-Memo 库。它附带了一个小用户手册和几个用于通用哈希的第三方许可许可库。如果位置发生变化,我会尝试更新此链接。我发现这对我的项目很有帮助,希望其他人也会发现它对他们的项目有用。

Okay, seeing as there were no memoization libraries for C and I was looking for a drop-in solution for memoizing existing C functions in a code base, I made my own little memoization library that I'm releasing under the APL 2.0. Hopefully people will find this useful and it won't crash and burn on other compilers. If it does have issues, message me here and I'll look into it whenever I have the time (which would probably be measured in increments of months).

This library is not built for speed, but it works and has been tested to make sure it is fairly straightforward to use and doesn't display any memory leaks in my testing. Fundamentally, this lets me add memoization to functions similar to the decorator pattern that I'm used to in Python.

The library is currently on SourceForge as the C-Memo Library. It comes with a little user manual and a couple of 3rd party permissively licensed libraries for generic hashing. If the location changes, I'll try to update this link. I found this helpful in working on my project, hopefully others will find it useful for their projects.

眼藏柔 2024-11-15 17:56:11

记忆化几乎内置于 haskell 语言中。您可以从 c 调用此功能

更新:
我仍在学习函数式编程,但我确实知道记忆化在函数式编程中相当常见,因为语言特性使它变得容易。我正在学习 f#。我不知道 haskell,但它是我所知道的唯一可以与 c 交互的函数式语言。您也许能够找到另一种函数式编程语言,它以比 haskell 提供的更合适的方式与 c 交互。

memoization is all but built into the haskell language. You can call this functionality from c

Update:
I'm still learning about functional programming, but I do know that memoization is fairly common in functional programming becuase the language features make it easy. I'm learning f#. I don't know haskell, but it is the only functional language I know of that will interact with c. You might be able to find another functional programming language that interfaces with c in a more suitable fashion than what haskell provides.

陈独秀 2024-11-15 17:56:11

为什么不能是 C++?

只是作为起点,看看这个记忆函数:

声明:

template<typename T, typename F>
auto Memoize(T key, F function) {
  static T memory_key = key;
  static auto memory = function(memory_key);
  if (memory_key != key) {
    memory_key = key;
    memory = function(memory_key);
  }

  return memory;
}

用法示例:

auto index = Memoize(value, IndexByLetter);

Why, just can't be C++?

Just for a starting point look to this memoization function:

declaration:

template<typename T, typename F>
auto Memoize(T key, F function) {
  static T memory_key = key;
  static auto memory = function(memory_key);
  if (memory_key != key) {
    memory_key = key;
    memory = function(memory_key);
  }

  return memory;
}

Usage example:

auto index = Memoize(value, IndexByLetter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文