说出该技术的名称(可能称为“搭载”)

发布于 2024-08-24 02:44:27 字数 595 浏览 6 评论 0原文

以下方法/技术的名称是什么(我将尽力描述,可能需要“记忆”背景才能理解为什么该技术非常有用)

:一些可能很长的异步计算,并且您意识到相同的计算已经开始,但尚未完成,并且您“搭载”了第一个计算。然后,当第一次计算结束时,它发出的不是一个而是两个回调。

目标是不要不必要地启动第二次计算,因为您知道已经有一个相同的计算正在运行。

请注意,尽管并非完全不同,但我并不是在寻找缓存的特殊情况,“记忆化”是:记忆化是当您开始计算并找到已经完成的相同计算的缓存(记忆化)结果时 您可以重复使用。

在这里,我正在寻找与记忆化有点相似的技术名称(因为它可以出于某些与记忆化是有用技术相同的原因而有用),只不过它重用了第一个计算即使在您发出第二个计算时第一个计算尚未完成

我一直将这种技术称为“搭载”,但我不知道这是否正确。

实际上,我不止一次地使用过它作为某种“类固醇记忆”,而且它非常方便。

我只是不知道这种(高级?)技术的名称是什么。

编辑

该死,我想对 epatel 的答案发表评论,但它消失了。 epatel 的回答给了我一个想法,这种技术可以称为“惰性记忆”:)

What is the name of the following method/technique (I'll try to describe the best I could, background on "memoization" is probably needed to understand why this technique can be very useful):

You start some potentially lenghty asynchronous computation and you realize that an identical computation has already been started but is not done yet and you "piggyback" on the first computation. Then when the first computation ends, it issues not one but two callbacks.

The goal is to not needlessly start a second computation because you know that there's already an identical computation running.

Note that altough not entirely dissimilar, I'm not looking for the particular case of caching that "memoization" is: memoization is when you start a computation and find a cached (memoized) result of that same computation that is already done that you can reuse.

Here I'm looking for the name of the technique that is in a way a bit similar to memoization (in that it is can be useful for some of the same reasons that memoization is a useful technique), except that it reuses the result of the first computation even if the first computation is not done yet at the time you issue the second computation.

I've always called that technique "piggybacking" but I don't know if this is correct.

I've actually used this more than once as some kind of "memoization on steroids" and it came very handy.

I just don't know what the name of this (advanced ?) technique is.

EDIT

Damn, I wanted to comment on epatel's answer but it disappeared. epatel's answer gave me an idea, this technique could be called "lazy memoization" :)

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

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

发布评论

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

评论(4

幸福丶如此 2024-08-31 02:44:27

这只是期货的记忆

正常的“热切”记忆是这样工作的:

f_memo(x):
  critical_section:
    if (exists answers(f,x))
      return answers(f,x)
    else
      a = f(x)
      answers(f,x) = a
      return a

现在,如果 f(x) 返回 futures 而不是实际结果,则上面的代码按原样工作。你会得到捎带效应,即像这样:

  1. 第一个线程调用 f(3)
  2. 没有存储 f(3) 的答案,因此在关键部分有对 f(3) 的调用。 f(3) 被实现为返回一个 future,因此“答案”立即准备就绪;上面代码中的“a”被设置为未来的 F 并且未来的 F 存储在答案表中
  3. 未来的 F 作为调用 f(3) 的“结果”返回,这可能仍在进行中< /strong>
  4. 另一个线程调用 f(3)
  5. 从表中找到未来的 F,并立即返回
  6. 现在两个线程都掌握了计算结果;当他们尝试读取它时,他们会阻塞,直到计算准备好为止——在帖子中提到这种通信机制是通过回调实现的,大概是在 future 不太常见的情况下

This is just memoization of futures.

Normal "eager" memoization works like this:

f_memo(x):
  critical_section:
    if (exists answers(f,x))
      return answers(f,x)
    else
      a = f(x)
      answers(f,x) = a
      return a

Now if f(x) returns futures instead of actual results, the above code works as is. You get the piggyback effect, i.e. like this:

  1. First thread calls f(3)
  2. There is no stored answer for f(3), so in the critical section there's a call to f(3). f(3) is implemented as returning a future, so the 'answer' is ready immediately; 'a' in the code above is set to the future F and the future F is stored in the answers table
  3. The future F is returned as the "result" of the call f(3), which is potentially still ongoing
  4. Another thread calls f(3)
  5. The future F is found from the table, and returned immediately
  6. Now both threads have handle to the result of the computation; when they try to read it, they block until the computation is ready---in the post this communication mechanism was mentioned as being implemented by a callback, presumeably in a context where futures are less common
待天淡蓝洁白时 2024-08-31 02:44:27

在某些情况下,我听说过这称为“请求合并”。

In some contexts, I've heard this called "Request Merging".

药祭#氼 2024-08-31 02:44:27

听起来有点像延迟评估,但不完全是......

Sounds a little like Lazy Evaluation, but not exactly...

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