From 7f12bfa5633da83fb736bb65a83cf814b951d709 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 23 May 2021 17:22:57 +0300 Subject: [PATCH] builtin: optimise `sx == sy` in the case where strings have common prefixes --- vlib/builtin/string.v | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index da89143852..6bc0421812 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -496,6 +496,7 @@ pub fn (s string) u64() u64 { } // eq implements the `s == a` (equal) operator. +[direct_array_access] fn (s string) eq(a string) bool { if s.str == 0 { // should never happen @@ -504,6 +505,12 @@ fn (s string) eq(a string) bool { if s.len != a.len { return false } + if s.len > 0 { + last_idx := s.len - 1 + if s[last_idx] != a[last_idx] { + return false + } + } unsafe { return C.memcmp(s.str, a.str, a.len) == 0 }