Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: optimizes time variables. #1242

Merged
merged 2 commits into from
Dec 9, 2024
Merged

Conversation

jcchavezs
Copy link
Member

@jcchavezs jcchavezs commented Dec 9, 2024

Follows #1223

This PR adds a benchmark and also optimization for time variables

Before

BenchmarkTransactionTimestamped-12       6194395           165.1 ns/op        16 B/op          2 allocs/op

After

BenchmarkTransactionTimestamped-12      12029612            99.59 ns/op       16 B/op          2 allocs/op

cc @geoolekom

Make sure that you've checked the boxes below before you submit PR:

Thanks for your contribution ❤️

@jcchavezs jcchavezs requested a review from a team as a code owner December 9, 2024 09:51
Copy link

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.68%. Comparing base (f3d06a2) to head (959ccfe).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1242   +/-   ##
=======================================
  Coverage   81.67%   81.68%           
=======================================
  Files         168      168           
  Lines        9757     9762    +5     
=======================================
+ Hits         7969     7974    +5     
  Misses       1537     1537           
  Partials      251      251           
Flag Coverage Δ
coraza.rule.case_sensitive_args_keys 81.64% <100.00%> (+<0.01%) ⬆️
coraza.rule.multiphase_valuation 81.68% <100.00%> (+<0.01%) ⬆️
coraza.rule.no_regex_multiline 81.62% <100.00%> (+<0.01%) ⬆️
default 81.68% <100.00%> (+<0.01%) ⬆️
examples+ 16.55% <100.00%> (+0.04%) ⬆️
examples+coraza.rule.case_sensitive_args_keys 81.64% <100.00%> (+<0.01%) ⬆️
examples+coraza.rule.multiphase_valuation 81.51% <100.00%> (+<0.01%) ⬆️
examples+coraza.rule.no_regex_multiline 81.54% <100.00%> (+<0.01%) ⬆️
examples+memoize_builders 81.64% <100.00%> (+<0.01%) ⬆️
examples+no_fs_access 80.96% <100.00%> (+<0.01%) ⬆️
ftw 81.68% <100.00%> (+<0.01%) ⬆️
memoize_builders 81.77% <100.00%> (+<0.01%) ⬆️
no_fs_access 81.13% <100.00%> (+<0.01%) ⬆️
tinygo 81.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

tx.variables.timeMin.Set(timeOnly[3:5])
tx.variables.timeSec.Set(timeOnly)

y, m, d := timestamp.Date()
Copy link
Member Author

Choose a reason for hiding this comment

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

timestamp.[Day|Month|Year]() calls t.date(true) and ignores pieces of output, hence using timestamp.Date() uses all values and saves time.


timeOnly := timestamp.Format(time.TimeOnly)
tx.variables.time.Set(timeOnly)
tx.variables.timeHour.Set(timeOnly[0:2])
Copy link
Member Author

Choose a reason for hiding this comment

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

👅

Comment on lines +1624 to +1626
tx.variables.timeDay.Set(strconv.Itoa(d))
tx.variables.timeMon.Set(strconv.Itoa(int(m)))
tx.variables.timeYear.Set(strconv.Itoa(y))
Copy link
Member

Choose a reason for hiding this comment

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

Do we save some cycles by converting at read time instead? Or the optimization it is negligible?

Copy link
Member Author

Choose a reason for hiding this comment

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

We cannot convert at read time unless lazy collections.

Copy link
Member

Choose a reason for hiding this comment

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

You can. Instead of doing strconv.Itoa(y), you just store y.

Then, when reading, you return strconv.Itoa(y).

Copy link
Member Author

Choose a reason for hiding this comment

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

timeYear is a Single variable so you store strings.

@jcchavezs jcchavezs enabled auto-merge (squash) December 9, 2024 19:18
@jcchavezs jcchavezs merged commit 8176c09 into main Dec 9, 2024
71 checks passed
@jcchavezs jcchavezs deleted the optimizes_timestamp_variables branch December 9, 2024 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants