From 57e9e5bf23ba0d498ffaedf8f8df37ad2968d4dd Mon Sep 17 00:00:00 2001 From: Jim Date: Thu, 16 Jul 2015 08:29:02 -0700 Subject: [PATCH] Improved performance for our adler32 implementation --- src/shared/utils/__tests__/adler32-test.js | 41 ++++++++++++++++++++++ src/shared/utils/adler32.js | 31 ++++++++++++---- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/shared/utils/__tests__/adler32-test.js diff --git a/src/shared/utils/__tests__/adler32-test.js b/src/shared/utils/__tests__/adler32-test.js new file mode 100644 index 0000000000..040cb32e1f --- /dev/null +++ b/src/shared/utils/__tests__/adler32-test.js @@ -0,0 +1,41 @@ +/** + * Copyright 2014-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails react-core + */ + +'use strict'; + +var adler32 = require('adler32'); + +describe('shallowEqual', function() { + it('generates differing checksums', function() { + expect(adler32('foo')).not.toBe(adler32('bar')); + }); + + it('generates consistent checksums', function() { + expect(adler32('linux')).toBe(adler32('linux')); + }); + + it('is case sensitive', function() { + expect(adler32('a')).not.toBe(adler32('A')); + }); + + it('doesn\'t barf on large inputs', function() { + var str = ''; + for (var i = 0; i < 100000; i++) { + str = str + 'This will be repeated to be very large indeed. '; + } + expect(adler32(str)).toBe(692898118); + }); + + it('doesn\'t barf on international inputs', function() { + var str = 'Linux 是一個真棒操作系統!'; + expect(adler32(str)).toBe(-1183804097); + }); +}); diff --git a/src/shared/utils/adler32.js b/src/shared/utils/adler32.js index 37d62dbcf3..a29b4fe5fa 100644 --- a/src/shared/utils/adler32.js +++ b/src/shared/utils/adler32.js @@ -13,17 +13,34 @@ var MOD = 65521; -// This is a clean-room implementation of adler32 designed for detecting -// if markup is not what we expect it to be. It does not need to be -// cryptographically strong, only reasonably good at detecting if markup -// generated on the server is different than that on the client. +// adler32 is not cryptographically strong, and is only used to sanity check that +// markup generated on the server matches the markup generated on the client. +// This implementation (a modified version of the SheetJS version) has been optimized +// for our use case, at the expense of conforming to the adler32 specification +// for non-ascii inputs. function adler32(data) { var a = 1; var b = 0; - for (var i = 0; i < data.length; i++) { - a = (a + data.charCodeAt(i)) % MOD; - b = (b + a) % MOD; + var i = 0; + var l = data.length; + var m = l & ~0x3; + while (i < m) { + for (; i < Math.min(i + 4096, m); i += 4) { + b += ( + (a += data.charCodeAt(i)) + + (a += data.charCodeAt(i + 1)) + + (a += data.charCodeAt(i + 2)) + + (a += data.charCodeAt(i + 3)) + ); + } + a %= MOD; + b %= MOD; } + for (; i < l; i++) { + b += (a += data.charCodeAt(i)); + } + a %= MOD; + b %= MOD; return a | (b << 16); }