SlideShare une entreprise Scribd logo
1  sur  15
Java Performance

Playing toStrings

By Konstantin Pavlov
konstantinpavlov.net
linkedin.com/in/kpavlov
Object.toString()
❖

Returns string informative representation of an object.

❖

Implementation in class Object returns
<class name>@<hashCode>

❖

Is overridden to provide better object view in logs

❖

Application business logic should not relay on a value
returned by this method
What can be wrong with
toString() method?
There is no problem unless
you're getting a High Load!
❖

Depending on logging strategies, may be called very
frequently

❖

This may slow down your system significantly

❖

Creating new object instances (ToStringBuilder) with
method is cheap but not free.
Implementation strategies:
Long Living Objects
❖

There are a limited number of long living objects

❖

A set of objects may change at runtime

❖

Used as Map keys

❖

toString is called frequently (logging)

❖

toString is called on the same instance multiple times
Long Living Objects. Solution
❖

Since the instance set is not known at compile time, it is
not possible to use Enums.

❖

Use object pool. You'll also be able to use identity
equals(==) and identity hashCode.

❖

Evaluate and cache toString value in field of the object

❖

It’s OK to use ToStringBuilder for evaluation

❖

Keep toString value as short as possible
Short Living Objects

❖

e.g. Data Transfer Objects (DTO)

❖

Created frequently

❖

toString usually called once
Short Living Objects. Receipts

❖

Avoid using ToStringBuilder, no need to create extra
objects

❖

Use as little fields in toString as possible
Other objects

❖

It is not known yet how frequently object will be used

❖

How long is lifetime?
Other Objects. Solution

❖

Follow KISS principle, keep it simple!

❖

Code must be readable and maintainable!
Final Thoughts
❖

Don't spend much time on optimization on early phases
of the project

❖

But don't put a performance bomb in your code. Try to
predict future object usage and don't write a code which
will definitely slow your application.
Thanks!

konstantinpavlov.net
linkedin.com/in/kpavlov

Contenu connexe

Tendances

APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular Expression
Byte
 

Tendances (20)

Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015
 
Basics of dictionary object
Basics of dictionary objectBasics of dictionary object
Basics of dictionary object
 
APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular Expression
 
Slide smallfiles
Slide smallfilesSlide smallfiles
Slide smallfiles
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
Object Class
Object Class Object Class
Object Class
 
The Other Programming Languages
The Other Programming LanguagesThe Other Programming Languages
The Other Programming Languages
 
Be a Zen monk, the Python way
Be a Zen monk, the Python wayBe a Zen monk, the Python way
Be a Zen monk, the Python way
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)
 
Intro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and ArrayIntro to JavaScript - Week 4: Object and Array
Intro to JavaScript - Week 4: Object and Array
 
12. Stack
12. Stack12. Stack
12. Stack
 
Class
ClassClass
Class
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
Session 14 - Object Class
Session 14 - Object ClassSession 14 - Object Class
Session 14 - Object Class
 
Using Elastiknn for exact and approximate nearest neighbor search
Using Elastiknn for exact and approximate nearest neighbor searchUsing Elastiknn for exact and approximate nearest neighbor search
Using Elastiknn for exact and approximate nearest neighbor search
 
Streaming data to s3 using akka streams
Streaming data to s3 using akka streamsStreaming data to s3 using akka streams
Streaming data to s3 using akka streams
 
“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection
 
R meetup talk
R meetup talkR meetup talk
R meetup talk
 
ORM: Object-relational mapping
ORM: Object-relational mappingORM: Object-relational mapping
ORM: Object-relational mapping
 

En vedette

March HHT news letter
March HHT news letterMarch HHT news letter
March HHT news letter
Squadron FRSA
 
Mandar Sant _ Branding & Promotion
Mandar Sant _ Branding & PromotionMandar Sant _ Branding & Promotion
Mandar Sant _ Branding & Promotion
MANDAR SANT
 
публикация2
публикация2публикация2
публикация2
andrieieva64
 
Certificate_IFormBuilder
Certificate_IFormBuilderCertificate_IFormBuilder
Certificate_IFormBuilder
Fredson Joseph
 
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
Dom DECRESSAIN
 
Orientaciones sobre los videojuegos de Jesús Jarque
Orientaciones sobre los videojuegos de Jesús JarqueOrientaciones sobre los videojuegos de Jesús Jarque
Orientaciones sobre los videojuegos de Jesús Jarque
cristinatesti
 

En vedette (19)

brackinresume2
brackinresume2brackinresume2
brackinresume2
 
Daily Forex News February 21st 2013
Daily Forex News February 21st 2013Daily Forex News February 21st 2013
Daily Forex News February 21st 2013
 
March HHT news letter
March HHT news letterMarch HHT news letter
March HHT news letter
 
Phuong phap giai nhanh bai toan hoa hoc
Phuong phap giai nhanh bai toan hoa hocPhuong phap giai nhanh bai toan hoa hoc
Phuong phap giai nhanh bai toan hoa hoc
 
【再放送】スタートアップのための広報・PR短期特訓
【再放送】スタートアップのための広報・PR短期特訓【再放送】スタートアップのための広報・PR短期特訓
【再放送】スタートアップのための広報・PR短期特訓
 
