Return-Path: X-Original-To: apmail-aurora-commits-archive@minotaur.apache.org Delivered-To: apmail-aurora-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 654EE10FFE for ; Thu, 23 Jan 2014 22:30:29 +0000 (UTC) Received: (qmail 224 invoked by uid 500); 23 Jan 2014 22:30:29 -0000 Delivered-To: apmail-aurora-commits-archive@aurora.apache.org Received: (qmail 201 invoked by uid 500); 23 Jan 2014 22:30:28 -0000 Mailing-List: contact commits-help@aurora.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@aurora.incubator.apache.org Delivered-To: mailing list commits@aurora.incubator.apache.org Received: (qmail 194 invoked by uid 99); 23 Jan 2014 22:30:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jan 2014 22:30:28 +0000 X-ASF-Spam-Status: No, hits=-1998.9 required=5.0 tests=ALL_TRUSTED,FR_ALMOST_VIAG2,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 23 Jan 2014 22:30:27 +0000 Received: (qmail 99229 invoked by uid 99); 23 Jan 2014 22:30:07 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jan 2014 22:30:07 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id D62D38BDA24; Thu, 23 Jan 2014 22:30:06 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: wfarner@apache.org To: commits@aurora.incubator.apache.org Message-Id: <242da1eed5c142dca2ad7d17df74c387@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: AURORA-58: Cache hashCode in generated immutable classes. Date: Thu, 23 Jan 2014 22:30:06 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org Updated Branches: refs/heads/master 60b119a50 -> bbfe7367f AURORA-58: Cache hashCode in generated immutable classes. I've also silenced the code generator output by default, cleaning up the output when building via gradle. Bugs closed: AURORA-58 Reviewed at https://reviews.apache.org/r/17088/ Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/bbfe7367 Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/bbfe7367 Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/bbfe7367 Branch: refs/heads/master Commit: bbfe7367f1da1b56d18d0e57f316173e7020f635 Parents: 60b119a Author: Bill Farner Authored: Thu Jan 23 14:29:52 2014 -0800 Committer: Bill Farner Committed: Thu Jan 23 14:29:52 2014 -0800 ---------------------------------------------------------------------- .../aurora/tools/java/thrift_wrapper_codegen.py | 44 +++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/bbfe7367/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py ---------------------------------------------------------------------- diff --git a/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py b/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py index a321ccb..3143c6a 100644 --- a/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py +++ b/src/main/python/apache/aurora/tools/java/thrift_wrapper_codegen.py @@ -20,6 +20,8 @@ import os import re import sys +from optparse import OptionParser + class Type(object): '''A data type.''' @@ -152,6 +154,7 @@ package %(package)s; */ public final class %(name)s { private final %(wrapped)s wrapped; + private int cachedHashCode = 0; %(fields)s private %(name)s(%(wrapped)s wrapped) { this.wrapped = Preconditions.checkNotNull(wrapped);%(assignments)s @@ -214,7 +217,14 @@ public final class %(name)s { @Override public int hashCode() { - return wrapped.hashCode(); + // Following java.lang.String's example of caching hashCode. + // This is thread safe in that multiple threads may wind up + // computing the value, which is apparently favorable to constant + // synchronization overhead. + if (cachedHashCode == 0) { + cachedHashCode = wrapped.hashCode(); + } + return cachedHashCode; } @Override @@ -233,9 +243,6 @@ class GeneratedCode(object): self._fields = [] self._assignments = [] - def add(self, s, end='\n'): - print('This no longer does anything.') - def add_import(self, import_class): self._imports.add(import_class) @@ -422,13 +429,24 @@ THRIFT_ALIASES = { } -def main(args): - if len(args) != 4: +if __name__ == '__main__': + parser = OptionParser() + parser.add_option('-v', '--verbose', + dest='verbose', + action='store_true', + help='Display extra information about code generation.') + options, args = parser.parse_args() + + def log(value): + if options.verbose: + print(value) + + if len(args) != 3: print('usage: %s thrift_file struct_name output_directory' % sys.argv[0]) sys.exit(1) thrift_file, struct_name, output_directory = sys.argv[1:] - print('Searching for %s in %s' % (sys.argv[2], sys.argv[1])) + log('Searching for %s in %s' % (sys.argv[2], sys.argv[1])) with open(sys.argv[1]) as f: # Load all structs found in the thrift file. structs = parse_structs(f.read()) @@ -466,24 +484,20 @@ def main(args): return symbol find_symbol(sys.argv[2]) - print('Symbol table:') + log('Symbol table:') for _, symbol in symbol_table.items(): - print(' %s' % symbol) + log(' %s' % symbol) for _, symbol in symbol_table.items(): if isinstance(symbol, StructType): if symbol.kind == 'enum': - print('Skipping code generation for %s, since it is immutable' % symbol.name) + log('Skipping code generation for %s, since it is immutable' % symbol.name) else: package_dir = os.path.join(sys.argv[3], PACKAGE_NAME.replace('.', os.path.sep)) if not os.path.isdir(package_dir): os.makedirs(package_dir) gen_file = os.path.join(package_dir, '%s.java' % symbol.codegen_name) - print('Generating %s' % gen_file) + log('Generating %s' % gen_file) with open(gen_file, 'w') as f: code = generate_java(symbol) code.dump(f) - - -if __name__ == '__main__': - main(sys.argv)