Skip to content

Commit

Permalink
[opt](cache elimation) using step decending method to elimiate cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-Extras committed Sep 21, 2024
1 parent 00be77e commit cca827d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
44 changes: 44 additions & 0 deletions be/src/util/algorithm_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <cstdint>
#include <utility>

#include "common/status.h"
namespace doris {
class AlgoUtil {
// descent the value step by step not linear continuity
double descent_by_step(int step_num, int64_t low_bound, int64_t high_bound, int64_t current) {
if (current <= low_bound) {
return 1;
}
if (current >= high_bound) {
return 0;
}
if (high_bound <= low_bound) {
// Invalid
return 0;
}
int64_t step_size = (int64_t)std::floor((high_bound - low_bound) / step_num);
int64_t used_step = (int64_t)std::ceil((current - low_bound) / step_size);
int64_t left_step = step_num - used_step;
return left_step / step_num;
}
};
} // namespace doris
41 changes: 41 additions & 0 deletions be/test/util/algo_util_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>

#include <boost/utility/binary.hpp>
#include <memory>

#include "gtest/gtest_pred_impl.h"
#include "util/algorithm_util.h"

namespace doris {

TEST(TestAlgoUtil, DescentByStep) {
// double descent_by_step(int step_num, int64_t low_bound, int64_t high_bound, int64_t current)
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 101), 0.9);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 99), 1);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 200, 100, 101), 0);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 111), 0.8);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 188), 0.1);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 100), 1);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 200), 0);
EXPECT_EQ(AlgoUtil::descent_by_step(10, 100, 200, 300), 0);
}

} // namespace doris

0 comments on commit cca827d

Please sign in to comment.