Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use String#start_with? to detect leading special character #12

Merged
merged 1 commit into from
Nov 30, 2022

Conversation

elebow
Copy link
Contributor

@elebow elebow commented Nov 29, 2022

This change uses Ruby's built-in String#start_with?, which is implemented entirely in C (in MRI, anyway) and faster than the current implementation. String#start_with? is available in all versions of Ruby that CSVSafe supports.

Benchmarks show a 4.1% speed improvement in line generation.

require "benchmark"

Benchmark.bmbm do |bm_out|
	bm_out.report { 5_000_000.times { CSVSafe.generate_line(["|yes", "no"]) } }
end

Legacy code:

       user     system      total        real
  92.012501   0.246394  92.258895 ( 92.277018)

This branch:

       user     system      total        real
  88.270658   0.248487  88.519145 ( 88.541700)

The #starts_with_special_character? method itself is between 20.9% and 28.2% faster, using a similar benchmark.

I was unable to find any cases where this branch is slower.

@zvory zvory merged commit 7d6fe6b into zvory:master Nov 30, 2022
@zvory
Copy link
Owner

zvory commented Nov 30, 2022

thank you

@zvory
Copy link
Owner

zvory commented Nov 30, 2022

rolled out in #14 and released to rubygems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants