Skip to content

[comment] Lemire's string escape check #5

@prlw1

Description

@prlw1

Just for your amusement...

I was intrigured by Daniel Lemire's blog post on checking whether a string needs escaping, in which he provides a benchmark including optimised SSE2 code.

For fun I tried what I hope is the rather more readable equivalent using <simd>:

#include <simd>
inline bool stdsimd_needs_escaping(std::string_view view) {
  using V = std::datapar::simd<std::string_view::value_type, 16>;
  if (view.size() < V::size()) {
    return simple_needs_escaping(view);
  }
  auto it = view.begin();
  std::datapar::simd_mask<std::string_view::value_type, 16> running{false};
  for (; it + V::size() <= view.end(); it += V::size) {
    V word(std::datapar::unchecked_load<V>(it, V::size()));
    running = running || (word == V{34});
    running = running || (word == V{92});
    //running = running || (std::sub_sat(word,V{31}) == V{0});
    running = running || (word <  V{32});
  }
  if (it < view.end()) {
    V word(std::datapar::unchecked_load<V>(view.data() + view.length() - V::size(), V::size()));
    running = running || (word == V{34});
    running = running || (word == V{92});
    //running = running || (std::sub_sat(word,V{31}) == V{0});
    running = running || (word <  V{32});
  }
  return std::datapar::any_of(running);
}

and obtained a pleasing

simple_needs_escaping                    :   0.74 GB/s   32.4 Ma/s  30.82 ns/d 
branchless_needs_escaping                :   0.89 GB/s   39.1 Ma/s  25.58 ns/d 
table_needs_escaping                     :   2.39 GB/s  104.5 Ma/s   9.57 ns/d 
SIMD implementation is correct: 1 1
simd_needs_escaping                      :   4.90 GB/s  214.8 Ma/s   4.66 ns/d 
std::SIMD implementation is correct: 1 1
std:simd_needs_escaping                  :   4.31 GB/s  188.9 Ma/s   5.29 ns/d 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions