在Cloud Firestore中使用自定义对象是否会导致性能问题?

huangapple go评论63阅读模式
英文:

Does using Custom Objects in Cloud Firestore cause performance issues?

问题

关于性能和便利性,使用自定义对象(Custom Object)还是 Java 中的 Map 来在 Firestore 中存储/检索文档,哪个更好?使用自定义对象会导致性能问题吗?

// HashMap
Map<String, Object> sampleMap = new HashMap<>();
sampleMap.put("name", "sample1");
sampleMap.put("age", 26);

// Custom Class
class Sample {
    String name;
    int age;

    public Sample(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

// Custom Object
Sample sample = new Sample("sample1", 26);

FirestoreOptions firestoreOptions =
    FirestoreOptions.getDefaultInstance().toBuilder().setProjectId("project-id")
            .setCredentials(GoogleCredentials.getApplicationDefault()).build();
Firestore db = firestoreOptions.getService();

// 使用自定义对象
db.collection("SampleData").document().set(sample);

// 使用 HashMap
db.collection("SampleData").document().set(sampleMap);

对于每秒 100 次读取和 100 次写入的应用程序,哪个更好?

拥有自定义类是否能够证明性能成本的合理性(如果有的话)?

英文:

In terms of performance and convenience which is better, using Custom Object or Map in Java to store/retrieve documents in Firestore. Does using Custom Object cause performance issues?

// HashMap
Map&lt;String, Object&gt; sampleMap = new HashMap&lt;&gt;();
sampleMap.put(&quot;name&quot;,&quot;sample1&quot;);
sampleMap.put(&quot;age&quot;, 26);
// Custom Class
class Sample {
String name;
int age;
public Sample(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// Custom Object
Sample sample = new Sample(&quot;sample1&quot;, 26);
FirestoreOptions firestoreOptions =
FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(&quot;project-id&quot;)
.setCredentials(GoogleCredentials.getApplicationDefault()).build();
Firestore db = firestoreOptions.getService();
// using Custom Object
db.collection(&quot;SampleData&quot;).document().set(sample);
// using HashMap
db.collection(&quot;SampleData&quot;).document().set(sampleMap);

Which is better for an application with 100 reads/100 writes per second?

Does having a custom class justify the performance cost (if any)?

答案1

得分: 0

我认为即使每秒有100次读写操作,性能成本也不会很高。而且使用自定义类绝对是正确的方法,否则你将不得不使用snapshot.data对象,这可能会导致人为错误等问题。

英文:

I don't think there is a lot of performance costs, even if there are 100 reads/writes per second. And using custom class is definitely the way to go, otherwise you would have to use the snapshot.data object, which can cause human errors etc

答案2

得分: 0

当您将一个普通Java对象(POJO)传递给Firestore时,性能会受到显著影响,特别是第一次使用该类时。这是因为Firestore SDK必须使用JVM反射来将对象序列化或反序列化到文档中。反射通常运行较慢,尽管对于您的用途而言可能足够快。您应该进行基准测试以确切了解情况。

如果您发送和接收Map对象,性能会更好。虽然您需要编写更多的代码,但如果操作正确,代码的运行速度会更快。您需要权衡方便性和性能,决定哪种方法更适合您。

另请参考:

英文:

When you pass a POJO type object to Firestore, there is a significant hit in performance, especially the first time that class is used. That's because the Firestore SDK has to use JVM reflection to serialize or deserialize the object to the document. Reflection is typically pretty slow, though it might be fast enough for your purposes. You should benchmark to know for sure.

You will get better performance if you send and receieve Maps. You will also write more code, but it will definitely run faster if you do it correctly. It's up to you to determine if you prefer convenience over performance.

See also:

huangapple
  • 本文由 发表于 2020年9月6日 15:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定