chiapos icon indicating copy to clipboard operation
chiapos copied to clipboard

Improved chacha8 performance with MSVC

Open ydipeepo opened this issue 4 years ago • 11 comments

This improves the performance of chacha8 a little with MSVC. (around 23%) Additional my test code and results with same compiler options:

#include <chrono>
#include <vector>
#include <iostream>

#include "chacha8.h"

int main ()
{
	const uint32_t iteration = 100;
	const uint32_t blocks = 4096 * 128;

	std::vector<uint8_t> key;
	std::vector<uint8_t> iv;
	for (int i = 0; i < 32; ++i) key.push_back ((uint8_t)i);
	for (int i = 0; i < 8; ++i) iv.push_back ((uint8_t)i);

	std::vector<uint8_t> out1;
	std::vector<uint8_t> out2;
	out1.resize (blocks * 64);
	out2.resize (blocks * 64);

	chacha8_ctx ctx;
	chacha8_keysetup (&ctx, key.data (), 256, iv.data ());

	std::vector<double> res;
	for (auto i = 0; i < iteration; ++i)
	{
		auto start_org = std::chrono::steady_clock::now ();
		chacha8_get_keystream (&ctx, 0, blocks, out1.data ());
		auto end_org = std::chrono::steady_clock::now ();

		auto start_new = std::chrono::steady_clock::now ();
		chacha8_get_keystream_cst (&ctx, 0, blocks, out2.data ());
		auto end_new = std::chrono::steady_clock::now ();

		if (std::equal (out1.cbegin (), out1.cend (), out2.cbegin ()))
		{
			const auto r =
				(double)std::chrono::duration_cast<std::chrono::nanoseconds>(end_org - start_org).count () /
				(double)std::chrono::duration_cast<std::chrono::nanoseconds>(end_new - start_new).count ();
			res.push_back (r);
			std::cout << "org/new: " << r << std::endl;
		}
		else
			std::cout << "ERR" << std::endl;
	}

	auto avg = 0.0;
	for (auto& r : res) avg += r;
	std::cout << "org/new mean: " << avg / res.size () << std::endl;
}
... abbrev ...
org/new: 1.23318
org/new: 1.21971
org/new: 1.23604
org/new: 1.23034
org/new: 1.24382
org/new: 1.23477
org/new: 1.22043
org/new: 1.25348
org/new: 1.22831
org/new: 1.222
org/new: 1.25699
org/new: 1.23271
org/new: 1.23033
org/new: 1.2323
org/new: 1.22129
org/new: 1.68185
org/new: 1.25155
org/new: 0.963338
org/new: 1.40123
org/new: 1.2432
org/new mean: 1.23764

It has passed all tests. Thanks for your review.

ydipeepo avatar May 30 '21 06:05 ydipeepo

Why closed?

stotiks avatar Jun 12 '21 01:06 stotiks

Not sure why - re-opening

hoffmang9 avatar Jun 12 '21 02:06 hoffmang9

@hoffmang9 @stotiks Just simply embrassed.

ydipeepo avatar Jun 12 '21 07:06 ydipeepo

And also added GCC thunk. 4~5% faster than before but only x64:

... abbrev ...
org/new: 1.04859
org/new: 1.04669
org/new: 1.08676
org/new: 1.05594
org/new: 1.05845
org/new: 1.05931
org/new: 1.04588
org/new: 1.04549
org/new: 1.04905
org/new: 1.05218
org/new mean: 1.04996

ydipeepo avatar Jun 12 '21 07:06 ydipeepo

Create also pull request for https://github.com/madMAx43v3r/chia-plotter

stotiks avatar Jun 12 '21 07:06 stotiks

I'm not sure we have sufficient test coverage of chacha8 to make changes with confidence. Could we have a CI test to run this against a reference implementation?

arvidn avatar Aug 03 '21 16:08 arvidn

'This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed.'

github-actions[bot] avatar Oct 03 '21 11:10 github-actions[bot]

'This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed.'

github-actions[bot] avatar Dec 03 '21 11:12 github-actions[bot]

'This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed.'

github-actions[bot] avatar Feb 02 '22 11:02 github-actions[bot]

This looks great but needs some dev time to review

wjblanke avatar Mar 25 '22 17:03 wjblanke

'This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed.'

github-actions[bot] avatar May 25 '22 11:05 github-actions[bot]