Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Optimize quickfixType Function for Performance & Readability #691

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SahibYar
Copy link

@SahibYar SahibYar commented Feb 6, 2025

Key Optimizations:

✅ Replaces fallthrough with map lookups
• Instead of a long switch with repeated fallthrough statements, we use map[string]bool.
• This makes lookups O(1) instead of scanning all cases in O(n) time.

✅ Improves Readability
• Grouping similar types into separate maps makes the function easier to read and maintain.
• The switch statement is shorter and clearer.

✅ Eliminates Unnecessary Variables
• Returns values directly instead of assigning to quickfixType.
• Instead of assigning prototype inside a switch, we fetch it directly from the map.
• If the type doesn’t exist in fieldTypeMapping, the function exits early.

allowedValues := d.FieldTypeByTag[int(field.tag)].Enums
if len(allowedValues) != 0 {
if _, validValue := allowedValues[string(field.value)]; !validValue {
return ValueIsIncorrect(field.tag)
}
}
// Define field type mappings for easy lookup
fieldTypeMapping := map[string]FieldValue{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instantiation performs a lot of unnecessary allocations which will negatively impact performance

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the addition of comments on this path to help the developer, but on a hot path (versus the code generation path you changed otherwise) a switch statement will perform better than a map lookup. A blurb on why that’s the case: https://stackoverflow.com/questions/46789259/map-vs-switch-performance-in-go

case "PERCENTAGE":
fallthrough
case "FLOAT":
// Define mappings of FIX field types to QuickFIX types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I do like the change here because it’s code generation and readability is more important than performance in this regard

@skrater
Copy link

skrater commented Feb 17, 2025

Can you provide any benchmarks?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants