Skip to content

Commit 09da72f

Browse files
committed
Use integer constants instead of strings for refund types
Memory optimization for large batches: - 20,000 orders with strings: ~160-320 KB - 20,000 orders with integers: ~160 KB + better cache locality - Constants: REFUND_TYPE_NONE/FULL/PARTIAL/MULTI (0-3)
1 parent 36d534e commit 09da72f

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

includes/Generator/Order.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class Order extends Generator {
3333
*/
3434
const SECOND_REFUND_MAX_DAYS = 30;
3535

36+
/**
37+
* Refund type constants for memory-efficient batch operations.
38+
*/
39+
const REFUND_TYPE_NONE = 0;
40+
const REFUND_TYPE_FULL = 1;
41+
const REFUND_TYPE_PARTIAL = 2;
42+
const REFUND_TYPE_MULTI = 3;
43+
3644
/**
3745
* Pre-generated coupon flags for exact ratio distribution in batch mode.
3846
* Each element is a boolean: true = apply coupon, false = skip.
@@ -43,7 +51,7 @@ class Order extends Generator {
4351

4452
/**
4553
* Pre-generated refund flags for exact ratio distribution in batch mode.
46-
* Each element is a string: 'none', 'full', 'partial', or 'multi'.
54+
* Each element is an integer constant: REFUND_TYPE_NONE, REFUND_TYPE_FULL, etc.
4755
*
4856
* @var array|null
4957
*/
@@ -202,7 +210,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
202210

203211
// Handle --refund-ratio parameter for completed orders
204212
if ( isset( $assoc_args['refund-ratio'] ) && 'completed' === $status ) {
205-
$refund_type = 'none';
213+
$refund_type = self::REFUND_TYPE_NONE;
206214

207215
// Use exact ratio flag if in batch mode
208216
if ( null !== self::$batch_refund_flags && ! empty( self::$batch_refund_flags ) ) {
@@ -218,25 +226,25 @@ public static function generate( $save = true, $assoc_args = array() ) {
218226

219227
if ( $refund_ratio >= 1.0 ) {
220228
// Always refund if ratio is 1.0 or higher
221-
$refund_type = 'full';
229+
$refund_type = self::REFUND_TYPE_FULL;
222230
} elseif ( $refund_ratio > 0 && wp_rand( 1, 100 ) <= ( $refund_ratio * 100 ) ) {
223231
// Use random chance for ratios between 0 and 1
224232
// Split evenly between full and partial
225-
$refund_type = (bool) wp_rand( 0, 1 ) ? 'full' : 'partial';
233+
$refund_type = (bool) wp_rand( 0, 1 ) ? self::REFUND_TYPE_FULL : self::REFUND_TYPE_PARTIAL;
226234

227235
// 25% chance for multi-partial
228-
if ( 'partial' === $refund_type && wp_rand( 1, 100 ) <= self::SECOND_REFUND_PROBABILITY ) {
229-
$refund_type = 'multi';
236+
if ( self::REFUND_TYPE_PARTIAL === $refund_type && wp_rand( 1, 100 ) <= self::SECOND_REFUND_PROBABILITY ) {
237+
$refund_type = self::REFUND_TYPE_MULTI;
230238
}
231239
}
232240
}
233241

234242
// Process refund based on type
235-
if ( 'full' === $refund_type ) {
243+
if ( self::REFUND_TYPE_FULL === $refund_type ) {
236244
self::create_refund( $order );
237-
} elseif ( 'partial' === $refund_type ) {
245+
} elseif ( self::REFUND_TYPE_PARTIAL === $refund_type ) {
238246
self::create_refund( $order, true );
239-
} elseif ( 'multi' === $refund_type ) {
247+
} elseif ( self::REFUND_TYPE_MULTI === $refund_type ) {
240248
$first_refund = self::create_refund( $order, true );
241249
if ( $first_refund && is_object( $first_refund ) ) {
242250
self::create_refund( $order, true, $first_refund );
@@ -873,12 +881,12 @@ protected static function init_ratio_flags( $count, $args ) {
873881
$num_multi = $total_refunds - $num_full - $num_partial; // Remainder goes to multi
874882
$num_none = $count - $total_refunds;
875883

876-
// Create array with exact counts
884+
// Create array with exact counts using integer constants for memory efficiency
877885
self::$batch_refund_flags = array_merge(
878-
array_fill( 0, $num_full, 'full' ),
879-
array_fill( 0, $num_partial, 'partial' ),
880-
array_fill( 0, $num_multi, 'multi' ),
881-
array_fill( 0, $num_none, 'none' )
886+
array_fill( 0, $num_full, self::REFUND_TYPE_FULL ),
887+
array_fill( 0, $num_partial, self::REFUND_TYPE_PARTIAL ),
888+
array_fill( 0, $num_multi, self::REFUND_TYPE_MULTI ),
889+
array_fill( 0, $num_none, self::REFUND_TYPE_NONE )
882890
);
883891

884892
// Shuffle for randomness

0 commit comments

Comments
 (0)