[
https://issues.apache.org/jira/browse/THRIFT-2877?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15145902#comment-15145902
]
ASF GitHub Bot commented on THRIFT-2877:
----------------------------------------
Github user nsuke commented on a diff in the pull request:
https://github.com/apache/thrift/pull/448#discussion_r52825710
--- Diff: compiler/cpp/src/generate/t_java_generator.cc ---
@@ -1886,24 +1889,57 @@ void
t_java_generator::generate_java_struct_equality(ofstream& out, t_struct* ts
bool can_be_null = type_can_be_null(t);
string name = (*m_iter)->get_name();
- string present = "true";
+ if (is_optional || can_be_null) {
+ indent(out) << "hashCode = hashCode * " << MUL << " + ((" <<
generate_isset_check(*m_iter)
+ << ") ? " << B_YES << " : " << B_NO << ");" << endl;
+ }
if (is_optional || can_be_null) {
- present += " && (" + generate_isset_check(*m_iter) + ")";
+ indent(out) << "if (" + generate_isset_check(*m_iter) + ")" << endl;
+ indent_up();
}
- indent(out) << "boolean present_" << name << " = " << present << ";"
<< endl;
- indent(out) << "list.add(present_" << name << ");" << endl;
- indent(out) << "if (present_" << name << ")" << endl;
if (t->is_enum()) {
- indent(out) << " list.add(" << name << ".getValue());" << endl;
+ indent(out) << "hashCode = hashCode * " << MUL << " + " << name <<
".getValue();" << endl;
+ } else if (t->is_base_type()) {
+ switch(((t_base_type*)t)->get_base()) {
+ case t_base_type::TYPE_VOID:
+ break;
+ case t_base_type::TYPE_STRING:
+ indent(out) << "hashCode = hashCode * " << MUL << " + " << name
<< ".hashCode();" << endl;
+ break;
+ case t_base_type::TYPE_BOOL:
+ indent(out) << "hashCode = hashCode * " << MUL << " + ((" <<
name << ") ? "
+ << B_YES << " : " << B_NO << ");" << endl;
+ break;
+ case t_base_type::TYPE_I8:
+ indent(out) << "hashCode = hashCode * " << MUL << " + (int) ("
<< name << ");" << endl;
+ break;
+ case t_base_type::TYPE_I16:
+ case t_base_type::TYPE_I32:
+ indent(out) << "hashCode = hashCode * " << MUL << " + " << name
<< ";" << endl;
+ break;
+ case t_base_type::TYPE_I64:
+ indent(out) << "hashCode = hashCode * " << MUL << " +
org.apache.thrift.TBaseHelper.hashCode(" << name << ");" << endl;
+ break;
+ case t_base_type::TYPE_DOUBLE:
+ indent(out) << "hashCode = hashCode * " << MUL << " +
org.apache.thrift.TBaseHelper.hashCode(" << name << ");" << endl;
+ break;
+ default:
+ throw "compiler error: the following base type has no hashcode
generator: " +
+ t_base_type::t_base_name(((t_base_type*)t)->get_base());
--- End diff --
Just a style nit, could you indent switch in the same way as the other part
of this file (less indentation) ?
> Optimize generated hashCode
> ---------------------------
>
> Key: THRIFT-2877
> URL: https://issues.apache.org/jira/browse/THRIFT-2877
> Project: Thrift
> Issue Type: Improvement
> Components: Java - Compiler
> Affects Versions: 0.9.3
> Reporter: Mike Rettig
> Priority: Critical
>
> The generated java hashCode method allocates an ArrayList then appends the
> fields to the list. Primitive fields will be boxed when added to the list.
> The generated code shouldn't allocate a list or box primitives. The hashCode
> can be calculated by using a primitive int and some static utility methods
> which can return the hashCode for each type.
> out << indent() << "@Override" << endl << indent() << "public int hashCode()
> {" << endl;
> 1839 indent_up();
> 1840 indent(out) << "List<Object> list = new ArrayList<Object>();" << endl;
> 1841
> 1842 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
> 1843 out << endl;
> 1844
> 1845 t_type* t = get_true_type((*m_iter)->get_type());
> 1846 bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL;
> 1847 bool can_be_null = type_can_be_null(t);
> 1848 string name = (*m_iter)->get_name();
> 1849
> 1850 string present = "true";
> 1851
> 1852 if (is_optional || can_be_null) {
> 1853 present += " && (" + generate_isset_check(*m_iter) + ")";
> 1854 }
> 1855
> 1856 indent(out) << "boolean present_" << name << " = " << present << ";"
> << endl;
> 1857 indent(out) << "list.add(present_" << name << ");" << endl;
> 1858 indent(out) << "if (present_" << name << ")" << endl;
> 1859 if (t->is_enum()) {
> 1860 indent(out) << " list.add(" << name << ".getValue());" << endl;
> 1861 } else {
> 1862 indent(out) << " list.add(" << name << ");" << endl;
> 1863 }
> 1864 }
> 1865
> 1866 out << endl;
> 1867 indent(out) << "return list.hashCode();" << endl;
> 1868 indent_down();
> 1869 indent(out) << "}" << endl << endl;
> 1870 }
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)