什么时候应该使用 XS?

发布于 2024-11-19 19:06:38 字数 42 浏览 3 评论 0 原文

我正在写一篇关于 XS 的演讲,我需要知道社区何时认为适合使用 XS。

I am writing up a talk on XS and I need to know when the community thinks it is proper to reach for XS.

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

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

发布评论

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

评论(2

离鸿 2024-11-26 19:06:38

我可以想到至少三个使用 XS 的原因:

  1. 您有一个想要在 Perl 5 中访问的 C 库
  2. 您有一个代码块,它确实会减慢您的程序,如果用 C 编写,它会更快
  3. 您需要访问仅在 XS

Reason 1 中可用的东西是显而易见的,无需解释。

当你确实需要理由 2 时就不那么明显了。通常,您最好查看代码的结构。仅当您分析了您的代码 并有一个 基准测试套件来证明 XS 代码更快且正确。

原因3是一个危险的原因。您实际上很少需要研究 Perl 的内部结构才能做某事,但至少有 一个有效案例

I can think of at least three reasons to use XS:

  1. You have a C library you want to access in Perl 5
  2. You have a block of code that is provably slowing down your program and it would be faster if written in C
  3. You need access to something only available in XS

Reason 1 is obvious and should need no explaination.

When you really need reason 2 is less obvious. Often you are better off looking at how the code is structured. You should only invoke reason 2 if you have profiled your code and have a benchmark and test suite to prove that the XS code is faster and correct.

Reason 3 is a dangerous reason. It is rare that you actually need to look into Perl's guts to do something, but there is at least one valid case.

混浊又暗下来 2024-11-26 19:06:38

在某些情况下,更好的内存管理是使用 XS 的另一个原因。例如,如果您有大量类似类型的对象,则可以通过 XS 更有效地管理。 KinoSearch 将其用于标记,例如,通过 XS 可以比作为一个巨大的标量池更有效地管理大字符串中的开始和结束偏移量。 PDL 还有内存管理方面的内容以及速度。

从长远来看,有人建议将这种方法的一部分集成到核心 Perl 中,最初是因为它提供了更好地共享线程数据的机会:请参阅:http://openparallel.com/2011/07/05/a-new-hope-for-efficient-safe-data-sharing- Between-threads-in-perl/

In a few cases, better memory management is another reason for using XS. For example, if you have a very large block of objects of some similar type, this can be managed more efficiently through XS. KinoSearch uses this for tokens, for example, where start and end offsets in a large string can be managed more effectively through XS than as a huge pool of scalars. PDL also has a memory management aspect to it, as well as speed.

There are proposals to integrate some of this approach into core Perl in the long term, initially because it offers a chance to make sharing data in threading better: see: http://openparallel.com/2011/07/05/a-new-hope-for-efficient-safe-data-sharing-between-threads-in-perl/.

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