Mandar Sant _ Branding & Promotion
Mandar Sant _ Branding & PromotionMandar Sant _ Branding & Promotion
Mandar Sant _ Branding & Promotion
 
Certificat_uml
Certificat_umlCertificat_uml
Certificat_uml
 
Asociación Interim Management España
Asociación Interim Management EspañaAsociación Interim Management España
Asociación Interim Management España
 
публикация2
публикация2публикация2
публикация2
 
Certificate_IFormBuilder
Certificate_IFormBuilderCertificate_IFormBuilder
Certificate_IFormBuilder
 
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
DECRESSAIN DOMINIQUE Curriculum VitaeDom2014
 
Orientaciones sobre los videojuegos de Jesús Jarque
Orientaciones sobre los videojuegos de Jesús JarqueOrientaciones sobre los videojuegos de Jesús Jarque
Orientaciones sobre los videojuegos de Jesús Jarque
 
Reglamento de prestación de servicios del AyA enero 2016
Reglamento de prestación de servicios del AyA enero 2016Reglamento de prestación de servicios del AyA enero 2016
Reglamento de prestación de servicios del AyA enero 2016
 
American clothing brands
American clothing brandsAmerican clothing brands
American clothing brands
 
The Best Construction Technology Innovations Of 2015
The Best Construction Technology Innovations Of 2015The Best Construction Technology Innovations Of 2015
The Best Construction Technology Innovations Of 2015
 
MAHNAMA KANZULIMAN FEBRUARY 2016
MAHNAMA KANZULIMAN FEBRUARY 2016MAHNAMA KANZULIMAN FEBRUARY 2016
MAHNAMA KANZULIMAN FEBRUARY 2016
 
Presentation of my HumanGuide® Concepts - Improves at Work and in Life!
Presentation of my HumanGuide® Concepts - Improves at Work and in Life!Presentation of my HumanGuide® Concepts - Improves at Work and in Life!
Presentation of my HumanGuide® Concepts - Improves at Work and in Life!
 
Certificats_Ayitec
Certificats_AyitecCertificats_Ayitec
Certificats_Ayitec
 
Multiplicités de devenir média
Multiplicités de devenir médiaMultiplicités de devenir média
Multiplicités de devenir média
 

Similaire à Playing the toStrings

Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Baruch Sadogursky
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
msg systems ag
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
Muhammad Abdullah
 
Java Intro
Java IntroJava Intro
Java Intro
backdoor
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
yohanbeschi
 

Similaire à Playing the toStrings (20)

Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Java script
Java scriptJava script
Java script
 
Java performance
Java performanceJava performance
Java performance
 
Java
Java Java
Java
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Java tutorial part 4
Java tutorial part 4Java tutorial part 4
Java tutorial part 4
 
Skillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet appSkillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet app
 
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
.Net Garbage Collector 101
.Net Garbage Collector 101.Net Garbage Collector 101
.Net Garbage Collector 101
 
The programming philosophy of jrql
The programming philosophy of jrqlThe programming philosophy of jrql
The programming philosophy of jrql
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
Java Intro
Java IntroJava Intro
Java Intro
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 

Playing the toStrings

  • 1. Java Performance Playing toStrings By Konstantin Pavlov konstantinpavlov.net linkedin.com/in/kpavlov
  • 2. Object.toString() ❖ Returns string informative representation of an object. ❖ Implementation in class Object returns <class name>@<hashCode> ❖ Is overridden to provide better object view in logs ❖ Application business logic should not relay on a value returned by this method
  • 3. What can be wrong with toString() method?
  • 4. There is no problem unless you're getting a High Load!
  • 5. ❖ Depending on logging strategies, may be called very frequently ❖ This may slow down your system significantly ❖ Creating new object instances (ToStringBuilder) with method is cheap but not free.
  • 7. Long Living Objects ❖ There are a limited number of long living objects ❖ A set of objects may change at runtime ❖ Used as Map keys ❖ toString is called frequently (logging) ❖ toString is called on the same instance multiple times
  • 8. Long Living Objects. Solution ❖ Since the instance set is not known at compile time, it is not possible to use Enums. ❖ Use object pool. You'll also be able to use identity equals(==) and identity hashCode. ❖ Evaluate and cache toString value in field of the object ❖ It’s OK to use ToStringBuilder for evaluation ❖ Keep toString value as short as possible
  • 9. Short Living Objects ❖ e.g. Data Transfer Objects (DTO) ❖ Created frequently ❖ toString usually called once
  • 10. Short Living Objects. Receipts ❖ Avoid using ToStringBuilder, no need to create extra objects ❖ Use as little fields in toString as possible
  • 11. Other objects ❖ It is not known yet how frequently object will be used ❖ How long is lifetime?
  • 12. Other Objects. Solution ❖ Follow KISS principle, keep it simple! ❖ Code must be readable and maintainable!
  • 14. ❖ Don't spend much time on optimization on early phases of the project ❖ But don't put a performance bomb in your code. Try to predict future object usage and don't write a code which will definitely slow your application.