Pregunta

In many places in BLPAPI there is a choice bewteen using string or Name

When should Name be used, and when is it better to use string?

¿Fue útil?

Solución

Name is a class optimized for comparison

It holds a static lookup for every string but each instance of name only contains a pointer (object) and every Name with the same String value will have a Name that contains the same pointer. So comparisons for equality are just comparing those pointers and not the whole string. It is similar to the Java's intern String

It is best to have static Names for every field and MessageType, so comparisons and field lookups are quicker.

Since it keeps a static lookup for every Name, it is best not to use names for field values, since names occupy space in the static lookup and this cannot be cleared.

The one exception for keeping Name's for Values is values for fields of tyype Enum. Enum's in BLPAPI are Strings (not ints) or more precisely they are Names

When the API connects, it downloads the schema, and every field, Messagetype, and Enum is loaded into the Name static lookup, so using names for these is very efficient.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top