DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Superior Stream Processing: Apache Flink's Impact on Data Lakehouse Architecture
  • What Is JHipster?
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • How To Ensure Fast JOIN Queries for Self-Service Business Intelligence

Trending

  • Superior Stream Processing: Apache Flink's Impact on Data Lakehouse Architecture
  • What Is JHipster?
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • How To Ensure Fast JOIN Queries for Self-Service Business Intelligence

Making Code Faster: That Pesky Dictionary

Oh no! A dictionary ate up almost 50% of an application's performance. Read on to learn how this terrifying problem was solved.

Oren Eini user avatar by
Oren Eini
·
Nov. 29, 16 · Opinion
Like (1)
Save
Tweet
Share
3.58K Views

Join the DZone community and get the full member experience.

Join For Free

it's a fact of life: if you are looking at high-performance code, a dictionary is probably going to be one of those things that will be painful. let's see how this looks in our current program:

image title

are you kidding me?! here i am worrying about every little bit and byte to try to get the most performance out of the system, and dictionary is eating up almost 50% of my performance?

let's look more deeply into this:

image title

so that's about 3 μs, which is pretty fast...but it isn’t fast enough.

now, know that there are about 200,000 unique values in the dictionary (look at the insert calls in the profiler output), and we have some knowledge about the problem space.

the id that we use has eight characters, so at most, we can have a hundred million ids. an array of that size would be roughly 762 mb in size, so that is doable. however, we also can be fairly certain that the ids are generated in some sequential manner, so there is a strong likelihood that we don’t need all of this space.

i wrote the following function:

private static void increment(ref long[] array, int id, long value)
 {
     if (id < array.length)
     {
         array[id] += value;
         return;
     }
     unlikelygrowarray(ref array, id, value);
 }

 private static void unlikelygrowarray(ref long[] array, int id, long value)
 {
     var size = array.length*2;
     while (id >= size)
         size *= 2;
     array.resize(ref array, size);
     increment(ref array, id, value);
 }

change the stats to start with an array of 256 longs and run it. the results are nice.

image title

this is single-threaded, of course, and it is faster than all the previous multi-threaded versions we had before.

Dictionary (software)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Superior Stream Processing: Apache Flink's Impact on Data Lakehouse Architecture
  • What Is JHipster?
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • How To Ensure Fast JOIN Queries for Self-Service Business Intelligence

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: