Are UTF-32 strings more efficient than UTF-{8,16} for searching?

agolangf · 2017-10-17 04:00:12 · 574 次点击    
这是一个分享于 2017-10-17 04:00:12 的资源,其中的信息可能已经有所发展或是发生改变。

Variable width string encodings like UTF-8 and UTF-16 are tough to slice reliably, since characters can straddle multiple array indices. I wonder if some string operations like skipping N runes are more efficient in UTF-32? Anyone know if document stores apply this advantage when manipulating large bodies of text?


评论:

gohacker:

https://manishearth.github.io/blog/2017/01/14/stop-ascribing-meaning-to-unicode-code-points/

mcandre:

Wowwowweewow!

In point of fact, is a Go rune a code point or a multi-code-point unit?

gohacker:

It's a code point.

type rune = int32

https://golang.org/pkg/builtin/#rune

stone_henge:

In terms of skipping code points, UTF-32 is simpler, and intuitively seems like it would be more efficient, but since UTF-32 is generally more spacious than UTF-8, the cache may be a concern and the logic required to step past UTF-8 code points could be faster than loading more memory.

In terms of testing equality or finding needles in a haystack, speed is only a matter of size, and UTF-8 code points will be as short or shorter than UTF-32 code points, so faster or as fast. UTF-8 is encoded so that a partial code point can not be mistaken for a full code point which simplifies searching a lot. It's like searching any string of bytes.

drvd:

Well, as usual: This depends. Measure. Any answer of the form "Yes!" or "No!" is plain bullshit.

jlinkels:

Most search engines use UTF-8, and don't bother converting because they operate at the byte level.

This gets hard when doing case insensitive matching -- the branching factors get gigantic, and it becomes a lot less effective to use a simple memchr search. One strategy to deal with this is to down case the entire search document and the query before matching the query.

Check out http://blog.burntsushi.net/ripgrep/ for some interesting search info.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

574 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传